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 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
13 * Flex-OneNAND support
14 * Copyright (C) Samsung Electronics, 2008
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/onenand.h>
31 #include <linux/mtd/partitions.h>
35 /* Default Flex-OneNAND boundary and lock respectively */
36 static int flex_bdry
[MAX_DIES
* 2] = { -1, 0, -1, 0 };
38 module_param_array(flex_bdry
, int, NULL
, 0400);
39 MODULE_PARM_DESC(flex_bdry
, "SLC Boundary information for Flex-OneNAND"
40 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
41 "DIE_BDRY: SLC boundary of the die"
42 "LOCK: Locking information for SLC boundary"
43 " : 0->Set boundary in unlocked status"
44 " : 1->Set boundary in locked status");
47 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
48 * For now, we expose only 64 out of 80 ecc bytes
50 static struct nand_ecclayout onenand_oob_128
= {
53 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
54 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
55 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
56 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
57 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
58 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
62 {2, 4}, {18, 4}, {34, 4}, {50, 4},
63 {66, 4}, {82, 4}, {98, 4}, {114, 4}
68 * onenand_oob_64 - oob info for large (2KB) page
70 static struct nand_ecclayout onenand_oob_64
= {
79 {2, 3}, {14, 2}, {18, 3}, {30, 2},
80 {34, 3}, {46, 2}, {50, 3}, {62, 2}
85 * onenand_oob_32 - oob info for middle (1KB) page
87 static struct nand_ecclayout onenand_oob_32
= {
93 .oobfree
= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
96 static const unsigned char ffchars
[] = {
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
116 * onenand_readw - [OneNAND Interface] Read OneNAND register
117 * @param addr address to read
119 * Read OneNAND register
121 static unsigned short onenand_readw(void __iomem
*addr
)
127 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
128 * @param value value to write
129 * @param addr address to write
131 * Write OneNAND register with value
133 static void onenand_writew(unsigned short value
, void __iomem
*addr
)
139 * onenand_block_address - [DEFAULT] Get block address
140 * @param this onenand chip data structure
141 * @param block the block
142 * @return translated block address if DDP, otherwise same
144 * Setup Start Address 1 Register (F100h)
146 static int onenand_block_address(struct onenand_chip
*this, int block
)
148 /* Device Flash Core select, NAND Flash Block Address */
149 if (block
& this->density_mask
)
150 return ONENAND_DDP_CHIP1
| (block
^ this->density_mask
);
156 * onenand_bufferram_address - [DEFAULT] Get bufferram address
157 * @param this onenand chip data structure
158 * @param block the block
159 * @return set DBS value if DDP, otherwise 0
161 * Setup Start Address 2 Register (F101h) for DDP
163 static int onenand_bufferram_address(struct onenand_chip
*this, int block
)
165 /* Device BufferRAM Select */
166 if (block
& this->density_mask
)
167 return ONENAND_DDP_CHIP1
;
169 return ONENAND_DDP_CHIP0
;
173 * onenand_page_address - [DEFAULT] Get page address
174 * @param page the page address
175 * @param sector the sector address
176 * @return combined page and sector address
178 * Setup Start Address 8 Register (F107h)
180 static int onenand_page_address(int page
, int sector
)
182 /* Flash Page Address, Flash Sector Address */
185 fpa
= page
& ONENAND_FPA_MASK
;
186 fsa
= sector
& ONENAND_FSA_MASK
;
188 return ((fpa
<< ONENAND_FPA_SHIFT
) | fsa
);
192 * onenand_buffer_address - [DEFAULT] Get buffer address
193 * @param dataram1 DataRAM index
194 * @param sectors the sector address
195 * @param count the number of sectors
196 * @return the start buffer value
198 * Setup Start Buffer Register (F200h)
200 static int onenand_buffer_address(int dataram1
, int sectors
, int count
)
204 /* BufferRAM Sector Address */
205 bsa
= sectors
& ONENAND_BSA_MASK
;
208 bsa
|= ONENAND_BSA_DATARAM1
; /* DataRAM1 */
210 bsa
|= ONENAND_BSA_DATARAM0
; /* DataRAM0 */
212 /* BufferRAM Sector Count */
213 bsc
= count
& ONENAND_BSC_MASK
;
215 return ((bsa
<< ONENAND_BSA_SHIFT
) | bsc
);
219 * flexonenand_block- For given address return block number
220 * @param this - OneNAND device structure
221 * @param addr - Address for which block number is needed
223 static unsigned flexonenand_block(struct onenand_chip
*this, loff_t addr
)
225 unsigned boundary
, blk
, die
= 0;
227 if (ONENAND_IS_DDP(this) && addr
>= this->diesize
[0]) {
229 addr
-= this->diesize
[0];
232 boundary
= this->boundary
[die
];
234 blk
= addr
>> (this->erase_shift
- 1);
236 blk
= (blk
+ boundary
+ 1) >> 1;
238 blk
+= die
? this->density_mask
: 0;
242 inline unsigned onenand_block(struct onenand_chip
*this, loff_t addr
)
244 if (!FLEXONENAND(this))
245 return addr
>> this->erase_shift
;
246 return flexonenand_block(this, addr
);
250 * flexonenand_addr - Return address of the block
251 * @this: OneNAND device structure
252 * @block: Block number on Flex-OneNAND
254 * Return address of the block
256 static loff_t
flexonenand_addr(struct onenand_chip
*this, int block
)
259 int die
= 0, boundary
;
261 if (ONENAND_IS_DDP(this) && block
>= this->density_mask
) {
262 block
-= this->density_mask
;
264 ofs
= this->diesize
[0];
267 boundary
= this->boundary
[die
];
268 ofs
+= (loff_t
)block
<< (this->erase_shift
- 1);
269 if (block
> (boundary
+ 1))
270 ofs
+= (loff_t
)(block
- boundary
- 1) << (this->erase_shift
- 1);
274 loff_t
onenand_addr(struct onenand_chip
*this, int block
)
276 if (!FLEXONENAND(this))
277 return (loff_t
)block
<< this->erase_shift
;
278 return flexonenand_addr(this, block
);
280 EXPORT_SYMBOL(onenand_addr
);
283 * onenand_get_density - [DEFAULT] Get OneNAND density
284 * @param dev_id OneNAND device ID
286 * Get OneNAND density from device ID
288 static inline int onenand_get_density(int dev_id
)
290 int density
= dev_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
291 return (density
& ONENAND_DEVICE_DENSITY_MASK
);
295 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
296 * @param mtd MTD device structure
297 * @param addr address whose erase region needs to be identified
299 int flexonenand_region(struct mtd_info
*mtd
, loff_t addr
)
303 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
304 if (addr
< mtd
->eraseregions
[i
].offset
)
308 EXPORT_SYMBOL(flexonenand_region
);
311 * onenand_command - [DEFAULT] Send command to OneNAND device
312 * @param mtd MTD device structure
313 * @param cmd the command to be sent
314 * @param addr offset to read from or write to
315 * @param len number of bytes to read or write
317 * Send command to OneNAND device. This function is used for middle/large page
318 * devices (1KB/2KB Bytes per page)
320 static int onenand_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
, size_t len
)
322 struct onenand_chip
*this = mtd
->priv
;
323 int value
, block
, page
;
325 /* Address translation */
327 case ONENAND_CMD_UNLOCK
:
328 case ONENAND_CMD_LOCK
:
329 case ONENAND_CMD_LOCK_TIGHT
:
330 case ONENAND_CMD_UNLOCK_ALL
:
335 case FLEXONENAND_CMD_PI_ACCESS
:
336 /* addr contains die index */
337 block
= addr
* this->density_mask
;
341 case ONENAND_CMD_ERASE
:
342 case ONENAND_CMD_BUFFERRAM
:
343 case ONENAND_CMD_OTP_ACCESS
:
344 block
= onenand_block(this, addr
);
348 case FLEXONENAND_CMD_READ_PI
:
349 cmd
= ONENAND_CMD_READ
;
350 block
= addr
* this->density_mask
;
355 block
= onenand_block(this, addr
);
356 page
= (int) (addr
- onenand_addr(this, block
)) >> this->page_shift
;
358 if (ONENAND_IS_2PLANE(this)) {
359 /* Make the even block number */
361 /* Is it the odd plane? */
362 if (addr
& this->writesize
)
366 page
&= this->page_mask
;
370 /* NOTE: The setting order of the registers is very important! */
371 if (cmd
== ONENAND_CMD_BUFFERRAM
) {
372 /* Select DataRAM for DDP */
373 value
= onenand_bufferram_address(this, block
);
374 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
376 if (ONENAND_IS_MLC(this) || ONENAND_IS_2PLANE(this))
377 /* It is always BufferRAM0 */
378 ONENAND_SET_BUFFERRAM0(this);
380 /* Switch to the next data buffer */
381 ONENAND_SET_NEXT_BUFFERRAM(this);
387 /* Write 'DFS, FBA' of Flash */
388 value
= onenand_block_address(this, block
);
389 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
391 /* Select DataRAM for DDP */
392 value
= onenand_bufferram_address(this, block
);
393 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
397 /* Now we use page size operation */
398 int sectors
= 0, count
= 0;
402 case FLEXONENAND_CMD_RECOVER_LSB
:
403 case ONENAND_CMD_READ
:
404 case ONENAND_CMD_READOOB
:
405 if (ONENAND_IS_MLC(this))
406 /* It is always BufferRAM0 */
407 dataram
= ONENAND_SET_BUFFERRAM0(this);
409 dataram
= ONENAND_SET_NEXT_BUFFERRAM(this);
413 if (ONENAND_IS_2PLANE(this) && cmd
== ONENAND_CMD_PROG
)
414 cmd
= ONENAND_CMD_2X_PROG
;
415 dataram
= ONENAND_CURRENT_BUFFERRAM(this);
419 /* Write 'FPA, FSA' of Flash */
420 value
= onenand_page_address(page
, sectors
);
421 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS8
);
423 /* Write 'BSA, BSC' of DataRAM */
424 value
= onenand_buffer_address(dataram
, sectors
, count
);
425 this->write_word(value
, this->base
+ ONENAND_REG_START_BUFFER
);
428 /* Interrupt clear */
429 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
432 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
438 * onenand_read_ecc - return ecc status
439 * @param this onenand chip structure
441 static inline int onenand_read_ecc(struct onenand_chip
*this)
443 int ecc
, i
, result
= 0;
445 if (!FLEXONENAND(this))
446 return this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
448 for (i
= 0; i
< 4; i
++) {
449 ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
+ i
);
452 if (ecc
& FLEXONENAND_UNCORRECTABLE_ERROR
)
453 return ONENAND_ECC_2BIT_ALL
;
455 result
= ONENAND_ECC_1BIT_ALL
;
462 * onenand_wait - [DEFAULT] wait until the command is done
463 * @param mtd MTD device structure
464 * @param state state to select the max. timeout value
466 * Wait for command done. This applies to all OneNAND command
467 * Read can take up to 30us, erase up to 2ms and program up to 350us
468 * according to general OneNAND specs
470 static int onenand_wait(struct mtd_info
*mtd
, int state
)
472 struct onenand_chip
* this = mtd
->priv
;
473 unsigned long timeout
;
474 unsigned int flags
= ONENAND_INT_MASTER
;
475 unsigned int interrupt
= 0;
478 /* The 20 msec is enough */
479 timeout
= jiffies
+ msecs_to_jiffies(20);
480 while (time_before(jiffies
, timeout
)) {
481 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
483 if (interrupt
& flags
)
486 if (state
!= FL_READING
)
489 /* To get correct interrupt status in timeout case */
490 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
492 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
495 * In the Spec. it checks the controller status first
496 * However if you get the correct information in case of
497 * power off recovery (POR) test, it should read ECC status first
499 if (interrupt
& ONENAND_INT_READ
) {
500 int ecc
= onenand_read_ecc(this);
502 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
503 printk(KERN_ERR
"onenand_wait: ECC error = 0x%04x\n", ecc
);
504 mtd
->ecc_stats
.failed
++;
506 } else if (ecc
& ONENAND_ECC_1BIT_ALL
) {
507 printk(KERN_DEBUG
"onenand_wait: correctable ECC error = 0x%04x\n", ecc
);
508 mtd
->ecc_stats
.corrected
++;
511 } else if (state
== FL_READING
) {
512 printk(KERN_ERR
"onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl
, interrupt
);
516 /* If there's controller error, it's a real error */
517 if (ctrl
& ONENAND_CTRL_ERROR
) {
518 printk(KERN_ERR
"onenand_wait: controller error = 0x%04x\n",
520 if (ctrl
& ONENAND_CTRL_LOCK
)
521 printk(KERN_ERR
"onenand_wait: it's locked error.\n");
529 * onenand_interrupt - [DEFAULT] onenand interrupt handler
530 * @param irq onenand interrupt number
531 * @param dev_id interrupt data
535 static irqreturn_t
onenand_interrupt(int irq
, void *data
)
537 struct onenand_chip
*this = data
;
539 /* To handle shared interrupt */
540 if (!this->complete
.done
)
541 complete(&this->complete
);
547 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
548 * @param mtd MTD device structure
549 * @param state state to select the max. timeout value
551 * Wait for command done.
553 static int onenand_interrupt_wait(struct mtd_info
*mtd
, int state
)
555 struct onenand_chip
*this = mtd
->priv
;
557 wait_for_completion(&this->complete
);
559 return onenand_wait(mtd
, state
);
563 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
564 * @param mtd MTD device structure
565 * @param state state to select the max. timeout value
567 * Try interrupt based wait (It is used one-time)
569 static int onenand_try_interrupt_wait(struct mtd_info
*mtd
, int state
)
571 struct onenand_chip
*this = mtd
->priv
;
572 unsigned long remain
, timeout
;
574 /* We use interrupt wait first */
575 this->wait
= onenand_interrupt_wait
;
577 timeout
= msecs_to_jiffies(100);
578 remain
= wait_for_completion_timeout(&this->complete
, timeout
);
580 printk(KERN_INFO
"OneNAND: There's no interrupt. "
581 "We use the normal wait\n");
583 /* Release the irq */
584 free_irq(this->irq
, this);
586 this->wait
= onenand_wait
;
589 return onenand_wait(mtd
, state
);
593 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
594 * @param mtd MTD device structure
596 * There's two method to wait onenand work
597 * 1. polling - read interrupt status register
598 * 2. interrupt - use the kernel interrupt method
600 static void onenand_setup_wait(struct mtd_info
*mtd
)
602 struct onenand_chip
*this = mtd
->priv
;
605 init_completion(&this->complete
);
607 if (this->irq
<= 0) {
608 this->wait
= onenand_wait
;
612 if (request_irq(this->irq
, &onenand_interrupt
,
613 IRQF_SHARED
, "onenand", this)) {
614 /* If we can't get irq, use the normal wait */
615 this->wait
= onenand_wait
;
619 /* Enable interrupt */
620 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
621 syscfg
|= ONENAND_SYS_CFG1_IOBE
;
622 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
624 this->wait
= onenand_try_interrupt_wait
;
628 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
629 * @param mtd MTD data structure
630 * @param area BufferRAM area
631 * @return offset given area
633 * Return BufferRAM offset given area
635 static inline int onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
637 struct onenand_chip
*this = mtd
->priv
;
639 if (ONENAND_CURRENT_BUFFERRAM(this)) {
640 /* Note: the 'this->writesize' is a real page size */
641 if (area
== ONENAND_DATARAM
)
642 return this->writesize
;
643 if (area
== ONENAND_SPARERAM
)
651 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
652 * @param mtd MTD data structure
653 * @param area BufferRAM area
654 * @param buffer the databuffer to put/get data
655 * @param offset offset to read from or write to
656 * @param count number of bytes to read/write
658 * Read the BufferRAM area
660 static int onenand_read_bufferram(struct mtd_info
*mtd
, int area
,
661 unsigned char *buffer
, int offset
, size_t count
)
663 struct onenand_chip
*this = mtd
->priv
;
664 void __iomem
*bufferram
;
666 bufferram
= this->base
+ area
;
668 bufferram
+= onenand_bufferram_offset(mtd
, area
);
670 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
673 /* Align with word(16-bit) size */
676 /* Read word and save byte */
677 word
= this->read_word(bufferram
+ offset
+ count
);
678 buffer
[count
] = (word
& 0xff);
681 memcpy(buffer
, bufferram
+ offset
, count
);
687 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
688 * @param mtd MTD data structure
689 * @param area BufferRAM area
690 * @param buffer the databuffer to put/get data
691 * @param offset offset to read from or write to
692 * @param count number of bytes to read/write
694 * Read the BufferRAM area with Sync. Burst Mode
696 static int onenand_sync_read_bufferram(struct mtd_info
*mtd
, int area
,
697 unsigned char *buffer
, int offset
, size_t count
)
699 struct onenand_chip
*this = mtd
->priv
;
700 void __iomem
*bufferram
;
702 bufferram
= this->base
+ area
;
704 bufferram
+= onenand_bufferram_offset(mtd
, area
);
706 this->mmcontrol(mtd
, ONENAND_SYS_CFG1_SYNC_READ
);
708 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
711 /* Align with word(16-bit) size */
714 /* Read word and save byte */
715 word
= this->read_word(bufferram
+ offset
+ count
);
716 buffer
[count
] = (word
& 0xff);
719 memcpy(buffer
, bufferram
+ offset
, count
);
721 this->mmcontrol(mtd
, 0);
727 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
728 * @param mtd MTD data structure
729 * @param area BufferRAM area
730 * @param buffer the databuffer to put/get data
731 * @param offset offset to read from or write to
732 * @param count number of bytes to read/write
734 * Write the BufferRAM area
736 static int onenand_write_bufferram(struct mtd_info
*mtd
, int area
,
737 const unsigned char *buffer
, int offset
, size_t count
)
739 struct onenand_chip
*this = mtd
->priv
;
740 void __iomem
*bufferram
;
742 bufferram
= this->base
+ area
;
744 bufferram
+= onenand_bufferram_offset(mtd
, area
);
746 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
750 /* Align with word(16-bit) size */
753 /* Calculate byte access offset */
754 byte_offset
= offset
+ count
;
756 /* Read word and save byte */
757 word
= this->read_word(bufferram
+ byte_offset
);
758 word
= (word
& ~0xff) | buffer
[count
];
759 this->write_word(word
, bufferram
+ byte_offset
);
762 memcpy(bufferram
+ offset
, buffer
, count
);
768 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
769 * @param mtd MTD data structure
770 * @param addr address to check
771 * @return blockpage address
773 * Get blockpage address at 2x program mode
775 static int onenand_get_2x_blockpage(struct mtd_info
*mtd
, loff_t addr
)
777 struct onenand_chip
*this = mtd
->priv
;
778 int blockpage
, block
, page
;
780 /* Calculate the even block number */
781 block
= (int) (addr
>> this->erase_shift
) & ~1;
782 /* Is it the odd plane? */
783 if (addr
& this->writesize
)
785 page
= (int) (addr
>> (this->page_shift
+ 1)) & this->page_mask
;
786 blockpage
= (block
<< 7) | page
;
792 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
793 * @param mtd MTD data structure
794 * @param addr address to check
795 * @return 1 if there are valid data, otherwise 0
797 * Check bufferram if there is data we required
799 static int onenand_check_bufferram(struct mtd_info
*mtd
, loff_t addr
)
801 struct onenand_chip
*this = mtd
->priv
;
802 int blockpage
, found
= 0;
805 if (ONENAND_IS_2PLANE(this))
806 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
808 blockpage
= (int) (addr
>> this->page_shift
);
810 /* Is there valid data? */
811 i
= ONENAND_CURRENT_BUFFERRAM(this);
812 if (this->bufferram
[i
].blockpage
== blockpage
)
815 /* Check another BufferRAM */
816 i
= ONENAND_NEXT_BUFFERRAM(this);
817 if (this->bufferram
[i
].blockpage
== blockpage
) {
818 ONENAND_SET_NEXT_BUFFERRAM(this);
823 if (found
&& ONENAND_IS_DDP(this)) {
824 /* Select DataRAM for DDP */
825 int block
= onenand_block(this, addr
);
826 int value
= onenand_bufferram_address(this, block
);
827 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
834 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
835 * @param mtd MTD data structure
836 * @param addr address to update
837 * @param valid valid flag
839 * Update BufferRAM information
841 static void onenand_update_bufferram(struct mtd_info
*mtd
, loff_t addr
,
844 struct onenand_chip
*this = mtd
->priv
;
848 if (ONENAND_IS_2PLANE(this))
849 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
851 blockpage
= (int) (addr
>> this->page_shift
);
853 /* Invalidate another BufferRAM */
854 i
= ONENAND_NEXT_BUFFERRAM(this);
855 if (this->bufferram
[i
].blockpage
== blockpage
)
856 this->bufferram
[i
].blockpage
= -1;
858 /* Update BufferRAM */
859 i
= ONENAND_CURRENT_BUFFERRAM(this);
861 this->bufferram
[i
].blockpage
= blockpage
;
863 this->bufferram
[i
].blockpage
= -1;
867 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
868 * @param mtd MTD data structure
869 * @param addr start address to invalidate
870 * @param len length to invalidate
872 * Invalidate BufferRAM information
874 static void onenand_invalidate_bufferram(struct mtd_info
*mtd
, loff_t addr
,
877 struct onenand_chip
*this = mtd
->priv
;
879 loff_t end_addr
= addr
+ len
;
881 /* Invalidate BufferRAM */
882 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
883 loff_t buf_addr
= this->bufferram
[i
].blockpage
<< this->page_shift
;
884 if (buf_addr
>= addr
&& buf_addr
< end_addr
)
885 this->bufferram
[i
].blockpage
= -1;
890 * onenand_get_device - [GENERIC] Get chip for selected access
891 * @param mtd MTD device structure
892 * @param new_state the state which is requested
894 * Get the device and lock it for exclusive access
896 static int onenand_get_device(struct mtd_info
*mtd
, int new_state
)
898 struct onenand_chip
*this = mtd
->priv
;
899 DECLARE_WAITQUEUE(wait
, current
);
902 * Grab the lock and see if the device is available
905 spin_lock(&this->chip_lock
);
906 if (this->state
== FL_READY
) {
907 this->state
= new_state
;
908 spin_unlock(&this->chip_lock
);
911 if (new_state
== FL_PM_SUSPENDED
) {
912 spin_unlock(&this->chip_lock
);
913 return (this->state
== FL_PM_SUSPENDED
) ? 0 : -EAGAIN
;
915 set_current_state(TASK_UNINTERRUPTIBLE
);
916 add_wait_queue(&this->wq
, &wait
);
917 spin_unlock(&this->chip_lock
);
919 remove_wait_queue(&this->wq
, &wait
);
926 * onenand_release_device - [GENERIC] release chip
927 * @param mtd MTD device structure
929 * Deselect, release chip lock and wake up anyone waiting on the device
931 static void onenand_release_device(struct mtd_info
*mtd
)
933 struct onenand_chip
*this = mtd
->priv
;
935 /* Release the chip */
936 spin_lock(&this->chip_lock
);
937 this->state
= FL_READY
;
939 spin_unlock(&this->chip_lock
);
943 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
944 * @param mtd MTD device structure
945 * @param buf destination address
946 * @param column oob offset to read from
947 * @param thislen oob length to read
949 static int onenand_transfer_auto_oob(struct mtd_info
*mtd
, uint8_t *buf
, int column
,
952 struct onenand_chip
*this = mtd
->priv
;
953 struct nand_oobfree
*free
;
954 int readcol
= column
;
955 int readend
= column
+ thislen
;
958 uint8_t *oob_buf
= this->oob_buf
;
960 free
= this->ecclayout
->oobfree
;
961 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
962 if (readcol
>= lastgap
)
963 readcol
+= free
->offset
- lastgap
;
964 if (readend
>= lastgap
)
965 readend
+= free
->offset
- lastgap
;
966 lastgap
= free
->offset
+ free
->length
;
968 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
969 free
= this->ecclayout
->oobfree
;
970 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
971 int free_end
= free
->offset
+ free
->length
;
972 if (free
->offset
< readend
&& free_end
> readcol
) {
973 int st
= max_t(int,free
->offset
,readcol
);
974 int ed
= min_t(int,free_end
,readend
);
976 memcpy(buf
, oob_buf
+ st
, n
);
978 } else if (column
== 0)
985 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
986 * @param mtd MTD device structure
987 * @param addr address to recover
988 * @param status return value from onenand_wait / onenand_bbt_wait
990 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
991 * lower page address and MSB page has higher page address in paired pages.
992 * If power off occurs during MSB page program, the paired LSB page data can
993 * become corrupt. LSB page recovery read is a way to read LSB page though page
994 * data are corrupted. When uncorrectable error occurs as a result of LSB page
995 * read after power up, issue LSB page recovery read.
997 static int onenand_recover_lsb(struct mtd_info
*mtd
, loff_t addr
, int status
)
999 struct onenand_chip
*this = mtd
->priv
;
1002 /* Recovery is only for Flex-OneNAND */
1003 if (!FLEXONENAND(this))
1006 /* check if we failed due to uncorrectable error */
1007 if (status
!= -EBADMSG
&& status
!= ONENAND_BBT_READ_ECC_ERROR
)
1010 /* check if address lies in MLC region */
1011 i
= flexonenand_region(mtd
, addr
);
1012 if (mtd
->eraseregions
[i
].erasesize
< (1 << this->erase_shift
))
1015 /* We are attempting to reread, so decrement stats.failed
1016 * which was incremented by onenand_wait due to read failure
1018 printk(KERN_INFO
"onenand_recover_lsb: Attempting to recover from uncorrectable read\n");
1019 mtd
->ecc_stats
.failed
--;
1021 /* Issue the LSB page recovery command */
1022 this->command(mtd
, FLEXONENAND_CMD_RECOVER_LSB
, addr
, this->writesize
);
1023 return this->wait(mtd
, FL_READING
);
1027 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1028 * @param mtd MTD device structure
1029 * @param from offset to read from
1030 * @param ops: oob operation description structure
1032 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1033 * So, read-while-load is not present.
1035 static int onenand_mlc_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
1036 struct mtd_oob_ops
*ops
)
1038 struct onenand_chip
*this = mtd
->priv
;
1039 struct mtd_ecc_stats stats
;
1040 size_t len
= ops
->len
;
1041 size_t ooblen
= ops
->ooblen
;
1042 u_char
*buf
= ops
->datbuf
;
1043 u_char
*oobbuf
= ops
->oobbuf
;
1044 int read
= 0, column
, thislen
;
1045 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
1047 int writesize
= this->writesize
;
1049 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_mlc_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
1051 if (ops
->mode
== MTD_OOB_AUTO
)
1052 oobsize
= this->ecclayout
->oobavail
;
1054 oobsize
= mtd
->oobsize
;
1056 oobcolumn
= from
& (mtd
->oobsize
- 1);
1058 /* Do not allow reads past end of device */
1059 if (from
+ len
> mtd
->size
) {
1060 printk(KERN_ERR
"onenand_mlc_read_ops_nolock: Attempt read beyond end of device\n");
1066 stats
= mtd
->ecc_stats
;
1068 while (read
< len
) {
1071 thislen
= min_t(int, writesize
, len
- read
);
1073 column
= from
& (writesize
- 1);
1074 if (column
+ thislen
> writesize
)
1075 thislen
= writesize
- column
;
1077 if (!onenand_check_bufferram(mtd
, from
)) {
1078 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1080 ret
= this->wait(mtd
, FL_READING
);
1082 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1083 onenand_update_bufferram(mtd
, from
, !ret
);
1084 if (ret
== -EBADMSG
)
1088 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
1090 thisooblen
= oobsize
- oobcolumn
;
1091 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
1093 if (ops
->mode
== MTD_OOB_AUTO
)
1094 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
1096 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
1097 oobread
+= thisooblen
;
1098 oobbuf
+= thisooblen
;
1111 * Return success, if no ECC failures, else -EBADMSG
1112 * fs driver will take care of that, because
1113 * retlen == desired len and result == -EBADMSG
1116 ops
->oobretlen
= oobread
;
1121 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1124 return mtd
->ecc_stats
.corrected
- stats
.corrected
? -EUCLEAN
: 0;
1128 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1129 * @param mtd MTD device structure
1130 * @param from offset to read from
1131 * @param ops: oob operation description structure
1133 * OneNAND read main and/or out-of-band data
1135 static int onenand_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
1136 struct mtd_oob_ops
*ops
)
1138 struct onenand_chip
*this = mtd
->priv
;
1139 struct mtd_ecc_stats stats
;
1140 size_t len
= ops
->len
;
1141 size_t ooblen
= ops
->ooblen
;
1142 u_char
*buf
= ops
->datbuf
;
1143 u_char
*oobbuf
= ops
->oobbuf
;
1144 int read
= 0, column
, thislen
;
1145 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
1146 int ret
= 0, boundary
= 0;
1147 int writesize
= this->writesize
;
1149 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
1151 if (ops
->mode
== MTD_OOB_AUTO
)
1152 oobsize
= this->ecclayout
->oobavail
;
1154 oobsize
= mtd
->oobsize
;
1156 oobcolumn
= from
& (mtd
->oobsize
- 1);
1158 /* Do not allow reads past end of device */
1159 if ((from
+ len
) > mtd
->size
) {
1160 printk(KERN_ERR
"onenand_read_ops_nolock: Attempt read beyond end of device\n");
1166 stats
= mtd
->ecc_stats
;
1168 /* Read-while-load method */
1170 /* Do first load to bufferRAM */
1172 if (!onenand_check_bufferram(mtd
, from
)) {
1173 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1174 ret
= this->wait(mtd
, FL_READING
);
1175 onenand_update_bufferram(mtd
, from
, !ret
);
1176 if (ret
== -EBADMSG
)
1181 thislen
= min_t(int, writesize
, len
- read
);
1182 column
= from
& (writesize
- 1);
1183 if (column
+ thislen
> writesize
)
1184 thislen
= writesize
- column
;
1187 /* If there is more to load then start next load */
1189 if (read
+ thislen
< len
) {
1190 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1192 * Chip boundary handling in DDP
1193 * Now we issued chip 1 read and pointed chip 1
1194 * bufferram so we have to point chip 0 bufferram.
1196 if (ONENAND_IS_DDP(this) &&
1197 unlikely(from
== (this->chipsize
>> 1))) {
1198 this->write_word(ONENAND_DDP_CHIP0
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1202 ONENAND_SET_PREV_BUFFERRAM(this);
1204 /* While load is going, read from last bufferRAM */
1205 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
1207 /* Read oob area if needed */
1209 thisooblen
= oobsize
- oobcolumn
;
1210 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
1212 if (ops
->mode
== MTD_OOB_AUTO
)
1213 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
1215 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
1216 oobread
+= thisooblen
;
1217 oobbuf
+= thisooblen
;
1221 /* See if we are done */
1225 /* Set up for next read from bufferRAM */
1226 if (unlikely(boundary
))
1227 this->write_word(ONENAND_DDP_CHIP1
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1228 ONENAND_SET_NEXT_BUFFERRAM(this);
1230 thislen
= min_t(int, writesize
, len
- read
);
1233 /* Now wait for load */
1234 ret
= this->wait(mtd
, FL_READING
);
1235 onenand_update_bufferram(mtd
, from
, !ret
);
1236 if (ret
== -EBADMSG
)
1241 * Return success, if no ECC failures, else -EBADMSG
1242 * fs driver will take care of that, because
1243 * retlen == desired len and result == -EBADMSG
1246 ops
->oobretlen
= oobread
;
1251 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1254 return mtd
->ecc_stats
.corrected
- stats
.corrected
? -EUCLEAN
: 0;
1258 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1259 * @param mtd MTD device structure
1260 * @param from offset to read from
1261 * @param ops: oob operation description structure
1263 * OneNAND read out-of-band data from the spare area
1265 static int onenand_read_oob_nolock(struct mtd_info
*mtd
, loff_t from
,
1266 struct mtd_oob_ops
*ops
)
1268 struct onenand_chip
*this = mtd
->priv
;
1269 struct mtd_ecc_stats stats
;
1270 int read
= 0, thislen
, column
, oobsize
;
1271 size_t len
= ops
->ooblen
;
1272 mtd_oob_mode_t mode
= ops
->mode
;
1273 u_char
*buf
= ops
->oobbuf
;
1274 int ret
= 0, readcmd
;
1276 from
+= ops
->ooboffs
;
1278 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
1280 /* Initialize return length value */
1283 if (mode
== MTD_OOB_AUTO
)
1284 oobsize
= this->ecclayout
->oobavail
;
1286 oobsize
= mtd
->oobsize
;
1288 column
= from
& (mtd
->oobsize
- 1);
1290 if (unlikely(column
>= oobsize
)) {
1291 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to start read outside oob\n");
1295 /* Do not allow reads past end of device */
1296 if (unlikely(from
>= mtd
->size
||
1297 column
+ len
> ((mtd
->size
>> this->page_shift
) -
1298 (from
>> this->page_shift
)) * oobsize
)) {
1299 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1303 stats
= mtd
->ecc_stats
;
1305 readcmd
= ONENAND_IS_MLC(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1307 while (read
< len
) {
1310 thislen
= oobsize
- column
;
1311 thislen
= min_t(int, thislen
, len
);
1313 this->command(mtd
, readcmd
, from
, mtd
->oobsize
);
1315 onenand_update_bufferram(mtd
, from
, 0);
1317 ret
= this->wait(mtd
, FL_READING
);
1319 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1321 if (ret
&& ret
!= -EBADMSG
) {
1322 printk(KERN_ERR
"onenand_read_oob_nolock: read failed = 0x%x\n", ret
);
1326 if (mode
== MTD_OOB_AUTO
)
1327 onenand_transfer_auto_oob(mtd
, buf
, column
, thislen
);
1329 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
1341 from
+= mtd
->writesize
;
1346 ops
->oobretlen
= read
;
1351 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1358 * onenand_read - [MTD Interface] Read data from flash
1359 * @param mtd MTD device structure
1360 * @param from offset to read from
1361 * @param len number of bytes to read
1362 * @param retlen pointer to variable to store the number of read bytes
1363 * @param buf the databuffer to put data
1367 static int onenand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1368 size_t *retlen
, u_char
*buf
)
1370 struct onenand_chip
*this = mtd
->priv
;
1371 struct mtd_oob_ops ops
= {
1379 onenand_get_device(mtd
, FL_READING
);
1380 ret
= ONENAND_IS_MLC(this) ?
1381 onenand_mlc_read_ops_nolock(mtd
, from
, &ops
) :
1382 onenand_read_ops_nolock(mtd
, from
, &ops
);
1383 onenand_release_device(mtd
);
1385 *retlen
= ops
.retlen
;
1390 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1391 * @param mtd: MTD device structure
1392 * @param from: offset to read from
1393 * @param ops: oob operation description structure
1395 * Read main and/or out-of-band
1397 static int onenand_read_oob(struct mtd_info
*mtd
, loff_t from
,
1398 struct mtd_oob_ops
*ops
)
1400 struct onenand_chip
*this = mtd
->priv
;
1403 switch (ops
->mode
) {
1408 /* Not implemented yet */
1413 onenand_get_device(mtd
, FL_READING
);
1415 ret
= ONENAND_IS_MLC(this) ?
1416 onenand_mlc_read_ops_nolock(mtd
, from
, ops
) :
1417 onenand_read_ops_nolock(mtd
, from
, ops
);
1419 ret
= onenand_read_oob_nolock(mtd
, from
, ops
);
1420 onenand_release_device(mtd
);
1426 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1427 * @param mtd MTD device structure
1428 * @param state state to select the max. timeout value
1430 * Wait for command done.
1432 static int onenand_bbt_wait(struct mtd_info
*mtd
, int state
)
1434 struct onenand_chip
*this = mtd
->priv
;
1435 unsigned long timeout
;
1436 unsigned int interrupt
;
1439 /* The 20 msec is enough */
1440 timeout
= jiffies
+ msecs_to_jiffies(20);
1441 while (time_before(jiffies
, timeout
)) {
1442 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1443 if (interrupt
& ONENAND_INT_MASTER
)
1446 /* To get correct interrupt status in timeout case */
1447 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1448 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
1450 if (interrupt
& ONENAND_INT_READ
) {
1451 int ecc
= onenand_read_ecc(this);
1452 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
1453 printk(KERN_INFO
"onenand_bbt_wait: ecc error = 0x%04x"
1454 ", controller error 0x%04x\n", ecc
, ctrl
);
1455 return ONENAND_BBT_READ_ECC_ERROR
;
1458 printk(KERN_ERR
"onenand_bbt_wait: read timeout!"
1459 "ctrl=0x%04x intr=0x%04x\n", ctrl
, interrupt
);
1460 return ONENAND_BBT_READ_FATAL_ERROR
;
1463 /* Initial bad block case: 0x2400 or 0x0400 */
1464 if (ctrl
& ONENAND_CTRL_ERROR
) {
1465 printk(KERN_DEBUG
"onenand_bbt_wait: "
1466 "controller error = 0x%04x\n", ctrl
);
1467 return ONENAND_BBT_READ_ERROR
;
1474 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1475 * @param mtd MTD device structure
1476 * @param from offset to read from
1477 * @param ops oob operation description structure
1479 * OneNAND read out-of-band data from the spare area for bbt scan
1481 int onenand_bbt_read_oob(struct mtd_info
*mtd
, loff_t from
,
1482 struct mtd_oob_ops
*ops
)
1484 struct onenand_chip
*this = mtd
->priv
;
1485 int read
= 0, thislen
, column
;
1486 int ret
= 0, readcmd
;
1487 size_t len
= ops
->ooblen
;
1488 u_char
*buf
= ops
->oobbuf
;
1490 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from
, len
);
1492 /* Initialize return value */
1495 /* Do not allow reads past end of device */
1496 if (unlikely((from
+ len
) > mtd
->size
)) {
1497 printk(KERN_ERR
"onenand_bbt_read_oob: Attempt read beyond end of device\n");
1498 return ONENAND_BBT_READ_FATAL_ERROR
;
1501 /* Grab the lock and see if the device is available */
1502 onenand_get_device(mtd
, FL_READING
);
1504 column
= from
& (mtd
->oobsize
- 1);
1506 readcmd
= ONENAND_IS_MLC(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1508 while (read
< len
) {
1511 thislen
= mtd
->oobsize
- column
;
1512 thislen
= min_t(int, thislen
, len
);
1514 this->command(mtd
, readcmd
, from
, mtd
->oobsize
);
1516 onenand_update_bufferram(mtd
, from
, 0);
1518 ret
= this->bbt_wait(mtd
, FL_READING
);
1520 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1525 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
1534 /* Update Page size */
1535 from
+= this->writesize
;
1540 /* Deselect and wake up anyone waiting on the device */
1541 onenand_release_device(mtd
);
1543 ops
->oobretlen
= read
;
1547 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1549 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1550 * @param mtd MTD device structure
1551 * @param buf the databuffer to verify
1552 * @param to offset to read from
1554 static int onenand_verify_oob(struct mtd_info
*mtd
, const u_char
*buf
, loff_t to
)
1556 struct onenand_chip
*this = mtd
->priv
;
1557 u_char
*oob_buf
= this->oob_buf
;
1558 int status
, i
, readcmd
;
1560 readcmd
= ONENAND_IS_MLC(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1562 this->command(mtd
, readcmd
, to
, mtd
->oobsize
);
1563 onenand_update_bufferram(mtd
, to
, 0);
1564 status
= this->wait(mtd
, FL_READING
);
1568 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
1569 for (i
= 0; i
< mtd
->oobsize
; i
++)
1570 if (buf
[i
] != 0xFF && buf
[i
] != oob_buf
[i
])
1577 * onenand_verify - [GENERIC] verify the chip contents after a write
1578 * @param mtd MTD device structure
1579 * @param buf the databuffer to verify
1580 * @param addr offset to read from
1581 * @param len number of bytes to read and compare
1583 static int onenand_verify(struct mtd_info
*mtd
, const u_char
*buf
, loff_t addr
, size_t len
)
1585 struct onenand_chip
*this = mtd
->priv
;
1586 void __iomem
*dataram
;
1588 int thislen
, column
;
1591 thislen
= min_t(int, this->writesize
, len
);
1592 column
= addr
& (this->writesize
- 1);
1593 if (column
+ thislen
> this->writesize
)
1594 thislen
= this->writesize
- column
;
1596 this->command(mtd
, ONENAND_CMD_READ
, addr
, this->writesize
);
1598 onenand_update_bufferram(mtd
, addr
, 0);
1600 ret
= this->wait(mtd
, FL_READING
);
1604 onenand_update_bufferram(mtd
, addr
, 1);
1606 dataram
= this->base
+ ONENAND_DATARAM
;
1607 dataram
+= onenand_bufferram_offset(mtd
, ONENAND_DATARAM
);
1609 if (memcmp(buf
, dataram
+ column
, thislen
))
1620 #define onenand_verify(...) (0)
1621 #define onenand_verify_oob(...) (0)
1624 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1626 static void onenand_panic_wait(struct mtd_info
*mtd
)
1628 struct onenand_chip
*this = mtd
->priv
;
1629 unsigned int interrupt
;
1632 for (i
= 0; i
< 2000; i
++) {
1633 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1634 if (interrupt
& ONENAND_INT_MASTER
)
1641 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1642 * @param mtd MTD device structure
1643 * @param to offset to write to
1644 * @param len number of bytes to write
1645 * @param retlen pointer to variable to store the number of written bytes
1646 * @param buf the data to write
1650 static int onenand_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1651 size_t *retlen
, const u_char
*buf
)
1653 struct onenand_chip
*this = mtd
->priv
;
1654 int column
, subpage
;
1658 if (this->state
== FL_PM_SUSPENDED
)
1661 /* Wait for any existing operation to clear */
1662 onenand_panic_wait(mtd
);
1664 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_panic_write: to = 0x%08x, len = %i\n",
1665 (unsigned int) to
, (int) len
);
1667 /* Initialize retlen, in case of early exit */
1670 /* Do not allow writes past end of device */
1671 if (unlikely((to
+ len
) > mtd
->size
)) {
1672 printk(KERN_ERR
"onenand_panic_write: Attempt write to past end of device\n");
1676 /* Reject writes, which are not page aligned */
1677 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1678 printk(KERN_ERR
"onenand_panic_write: Attempt to write not page aligned data\n");
1682 column
= to
& (mtd
->writesize
- 1);
1684 /* Loop until all data write */
1685 while (written
< len
) {
1686 int thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1687 u_char
*wbuf
= (u_char
*) buf
;
1689 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1691 /* Partial page write */
1692 subpage
= thislen
< mtd
->writesize
;
1694 memset(this->page_buf
, 0xff, mtd
->writesize
);
1695 memcpy(this->page_buf
+ column
, buf
, thislen
);
1696 wbuf
= this->page_buf
;
1699 this->write_bufferram(mtd
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1700 this->write_bufferram(mtd
, ONENAND_SPARERAM
, ffchars
, 0, mtd
->oobsize
);
1702 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->writesize
);
1704 onenand_panic_wait(mtd
);
1706 /* In partial page write we don't update bufferram */
1707 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
1708 if (ONENAND_IS_2PLANE(this)) {
1709 ONENAND_SET_BUFFERRAM1(this);
1710 onenand_update_bufferram(mtd
, to
+ this->writesize
, !ret
&& !subpage
);
1714 printk(KERN_ERR
"onenand_panic_write: write failed %d\n", ret
);
1733 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1734 * @param mtd MTD device structure
1735 * @param oob_buf oob buffer
1736 * @param buf source address
1737 * @param column oob offset to write to
1738 * @param thislen oob length to write
1740 static int onenand_fill_auto_oob(struct mtd_info
*mtd
, u_char
*oob_buf
,
1741 const u_char
*buf
, int column
, int thislen
)
1743 struct onenand_chip
*this = mtd
->priv
;
1744 struct nand_oobfree
*free
;
1745 int writecol
= column
;
1746 int writeend
= column
+ thislen
;
1750 free
= this->ecclayout
->oobfree
;
1751 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1752 if (writecol
>= lastgap
)
1753 writecol
+= free
->offset
- lastgap
;
1754 if (writeend
>= lastgap
)
1755 writeend
+= free
->offset
- lastgap
;
1756 lastgap
= free
->offset
+ free
->length
;
1758 free
= this->ecclayout
->oobfree
;
1759 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1760 int free_end
= free
->offset
+ free
->length
;
1761 if (free
->offset
< writeend
&& free_end
> writecol
) {
1762 int st
= max_t(int,free
->offset
,writecol
);
1763 int ed
= min_t(int,free_end
,writeend
);
1765 memcpy(oob_buf
+ st
, buf
, n
);
1767 } else if (column
== 0)
1774 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1775 * @param mtd MTD device structure
1776 * @param to offset to write to
1777 * @param ops oob operation description structure
1779 * Write main and/or oob with ECC
1781 static int onenand_write_ops_nolock(struct mtd_info
*mtd
, loff_t to
,
1782 struct mtd_oob_ops
*ops
)
1784 struct onenand_chip
*this = mtd
->priv
;
1785 int written
= 0, column
, thislen
= 0, subpage
= 0;
1786 int prev
= 0, prevlen
= 0, prev_subpage
= 0, first
= 1;
1787 int oobwritten
= 0, oobcolumn
, thisooblen
, oobsize
;
1788 size_t len
= ops
->len
;
1789 size_t ooblen
= ops
->ooblen
;
1790 const u_char
*buf
= ops
->datbuf
;
1791 const u_char
*oob
= ops
->oobbuf
;
1795 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1797 /* Initialize retlen, in case of early exit */
1801 /* Do not allow writes past end of device */
1802 if (unlikely((to
+ len
) > mtd
->size
)) {
1803 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt write to past end of device\n");
1807 /* Reject writes, which are not page aligned */
1808 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1809 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1813 /* Check zero length */
1817 if (ops
->mode
== MTD_OOB_AUTO
)
1818 oobsize
= this->ecclayout
->oobavail
;
1820 oobsize
= mtd
->oobsize
;
1822 oobcolumn
= to
& (mtd
->oobsize
- 1);
1824 column
= to
& (mtd
->writesize
- 1);
1826 /* Loop until all data write */
1828 if (written
< len
) {
1829 u_char
*wbuf
= (u_char
*) buf
;
1831 thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1832 thisooblen
= min_t(int, oobsize
- oobcolumn
, ooblen
- oobwritten
);
1836 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1838 /* Partial page write */
1839 subpage
= thislen
< mtd
->writesize
;
1841 memset(this->page_buf
, 0xff, mtd
->writesize
);
1842 memcpy(this->page_buf
+ column
, buf
, thislen
);
1843 wbuf
= this->page_buf
;
1846 this->write_bufferram(mtd
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1849 oobbuf
= this->oob_buf
;
1851 /* We send data to spare ram with oobsize
1852 * to prevent byte access */
1853 memset(oobbuf
, 0xff, mtd
->oobsize
);
1854 if (ops
->mode
== MTD_OOB_AUTO
)
1855 onenand_fill_auto_oob(mtd
, oobbuf
, oob
, oobcolumn
, thisooblen
);
1857 memcpy(oobbuf
+ oobcolumn
, oob
, thisooblen
);
1859 oobwritten
+= thisooblen
;
1863 oobbuf
= (u_char
*) ffchars
;
1865 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1867 ONENAND_SET_NEXT_BUFFERRAM(this);
1870 * 2 PLANE, MLC, and Flex-OneNAND do not support
1871 * write-while-program feature.
1873 if (!ONENAND_IS_2PLANE(this) && !first
) {
1874 ONENAND_SET_PREV_BUFFERRAM(this);
1876 ret
= this->wait(mtd
, FL_WRITING
);
1878 /* In partial page write we don't update bufferram */
1879 onenand_update_bufferram(mtd
, prev
, !ret
&& !prev_subpage
);
1882 printk(KERN_ERR
"onenand_write_ops_nolock: write failed %d\n", ret
);
1886 if (written
== len
) {
1887 /* Only check verify write turn on */
1888 ret
= onenand_verify(mtd
, buf
- len
, to
- len
, len
);
1890 printk(KERN_ERR
"onenand_write_ops_nolock: verify failed %d\n", ret
);
1894 ONENAND_SET_NEXT_BUFFERRAM(this);
1897 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->writesize
);
1900 * 2 PLANE, MLC, and Flex-OneNAND wait here
1902 if (ONENAND_IS_2PLANE(this)) {
1903 ret
= this->wait(mtd
, FL_WRITING
);
1905 /* In partial page write we don't update bufferram */
1906 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
1908 printk(KERN_ERR
"onenand_write_ops_nolock: write failed %d\n", ret
);
1912 /* Only check verify write turn on */
1913 ret
= onenand_verify(mtd
, buf
, to
, thislen
);
1915 printk(KERN_ERR
"onenand_write_ops_nolock: verify failed %d\n", ret
);
1928 prev_subpage
= subpage
;
1936 /* In error case, clear all bufferrams */
1938 onenand_invalidate_bufferram(mtd
, 0, -1);
1940 ops
->retlen
= written
;
1941 ops
->oobretlen
= oobwritten
;
1948 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1949 * @param mtd MTD device structure
1950 * @param to offset to write to
1951 * @param len number of bytes to write
1952 * @param retlen pointer to variable to store the number of written bytes
1953 * @param buf the data to write
1954 * @param mode operation mode
1956 * OneNAND write out-of-band
1958 static int onenand_write_oob_nolock(struct mtd_info
*mtd
, loff_t to
,
1959 struct mtd_oob_ops
*ops
)
1961 struct onenand_chip
*this = mtd
->priv
;
1962 int column
, ret
= 0, oobsize
;
1963 int written
= 0, oobcmd
;
1965 size_t len
= ops
->ooblen
;
1966 const u_char
*buf
= ops
->oobbuf
;
1967 mtd_oob_mode_t mode
= ops
->mode
;
1971 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1973 /* Initialize retlen, in case of early exit */
1976 if (mode
== MTD_OOB_AUTO
)
1977 oobsize
= this->ecclayout
->oobavail
;
1979 oobsize
= mtd
->oobsize
;
1981 column
= to
& (mtd
->oobsize
- 1);
1983 if (unlikely(column
>= oobsize
)) {
1984 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to start write outside oob\n");
1988 /* For compatibility with NAND: Do not allow write past end of page */
1989 if (unlikely(column
+ len
> oobsize
)) {
1990 printk(KERN_ERR
"onenand_write_oob_nolock: "
1991 "Attempt to write past end of page\n");
1995 /* Do not allow reads past end of device */
1996 if (unlikely(to
>= mtd
->size
||
1997 column
+ len
> ((mtd
->size
>> this->page_shift
) -
1998 (to
>> this->page_shift
)) * oobsize
)) {
1999 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to write past end of device\n");
2003 oobbuf
= this->oob_buf
;
2005 oobcmd
= ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG
: ONENAND_CMD_PROGOOB
;
2007 /* Loop until all data write */
2008 while (written
< len
) {
2009 int thislen
= min_t(int, oobsize
, len
- written
);
2013 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobsize
);
2015 /* We send data to spare ram with oobsize
2016 * to prevent byte access */
2017 memset(oobbuf
, 0xff, mtd
->oobsize
);
2018 if (mode
== MTD_OOB_AUTO
)
2019 onenand_fill_auto_oob(mtd
, oobbuf
, buf
, column
, thislen
);
2021 memcpy(oobbuf
+ column
, buf
, thislen
);
2022 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
2024 if (ONENAND_IS_MLC(this)) {
2025 /* Set main area of DataRAM to 0xff*/
2026 memset(this->page_buf
, 0xff, mtd
->writesize
);
2027 this->write_bufferram(mtd
, ONENAND_DATARAM
,
2028 this->page_buf
, 0, mtd
->writesize
);
2031 this->command(mtd
, oobcmd
, to
, mtd
->oobsize
);
2033 onenand_update_bufferram(mtd
, to
, 0);
2034 if (ONENAND_IS_2PLANE(this)) {
2035 ONENAND_SET_BUFFERRAM1(this);
2036 onenand_update_bufferram(mtd
, to
+ this->writesize
, 0);
2039 ret
= this->wait(mtd
, FL_WRITING
);
2041 printk(KERN_ERR
"onenand_write_oob_nolock: write failed %d\n", ret
);
2045 ret
= onenand_verify_oob(mtd
, oobbuf
, to
);
2047 printk(KERN_ERR
"onenand_write_oob_nolock: verify failed %d\n", ret
);
2055 to
+= mtd
->writesize
;
2060 ops
->oobretlen
= written
;
2066 * onenand_write - [MTD Interface] write buffer to FLASH
2067 * @param mtd MTD device structure
2068 * @param to offset to write to
2069 * @param len number of bytes to write
2070 * @param retlen pointer to variable to store the number of written bytes
2071 * @param buf the data to write
2075 static int onenand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2076 size_t *retlen
, const u_char
*buf
)
2078 struct mtd_oob_ops ops
= {
2081 .datbuf
= (u_char
*) buf
,
2086 onenand_get_device(mtd
, FL_WRITING
);
2087 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
2088 onenand_release_device(mtd
);
2090 *retlen
= ops
.retlen
;
2095 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2096 * @param mtd: MTD device structure
2097 * @param to: offset to write
2098 * @param ops: oob operation description structure
2100 static int onenand_write_oob(struct mtd_info
*mtd
, loff_t to
,
2101 struct mtd_oob_ops
*ops
)
2105 switch (ops
->mode
) {
2110 /* Not implemented yet */
2115 onenand_get_device(mtd
, FL_WRITING
);
2117 ret
= onenand_write_ops_nolock(mtd
, to
, ops
);
2119 ret
= onenand_write_oob_nolock(mtd
, to
, ops
);
2120 onenand_release_device(mtd
);
2126 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2127 * @param mtd MTD device structure
2128 * @param ofs offset from device start
2129 * @param allowbbt 1, if its allowed to access the bbt area
2131 * Check, if the block is bad. Either by reading the bad block table or
2132 * calling of the scan function.
2134 static int onenand_block_isbad_nolock(struct mtd_info
*mtd
, loff_t ofs
, int allowbbt
)
2136 struct onenand_chip
*this = mtd
->priv
;
2137 struct bbm_info
*bbm
= this->bbm
;
2139 /* Return info from the table */
2140 return bbm
->isbad_bbt(mtd
, ofs
, allowbbt
);
2144 * onenand_erase - [MTD Interface] erase block(s)
2145 * @param mtd MTD device structure
2146 * @param instr erase instruction
2148 * Erase one ore more blocks
2150 static int onenand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
2152 struct onenand_chip
*this = mtd
->priv
;
2153 unsigned int block_size
;
2154 loff_t addr
= instr
->addr
;
2155 loff_t len
= instr
->len
;
2157 struct mtd_erase_region_info
*region
= NULL
;
2158 loff_t region_end
= 0;
2160 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_erase: start = 0x%012llx, len = %llu\n", (unsigned long long) instr
->addr
, (unsigned long long) instr
->len
);
2162 /* Do not allow erase past end of device */
2163 if (unlikely((len
+ addr
) > mtd
->size
)) {
2164 printk(KERN_ERR
"onenand_erase: Erase past end of device\n");
2168 if (FLEXONENAND(this)) {
2169 /* Find the eraseregion of this address */
2170 i
= flexonenand_region(mtd
, addr
);
2171 region
= &mtd
->eraseregions
[i
];
2173 block_size
= region
->erasesize
;
2174 region_end
= region
->offset
+ region
->erasesize
* region
->numblocks
;
2176 /* Start address within region must align on block boundary.
2177 * Erase region's start offset is always block start address.
2179 if (unlikely((addr
- region
->offset
) & (block_size
- 1))) {
2180 printk(KERN_ERR
"onenand_erase: Unaligned address\n");
2184 block_size
= 1 << this->erase_shift
;
2186 /* Start address must align on block boundary */
2187 if (unlikely(addr
& (block_size
- 1))) {
2188 printk(KERN_ERR
"onenand_erase: Unaligned address\n");
2193 /* Length must align on block boundary */
2194 if (unlikely(len
& (block_size
- 1))) {
2195 printk(KERN_ERR
"onenand_erase: Length not block aligned\n");
2199 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
2201 /* Grab the lock and see if the device is available */
2202 onenand_get_device(mtd
, FL_ERASING
);
2204 /* Loop through the blocks */
2205 instr
->state
= MTD_ERASING
;
2210 /* Check if we have a bad block, we do not erase bad blocks */
2211 if (onenand_block_isbad_nolock(mtd
, addr
, 0)) {
2212 printk (KERN_WARNING
"onenand_erase: attempt to erase a bad block at addr 0x%012llx\n", (unsigned long long) addr
);
2213 instr
->state
= MTD_ERASE_FAILED
;
2217 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
2219 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
2221 ret
= this->wait(mtd
, FL_ERASING
);
2222 /* Check, if it is write protected */
2224 printk(KERN_ERR
"onenand_erase: Failed erase, block %d\n",
2225 onenand_block(this, addr
));
2226 instr
->state
= MTD_ERASE_FAILED
;
2227 instr
->fail_addr
= addr
;
2234 if (addr
== region_end
) {
2239 block_size
= region
->erasesize
;
2240 region_end
= region
->offset
+ region
->erasesize
* region
->numblocks
;
2242 if (len
& (block_size
- 1)) {
2243 /* FIXME: This should be handled at MTD partitioning level. */
2244 printk(KERN_ERR
"onenand_erase: Unaligned address\n");
2251 instr
->state
= MTD_ERASE_DONE
;
2255 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
2257 /* Deselect and wake up anyone waiting on the device */
2258 onenand_release_device(mtd
);
2260 /* Do call back function */
2262 mtd_erase_callback(instr
);
2268 * onenand_sync - [MTD Interface] sync
2269 * @param mtd MTD device structure
2271 * Sync is actually a wait for chip ready function
2273 static void onenand_sync(struct mtd_info
*mtd
)
2275 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_sync: called\n");
2277 /* Grab the lock and see if the device is available */
2278 onenand_get_device(mtd
, FL_SYNCING
);
2280 /* Release it and go back */
2281 onenand_release_device(mtd
);
2285 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2286 * @param mtd MTD device structure
2287 * @param ofs offset relative to mtd start
2289 * Check whether the block is bad
2291 static int onenand_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
2295 /* Check for invalid offset */
2296 if (ofs
> mtd
->size
)
2299 onenand_get_device(mtd
, FL_READING
);
2300 ret
= onenand_block_isbad_nolock(mtd
, ofs
, 0);
2301 onenand_release_device(mtd
);
2306 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2307 * @param mtd MTD device structure
2308 * @param ofs offset from device start
2310 * This is the default implementation, which can be overridden by
2311 * a hardware specific driver.
2313 static int onenand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
2315 struct onenand_chip
*this = mtd
->priv
;
2316 struct bbm_info
*bbm
= this->bbm
;
2317 u_char buf
[2] = {0, 0};
2318 struct mtd_oob_ops ops
= {
2319 .mode
= MTD_OOB_PLACE
,
2326 /* Get block number */
2327 block
= onenand_block(this, ofs
);
2329 bbm
->bbt
[block
>> 2] |= 0x01 << ((block
& 0x03) << 1);
2331 /* We write two bytes, so we don't have to mess with 16-bit access */
2332 ofs
+= mtd
->oobsize
+ (bbm
->badblockpos
& ~0x01);
2333 /* FIXME : What to do when marking SLC block in partition
2334 * with MLC erasesize? For now, it is not advisable to
2335 * create partitions containing both SLC and MLC regions.
2337 return onenand_write_oob_nolock(mtd
, ofs
, &ops
);
2341 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2342 * @param mtd MTD device structure
2343 * @param ofs offset relative to mtd start
2345 * Mark the block as bad
2347 static int onenand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
2349 struct onenand_chip
*this = mtd
->priv
;
2352 ret
= onenand_block_isbad(mtd
, ofs
);
2354 /* If it was bad already, return success and do nothing */
2360 onenand_get_device(mtd
, FL_WRITING
);
2361 ret
= this->block_markbad(mtd
, ofs
);
2362 onenand_release_device(mtd
);
2367 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2368 * @param mtd MTD device structure
2369 * @param ofs offset relative to mtd start
2370 * @param len number of bytes to lock or unlock
2371 * @param cmd lock or unlock command
2373 * Lock or unlock one or more blocks
2375 static int onenand_do_lock_cmd(struct mtd_info
*mtd
, loff_t ofs
, size_t len
, int cmd
)
2377 struct onenand_chip
*this = mtd
->priv
;
2378 int start
, end
, block
, value
, status
;
2381 start
= onenand_block(this, ofs
);
2382 end
= onenand_block(this, ofs
+ len
) - 1;
2384 if (cmd
== ONENAND_CMD_LOCK
)
2385 wp_status_mask
= ONENAND_WP_LS
;
2387 wp_status_mask
= ONENAND_WP_US
;
2389 /* Continuous lock scheme */
2390 if (this->options
& ONENAND_HAS_CONT_LOCK
) {
2391 /* Set start block address */
2392 this->write_word(start
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2393 /* Set end block address */
2394 this->write_word(end
, this->base
+ ONENAND_REG_END_BLOCK_ADDRESS
);
2395 /* Write lock command */
2396 this->command(mtd
, cmd
, 0, 0);
2398 /* There's no return value */
2399 this->wait(mtd
, FL_LOCKING
);
2402 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2403 & ONENAND_CTRL_ONGO
)
2406 /* Check lock status */
2407 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2408 if (!(status
& wp_status_mask
))
2409 printk(KERN_ERR
"wp status = 0x%x\n", status
);
2414 /* Block lock scheme */
2415 for (block
= start
; block
< end
+ 1; block
++) {
2416 /* Set block address */
2417 value
= onenand_block_address(this, block
);
2418 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
2419 /* Select DataRAM for DDP */
2420 value
= onenand_bufferram_address(this, block
);
2421 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
2422 /* Set start block address */
2423 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2424 /* Write lock command */
2425 this->command(mtd
, cmd
, 0, 0);
2427 /* There's no return value */
2428 this->wait(mtd
, FL_LOCKING
);
2431 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2432 & ONENAND_CTRL_ONGO
)
2435 /* Check lock status */
2436 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2437 if (!(status
& wp_status_mask
))
2438 printk(KERN_ERR
"block = %d, wp status = 0x%x\n", block
, status
);
2445 * onenand_lock - [MTD Interface] Lock block(s)
2446 * @param mtd MTD device structure
2447 * @param ofs offset relative to mtd start
2448 * @param len number of bytes to unlock
2450 * Lock one or more blocks
2452 static int onenand_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2456 onenand_get_device(mtd
, FL_LOCKING
);
2457 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_LOCK
);
2458 onenand_release_device(mtd
);
2463 * onenand_unlock - [MTD Interface] Unlock block(s)
2464 * @param mtd MTD device structure
2465 * @param ofs offset relative to mtd start
2466 * @param len number of bytes to unlock
2468 * Unlock one or more blocks
2470 static int onenand_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2474 onenand_get_device(mtd
, FL_LOCKING
);
2475 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
2476 onenand_release_device(mtd
);
2481 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2482 * @param this onenand chip data structure
2486 static int onenand_check_lock_status(struct onenand_chip
*this)
2488 unsigned int value
, block
, status
;
2491 end
= this->chipsize
>> this->erase_shift
;
2492 for (block
= 0; block
< end
; block
++) {
2493 /* Set block address */
2494 value
= onenand_block_address(this, block
);
2495 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
2496 /* Select DataRAM for DDP */
2497 value
= onenand_bufferram_address(this, block
);
2498 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
2499 /* Set start block address */
2500 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2502 /* Check lock status */
2503 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2504 if (!(status
& ONENAND_WP_US
)) {
2505 printk(KERN_ERR
"block = %d, wp status = 0x%x\n", block
, status
);
2514 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2515 * @param mtd MTD device structure
2519 static void onenand_unlock_all(struct mtd_info
*mtd
)
2521 struct onenand_chip
*this = mtd
->priv
;
2523 loff_t len
= mtd
->size
;
2525 if (this->options
& ONENAND_HAS_UNLOCK_ALL
) {
2526 /* Set start block address */
2527 this->write_word(0, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2528 /* Write unlock command */
2529 this->command(mtd
, ONENAND_CMD_UNLOCK_ALL
, 0, 0);
2531 /* There's no return value */
2532 this->wait(mtd
, FL_LOCKING
);
2535 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2536 & ONENAND_CTRL_ONGO
)
2539 /* Don't check lock status */
2540 if (this->options
& ONENAND_SKIP_UNLOCK_CHECK
)
2543 /* Check lock status */
2544 if (onenand_check_lock_status(this))
2547 /* Workaround for all block unlock in DDP */
2548 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2549 /* All blocks on another chip */
2550 ofs
= this->chipsize
>> 1;
2551 len
= this->chipsize
>> 1;
2555 onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
2558 #ifdef CONFIG_MTD_ONENAND_OTP
2560 /* Internal OTP operation */
2561 typedef int (*otp_op_t
)(struct mtd_info
*mtd
, loff_t form
, size_t len
,
2562 size_t *retlen
, u_char
*buf
);
2565 * do_otp_read - [DEFAULT] Read OTP block area
2566 * @param mtd MTD device structure
2567 * @param from The offset to read
2568 * @param len number of bytes to read
2569 * @param retlen pointer to variable to store the number of readbytes
2570 * @param buf the databuffer to put/get data
2572 * Read OTP block area.
2574 static int do_otp_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2575 size_t *retlen
, u_char
*buf
)
2577 struct onenand_chip
*this = mtd
->priv
;
2578 struct mtd_oob_ops ops
= {
2586 /* Enter OTP access mode */
2587 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
2588 this->wait(mtd
, FL_OTPING
);
2590 ret
= ONENAND_IS_MLC(this) ?
2591 onenand_mlc_read_ops_nolock(mtd
, from
, &ops
) :
2592 onenand_read_ops_nolock(mtd
, from
, &ops
);
2594 /* Exit OTP access mode */
2595 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
2596 this->wait(mtd
, FL_RESETING
);
2602 * do_otp_write - [DEFAULT] Write OTP block area
2603 * @param mtd MTD device structure
2604 * @param to The offset to write
2605 * @param len number of bytes to write
2606 * @param retlen pointer to variable to store the number of write bytes
2607 * @param buf the databuffer to put/get data
2609 * Write OTP block area.
2611 static int do_otp_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2612 size_t *retlen
, u_char
*buf
)
2614 struct onenand_chip
*this = mtd
->priv
;
2615 unsigned char *pbuf
= buf
;
2617 struct mtd_oob_ops ops
;
2619 /* Force buffer page aligned */
2620 if (len
< mtd
->writesize
) {
2621 memcpy(this->page_buf
, buf
, len
);
2622 memset(this->page_buf
+ len
, 0xff, mtd
->writesize
- len
);
2623 pbuf
= this->page_buf
;
2624 len
= mtd
->writesize
;
2627 /* Enter OTP access mode */
2628 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
2629 this->wait(mtd
, FL_OTPING
);
2635 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
2636 *retlen
= ops
.retlen
;
2638 /* Exit OTP access mode */
2639 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
2640 this->wait(mtd
, FL_RESETING
);
2646 * do_otp_lock - [DEFAULT] Lock OTP block area
2647 * @param mtd MTD device structure
2648 * @param from The offset to lock
2649 * @param len number of bytes to lock
2650 * @param retlen pointer to variable to store the number of lock bytes
2651 * @param buf the databuffer to put/get data
2653 * Lock OTP block area.
2655 static int do_otp_lock(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2656 size_t *retlen
, u_char
*buf
)
2658 struct onenand_chip
*this = mtd
->priv
;
2659 struct mtd_oob_ops ops
;
2662 /* Enter OTP access mode */
2663 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
2664 this->wait(mtd
, FL_OTPING
);
2666 if (FLEXONENAND(this)) {
2668 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2669 * main area of page 49.
2671 ops
.len
= mtd
->writesize
;
2675 ret
= onenand_write_ops_nolock(mtd
, mtd
->writesize
* 49, &ops
);
2676 *retlen
= ops
.retlen
;
2678 ops
.mode
= MTD_OOB_PLACE
;
2682 ret
= onenand_write_oob_nolock(mtd
, from
, &ops
);
2683 *retlen
= ops
.oobretlen
;
2686 /* Exit OTP access mode */
2687 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
2688 this->wait(mtd
, FL_RESETING
);
2694 * onenand_otp_walk - [DEFAULT] Handle OTP operation
2695 * @param mtd MTD device structure
2696 * @param from The offset to read/write
2697 * @param len number of bytes to read/write
2698 * @param retlen pointer to variable to store the number of read bytes
2699 * @param buf the databuffer to put/get data
2700 * @param action do given action
2701 * @param mode specify user and factory
2703 * Handle OTP operation.
2705 static int onenand_otp_walk(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2706 size_t *retlen
, u_char
*buf
,
2707 otp_op_t action
, int mode
)
2709 struct onenand_chip
*this = mtd
->priv
;
2716 density
= onenand_get_density(this->device_id
);
2717 if (density
< ONENAND_DEVICE_DENSITY_512Mb
)
2722 if (mode
== MTD_OTP_FACTORY
) {
2723 from
+= mtd
->writesize
* otp_pages
;
2724 otp_pages
= 64 - otp_pages
;
2727 /* Check User/Factory boundary */
2728 if (((mtd
->writesize
* otp_pages
) - (from
+ len
)) < 0)
2731 onenand_get_device(mtd
, FL_OTPING
);
2732 while (len
> 0 && otp_pages
> 0) {
2733 if (!action
) { /* OTP Info functions */
2734 struct otp_info
*otpinfo
;
2736 len
-= sizeof(struct otp_info
);
2742 otpinfo
= (struct otp_info
*) buf
;
2743 otpinfo
->start
= from
;
2744 otpinfo
->length
= mtd
->writesize
;
2745 otpinfo
->locked
= 0;
2747 from
+= mtd
->writesize
;
2748 buf
+= sizeof(struct otp_info
);
2749 *retlen
+= sizeof(struct otp_info
);
2754 ret
= action(mtd
, from
, len
, &tmp_retlen
, buf
);
2765 onenand_release_device(mtd
);
2771 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2772 * @param mtd MTD device structure
2773 * @param buf the databuffer to put/get data
2774 * @param len number of bytes to read
2776 * Read factory OTP info.
2778 static int onenand_get_fact_prot_info(struct mtd_info
*mtd
,
2779 struct otp_info
*buf
, size_t len
)
2784 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_FACTORY
);
2786 return ret
? : retlen
;
2790 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2791 * @param mtd MTD device structure
2792 * @param from The offset to read
2793 * @param len number of bytes to read
2794 * @param retlen pointer to variable to store the number of read bytes
2795 * @param buf the databuffer to put/get data
2797 * Read factory OTP area.
2799 static int onenand_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2800 size_t len
, size_t *retlen
, u_char
*buf
)
2802 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_FACTORY
);
2806 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2807 * @param mtd MTD device structure
2808 * @param buf the databuffer to put/get data
2809 * @param len number of bytes to read
2811 * Read user OTP info.
2813 static int onenand_get_user_prot_info(struct mtd_info
*mtd
,
2814 struct otp_info
*buf
, size_t len
)
2819 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_USER
);
2821 return ret
? : retlen
;
2825 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2826 * @param mtd MTD device structure
2827 * @param from The offset to read
2828 * @param len number of bytes to read
2829 * @param retlen pointer to variable to store the number of read bytes
2830 * @param buf the databuffer to put/get data
2832 * Read user OTP area.
2834 static int onenand_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2835 size_t len
, size_t *retlen
, u_char
*buf
)
2837 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_USER
);
2841 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2842 * @param mtd MTD device structure
2843 * @param from The offset to write
2844 * @param len number of bytes to write
2845 * @param retlen pointer to variable to store the number of write bytes
2846 * @param buf the databuffer to put/get data
2848 * Write user OTP area.
2850 static int onenand_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2851 size_t len
, size_t *retlen
, u_char
*buf
)
2853 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_write
, MTD_OTP_USER
);
2857 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2858 * @param mtd MTD device structure
2859 * @param from The offset to lock
2860 * @param len number of bytes to unlock
2862 * Write lock mark on spare area in page 0 in OTP block
2864 static int onenand_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2867 struct onenand_chip
*this = mtd
->priv
;
2868 u_char
*buf
= FLEXONENAND(this) ? this->page_buf
: this->oob_buf
;
2872 memset(buf
, 0xff, FLEXONENAND(this) ? this->writesize
2875 * Note: OTP lock operation
2876 * OTP block : 0xXXFC
2877 * 1st block : 0xXXF3 (If chip support)
2878 * Both : 0xXXF0 (If chip support)
2880 if (FLEXONENAND(this))
2881 buf
[FLEXONENAND_OTP_LOCK_OFFSET
] = 0xFC;
2883 buf
[ONENAND_OTP_LOCK_OFFSET
] = 0xFC;
2886 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2887 * We write 16 bytes spare area instead of 2 bytes.
2888 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2889 * main area of page 49.
2893 len
= FLEXONENAND(this) ? mtd
->writesize
: 16;
2895 ret
= onenand_otp_walk(mtd
, from
, len
, &retlen
, buf
, do_otp_lock
, MTD_OTP_USER
);
2897 return ret
? : retlen
;
2899 #endif /* CONFIG_MTD_ONENAND_OTP */
2902 * onenand_check_features - Check and set OneNAND features
2903 * @param mtd MTD data structure
2905 * Check and set OneNAND features
2909 static void onenand_check_features(struct mtd_info
*mtd
)
2911 struct onenand_chip
*this = mtd
->priv
;
2912 unsigned int density
, process
;
2914 /* Lock scheme depends on density and process */
2915 density
= onenand_get_density(this->device_id
);
2916 process
= this->version_id
>> ONENAND_VERSION_PROCESS_SHIFT
;
2920 case ONENAND_DEVICE_DENSITY_4Gb
:
2921 this->options
|= ONENAND_HAS_2PLANE
;
2923 case ONENAND_DEVICE_DENSITY_2Gb
:
2924 /* 2Gb DDP does not have 2 plane */
2925 if (!ONENAND_IS_DDP(this))
2926 this->options
|= ONENAND_HAS_2PLANE
;
2927 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
2929 case ONENAND_DEVICE_DENSITY_1Gb
:
2930 /* A-Die has all block unlock */
2932 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
2936 /* Some OneNAND has continuous lock scheme */
2938 this->options
|= ONENAND_HAS_CONT_LOCK
;
2942 if (ONENAND_IS_MLC(this))
2943 this->options
&= ~ONENAND_HAS_2PLANE
;
2945 if (FLEXONENAND(this)) {
2946 this->options
&= ~ONENAND_HAS_CONT_LOCK
;
2947 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
2950 if (this->options
& ONENAND_HAS_CONT_LOCK
)
2951 printk(KERN_DEBUG
"Lock scheme is Continuous Lock\n");
2952 if (this->options
& ONENAND_HAS_UNLOCK_ALL
)
2953 printk(KERN_DEBUG
"Chip support all block unlock\n");
2954 if (this->options
& ONENAND_HAS_2PLANE
)
2955 printk(KERN_DEBUG
"Chip has 2 plane\n");
2959 * onenand_print_device_info - Print device & version ID
2960 * @param device device ID
2961 * @param version version ID
2963 * Print device & version ID
2965 static void onenand_print_device_info(int device
, int version
)
2967 int vcc
, demuxed
, ddp
, density
, flexonenand
;
2969 vcc
= device
& ONENAND_DEVICE_VCC_MASK
;
2970 demuxed
= device
& ONENAND_DEVICE_IS_DEMUX
;
2971 ddp
= device
& ONENAND_DEVICE_IS_DDP
;
2972 density
= onenand_get_density(device
);
2973 flexonenand
= device
& DEVICE_IS_FLEXONENAND
;
2974 printk(KERN_INFO
"%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2975 demuxed
? "" : "Muxed ",
2976 flexonenand
? "Flex-" : "",
2979 vcc
? "2.65/3.3" : "1.8",
2981 printk(KERN_INFO
"OneNAND version = 0x%04x\n", version
);
2984 static const struct onenand_manufacturers onenand_manuf_ids
[] = {
2985 {ONENAND_MFR_SAMSUNG
, "Samsung"},
2986 {ONENAND_MFR_NUMONYX
, "Numonyx"},
2990 * onenand_check_maf - Check manufacturer ID
2991 * @param manuf manufacturer ID
2993 * Check manufacturer ID
2995 static int onenand_check_maf(int manuf
)
2997 int size
= ARRAY_SIZE(onenand_manuf_ids
);
3001 for (i
= 0; i
< size
; i
++)
3002 if (manuf
== onenand_manuf_ids
[i
].id
)
3006 name
= onenand_manuf_ids
[i
].name
;
3010 printk(KERN_DEBUG
"OneNAND Manufacturer: %s (0x%0x)\n", name
, manuf
);
3016 * flexonenand_get_boundary - Reads the SLC boundary
3017 * @param onenand_info - onenand info structure
3019 static int flexonenand_get_boundary(struct mtd_info
*mtd
)
3021 struct onenand_chip
*this = mtd
->priv
;
3023 int ret
, syscfg
, locked
;
3026 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
3027 this->write_word((syscfg
| 0x0100), this->base
+ ONENAND_REG_SYS_CFG1
);
3029 for (die
= 0; die
< this->dies
; die
++) {
3030 this->command(mtd
, FLEXONENAND_CMD_PI_ACCESS
, die
, 0);
3031 this->wait(mtd
, FL_SYNCING
);
3033 this->command(mtd
, FLEXONENAND_CMD_READ_PI
, die
, 0);
3034 ret
= this->wait(mtd
, FL_READING
);
3036 bdry
= this->read_word(this->base
+ ONENAND_DATARAM
);
3037 if ((bdry
>> FLEXONENAND_PI_UNLOCK_SHIFT
) == 3)
3041 this->boundary
[die
] = bdry
& FLEXONENAND_PI_MASK
;
3043 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
3044 ret
= this->wait(mtd
, FL_RESETING
);
3046 printk(KERN_INFO
"Die %d boundary: %d%s\n", die
,
3047 this->boundary
[die
], locked
? "(Locked)" : "(Unlocked)");
3051 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
3056 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3057 * boundary[], diesize[], mtd->size, mtd->erasesize
3058 * @param mtd - MTD device structure
3060 static void flexonenand_get_size(struct mtd_info
*mtd
)
3062 struct onenand_chip
*this = mtd
->priv
;
3063 int die
, i
, eraseshift
, density
;
3064 int blksperdie
, maxbdry
;
3067 density
= onenand_get_density(this->device_id
);
3068 blksperdie
= ((loff_t
)(16 << density
) << 20) >> (this->erase_shift
);
3069 blksperdie
>>= ONENAND_IS_DDP(this) ? 1 : 0;
3070 maxbdry
= blksperdie
- 1;
3071 eraseshift
= this->erase_shift
- 1;
3073 mtd
->numeraseregions
= this->dies
<< 1;
3075 /* This fills up the device boundary */
3076 flexonenand_get_boundary(mtd
);
3079 for (; die
< this->dies
; die
++) {
3080 if (!die
|| this->boundary
[die
-1] != maxbdry
) {
3082 mtd
->eraseregions
[i
].offset
= ofs
;
3083 mtd
->eraseregions
[i
].erasesize
= 1 << eraseshift
;
3084 mtd
->eraseregions
[i
].numblocks
=
3085 this->boundary
[die
] + 1;
3086 ofs
+= mtd
->eraseregions
[i
].numblocks
<< eraseshift
;
3089 mtd
->numeraseregions
-= 1;
3090 mtd
->eraseregions
[i
].numblocks
+=
3091 this->boundary
[die
] + 1;
3092 ofs
+= (this->boundary
[die
] + 1) << (eraseshift
- 1);
3094 if (this->boundary
[die
] != maxbdry
) {
3096 mtd
->eraseregions
[i
].offset
= ofs
;
3097 mtd
->eraseregions
[i
].erasesize
= 1 << eraseshift
;
3098 mtd
->eraseregions
[i
].numblocks
= maxbdry
^
3099 this->boundary
[die
];
3100 ofs
+= mtd
->eraseregions
[i
].numblocks
<< eraseshift
;
3103 mtd
->numeraseregions
-= 1;
3106 /* Expose MLC erase size except when all blocks are SLC */
3107 mtd
->erasesize
= 1 << this->erase_shift
;
3108 if (mtd
->numeraseregions
== 1)
3109 mtd
->erasesize
>>= 1;
3111 printk(KERN_INFO
"Device has %d eraseregions\n", mtd
->numeraseregions
);
3112 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
3113 printk(KERN_INFO
"[offset: 0x%08x, erasesize: 0x%05x,"
3114 " numblocks: %04u]\n",
3115 (unsigned int) mtd
->eraseregions
[i
].offset
,
3116 mtd
->eraseregions
[i
].erasesize
,
3117 mtd
->eraseregions
[i
].numblocks
);
3119 for (die
= 0, mtd
->size
= 0; die
< this->dies
; die
++) {
3120 this->diesize
[die
] = (loff_t
)blksperdie
<< this->erase_shift
;
3121 this->diesize
[die
] -= (loff_t
)(this->boundary
[die
] + 1)
3122 << (this->erase_shift
- 1);
3123 mtd
->size
+= this->diesize
[die
];
3128 * flexonenand_check_blocks_erased - Check if blocks are erased
3129 * @param mtd_info - mtd info structure
3130 * @param start - first erase block to check
3131 * @param end - last erase block to check
3133 * Converting an unerased block from MLC to SLC
3134 * causes byte values to change. Since both data and its ECC
3135 * have changed, reads on the block give uncorrectable error.
3136 * This might lead to the block being detected as bad.
3138 * Avoid this by ensuring that the block to be converted is
3141 static int flexonenand_check_blocks_erased(struct mtd_info
*mtd
, int start
, int end
)
3143 struct onenand_chip
*this = mtd
->priv
;
3146 struct mtd_oob_ops ops
= {
3147 .mode
= MTD_OOB_PLACE
,
3149 .ooblen
= mtd
->oobsize
,
3151 .oobbuf
= this->oob_buf
,
3155 printk(KERN_DEBUG
"Check blocks from %d to %d\n", start
, end
);
3157 for (block
= start
; block
<= end
; block
++) {
3158 addr
= flexonenand_addr(this, block
);
3159 if (onenand_block_isbad_nolock(mtd
, addr
, 0))
3163 * Since main area write results in ECC write to spare,
3164 * it is sufficient to check only ECC bytes for change.
3166 ret
= onenand_read_oob_nolock(mtd
, addr
, &ops
);
3170 for (i
= 0; i
< mtd
->oobsize
; i
++)
3171 if (this->oob_buf
[i
] != 0xff)
3174 if (i
!= mtd
->oobsize
) {
3175 printk(KERN_WARNING
"Block %d not erased.\n", block
);
3184 * flexonenand_set_boundary - Writes the SLC boundary
3185 * @param mtd - mtd info structure
3187 int flexonenand_set_boundary(struct mtd_info
*mtd
, int die
,
3188 int boundary
, int lock
)
3190 struct onenand_chip
*this = mtd
->priv
;
3191 int ret
, density
, blksperdie
, old
, new, thisboundary
;
3194 /* Change only once for SDP Flex-OneNAND */
3195 if (die
&& (!ONENAND_IS_DDP(this)))
3198 /* boundary value of -1 indicates no required change */
3199 if (boundary
< 0 || boundary
== this->boundary
[die
])
3202 density
= onenand_get_density(this->device_id
);
3203 blksperdie
= ((16 << density
) << 20) >> this->erase_shift
;
3204 blksperdie
>>= ONENAND_IS_DDP(this) ? 1 : 0;
3206 if (boundary
>= blksperdie
) {
3207 printk(KERN_ERR
"flexonenand_set_boundary: Invalid boundary value. "
3208 "Boundary not changed.\n");
3212 /* Check if converting blocks are erased */
3213 old
= this->boundary
[die
] + (die
* this->density_mask
);
3214 new = boundary
+ (die
* this->density_mask
);
3215 ret
= flexonenand_check_blocks_erased(mtd
, min(old
, new) + 1, max(old
, new));
3217 printk(KERN_ERR
"flexonenand_set_boundary: Please erase blocks before boundary change\n");
3221 this->command(mtd
, FLEXONENAND_CMD_PI_ACCESS
, die
, 0);
3222 this->wait(mtd
, FL_SYNCING
);
3224 /* Check is boundary is locked */
3225 this->command(mtd
, FLEXONENAND_CMD_READ_PI
, die
, 0);
3226 ret
= this->wait(mtd
, FL_READING
);
3228 thisboundary
= this->read_word(this->base
+ ONENAND_DATARAM
);
3229 if ((thisboundary
>> FLEXONENAND_PI_UNLOCK_SHIFT
) != 3) {
3230 printk(KERN_ERR
"flexonenand_set_boundary: boundary locked\n");
3235 printk(KERN_INFO
"flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
3236 die
, boundary
, lock
? "(Locked)" : "(Unlocked)");
3238 addr
= die
? this->diesize
[0] : 0;
3240 boundary
&= FLEXONENAND_PI_MASK
;
3241 boundary
|= lock
? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT
);
3243 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, 0);
3244 ret
= this->wait(mtd
, FL_ERASING
);
3246 printk(KERN_ERR
"flexonenand_set_boundary: Failed PI erase for Die %d\n", die
);
3250 this->write_word(boundary
, this->base
+ ONENAND_DATARAM
);
3251 this->command(mtd
, ONENAND_CMD_PROG
, addr
, 0);
3252 ret
= this->wait(mtd
, FL_WRITING
);
3254 printk(KERN_ERR
"flexonenand_set_boundary: Failed PI write for Die %d\n", die
);
3258 this->command(mtd
, FLEXONENAND_CMD_PI_UPDATE
, die
, 0);
3259 ret
= this->wait(mtd
, FL_WRITING
);
3261 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_REG_COMMAND
);
3262 this->wait(mtd
, FL_RESETING
);
3264 /* Recalculate device size on boundary change*/
3265 flexonenand_get_size(mtd
);
3271 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3272 * @param mtd MTD device structure
3274 * OneNAND detection method:
3275 * Compare the values from command with ones from register
3277 static int onenand_probe(struct mtd_info
*mtd
)
3279 struct onenand_chip
*this = mtd
->priv
;
3280 int bram_maf_id
, bram_dev_id
, maf_id
, dev_id
, ver_id
;
3284 /* Save system configuration 1 */
3285 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
3286 /* Clear Sync. Burst Read mode to read BootRAM */
3287 this->write_word((syscfg
& ~ONENAND_SYS_CFG1_SYNC_READ
& ~ONENAND_SYS_CFG1_SYNC_WRITE
), this->base
+ ONENAND_REG_SYS_CFG1
);
3289 /* Send the command for reading device ID from BootRAM */
3290 this->write_word(ONENAND_CMD_READID
, this->base
+ ONENAND_BOOTRAM
);
3292 /* Read manufacturer and device IDs from BootRAM */
3293 bram_maf_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x0);
3294 bram_dev_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x2);
3296 /* Reset OneNAND to read default register values */
3297 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_BOOTRAM
);
3299 this->wait(mtd
, FL_RESETING
);
3301 /* Restore system configuration 1 */
3302 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
3304 /* Check manufacturer ID */
3305 if (onenand_check_maf(bram_maf_id
))
3308 /* Read manufacturer and device IDs from Register */
3309 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
3310 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
3311 ver_id
= this->read_word(this->base
+ ONENAND_REG_VERSION_ID
);
3312 this->technology
= this->read_word(this->base
+ ONENAND_REG_TECHNOLOGY
);
3314 /* Check OneNAND device */
3315 if (maf_id
!= bram_maf_id
|| dev_id
!= bram_dev_id
)
3318 /* Flash device information */
3319 onenand_print_device_info(dev_id
, ver_id
);
3320 this->device_id
= dev_id
;
3321 this->version_id
= ver_id
;
3323 density
= onenand_get_density(dev_id
);
3324 if (FLEXONENAND(this)) {
3325 this->dies
= ONENAND_IS_DDP(this) ? 2 : 1;
3326 /* Maximum possible erase regions */
3327 mtd
->numeraseregions
= this->dies
<< 1;
3328 mtd
->eraseregions
= kzalloc(sizeof(struct mtd_erase_region_info
)
3329 * (this->dies
<< 1), GFP_KERNEL
);
3330 if (!mtd
->eraseregions
)
3335 * For Flex-OneNAND, chipsize represents maximum possible device size.
3336 * mtd->size represents the actual device size.
3338 this->chipsize
= (16 << density
) << 20;
3340 /* OneNAND page size & block size */
3341 /* The data buffer size is equal to page size */
3342 mtd
->writesize
= this->read_word(this->base
+ ONENAND_REG_DATA_BUFFER_SIZE
);
3343 /* We use the full BufferRAM */
3344 if (ONENAND_IS_MLC(this))
3345 mtd
->writesize
<<= 1;
3347 mtd
->oobsize
= mtd
->writesize
>> 5;
3348 /* Pages per a block are always 64 in OneNAND */
3349 mtd
->erasesize
= mtd
->writesize
<< 6;
3351 * Flex-OneNAND SLC area has 64 pages per block.
3352 * Flex-OneNAND MLC area has 128 pages per block.
3353 * Expose MLC erase size to find erase_shift and page_mask.
3355 if (FLEXONENAND(this))
3356 mtd
->erasesize
<<= 1;
3358 this->erase_shift
= ffs(mtd
->erasesize
) - 1;
3359 this->page_shift
= ffs(mtd
->writesize
) - 1;
3360 this->page_mask
= (1 << (this->erase_shift
- this->page_shift
)) - 1;
3361 /* Set density mask. it is used for DDP */
3362 if (ONENAND_IS_DDP(this))
3363 this->density_mask
= this->chipsize
>> (this->erase_shift
+ 1);
3364 /* It's real page size */
3365 this->writesize
= mtd
->writesize
;
3367 /* REVISIT: Multichip handling */
3369 if (FLEXONENAND(this))
3370 flexonenand_get_size(mtd
);
3372 mtd
->size
= this->chipsize
;
3374 /* Check OneNAND features */
3375 onenand_check_features(mtd
);
3378 * We emulate the 4KiB page and 256KiB erase block size
3379 * But oobsize is still 64 bytes.
3380 * It is only valid if you turn on 2X program support,
3381 * Otherwise it will be ignored by compiler.
3383 if (ONENAND_IS_2PLANE(this)) {
3384 mtd
->writesize
<<= 1;
3385 mtd
->erasesize
<<= 1;
3392 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3393 * @param mtd MTD device structure
3395 static int onenand_suspend(struct mtd_info
*mtd
)
3397 return onenand_get_device(mtd
, FL_PM_SUSPENDED
);
3401 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3402 * @param mtd MTD device structure
3404 static void onenand_resume(struct mtd_info
*mtd
)
3406 struct onenand_chip
*this = mtd
->priv
;
3408 if (this->state
== FL_PM_SUSPENDED
)
3409 onenand_release_device(mtd
);
3411 printk(KERN_ERR
"resume() called for the chip which is not"
3412 "in suspended state\n");
3416 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3417 * @param mtd MTD device structure
3418 * @param maxchips Number of chips to scan for
3420 * This fills out all the not initialized function pointers
3421 * with the defaults.
3422 * The flash ID is read and the mtd/chip structures are
3423 * filled with the appropriate values.
3425 int onenand_scan(struct mtd_info
*mtd
, int maxchips
)
3428 struct onenand_chip
*this = mtd
->priv
;
3430 if (!this->read_word
)
3431 this->read_word
= onenand_readw
;
3432 if (!this->write_word
)
3433 this->write_word
= onenand_writew
;
3436 this->command
= onenand_command
;
3438 onenand_setup_wait(mtd
);
3439 if (!this->bbt_wait
)
3440 this->bbt_wait
= onenand_bbt_wait
;
3441 if (!this->unlock_all
)
3442 this->unlock_all
= onenand_unlock_all
;
3444 if (!this->read_bufferram
)
3445 this->read_bufferram
= onenand_read_bufferram
;
3446 if (!this->write_bufferram
)
3447 this->write_bufferram
= onenand_write_bufferram
;
3449 if (!this->block_markbad
)
3450 this->block_markbad
= onenand_default_block_markbad
;
3451 if (!this->scan_bbt
)
3452 this->scan_bbt
= onenand_default_bbt
;
3454 if (onenand_probe(mtd
))
3457 /* Set Sync. Burst Read after probing */
3458 if (this->mmcontrol
) {
3459 printk(KERN_INFO
"OneNAND Sync. Burst Read support\n");
3460 this->read_bufferram
= onenand_sync_read_bufferram
;
3463 /* Allocate buffers, if necessary */
3464 if (!this->page_buf
) {
3465 this->page_buf
= kzalloc(mtd
->writesize
, GFP_KERNEL
);
3466 if (!this->page_buf
) {
3467 printk(KERN_ERR
"onenand_scan(): Can't allocate page_buf\n");
3470 this->options
|= ONENAND_PAGEBUF_ALLOC
;
3472 if (!this->oob_buf
) {
3473 this->oob_buf
= kzalloc(mtd
->oobsize
, GFP_KERNEL
);
3474 if (!this->oob_buf
) {
3475 printk(KERN_ERR
"onenand_scan(): Can't allocate oob_buf\n");
3476 if (this->options
& ONENAND_PAGEBUF_ALLOC
) {
3477 this->options
&= ~ONENAND_PAGEBUF_ALLOC
;
3478 kfree(this->page_buf
);
3482 this->options
|= ONENAND_OOBBUF_ALLOC
;
3485 this->state
= FL_READY
;
3486 init_waitqueue_head(&this->wq
);
3487 spin_lock_init(&this->chip_lock
);
3490 * Allow subpage writes up to oobsize.
3492 switch (mtd
->oobsize
) {
3494 this->ecclayout
= &onenand_oob_128
;
3495 mtd
->subpage_sft
= 0;
3498 this->ecclayout
= &onenand_oob_64
;
3499 mtd
->subpage_sft
= 2;
3503 this->ecclayout
= &onenand_oob_32
;
3504 mtd
->subpage_sft
= 1;
3508 printk(KERN_WARNING
"No OOB scheme defined for oobsize %d\n",
3510 mtd
->subpage_sft
= 0;
3511 /* To prevent kernel oops */
3512 this->ecclayout
= &onenand_oob_32
;
3516 this->subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
3519 * The number of bytes available for a client to place data into
3520 * the out of band area
3522 this->ecclayout
->oobavail
= 0;
3523 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&&
3524 this->ecclayout
->oobfree
[i
].length
; i
++)
3525 this->ecclayout
->oobavail
+=
3526 this->ecclayout
->oobfree
[i
].length
;
3527 mtd
->oobavail
= this->ecclayout
->oobavail
;
3529 mtd
->ecclayout
= this->ecclayout
;
3531 /* Fill in remaining MTD driver data */
3532 mtd
->type
= MTD_NANDFLASH
;
3533 mtd
->flags
= MTD_CAP_NANDFLASH
;
3534 mtd
->erase
= onenand_erase
;
3536 mtd
->unpoint
= NULL
;
3537 mtd
->read
= onenand_read
;
3538 mtd
->write
= onenand_write
;
3539 mtd
->read_oob
= onenand_read_oob
;
3540 mtd
->write_oob
= onenand_write_oob
;
3541 mtd
->panic_write
= onenand_panic_write
;
3542 #ifdef CONFIG_MTD_ONENAND_OTP
3543 mtd
->get_fact_prot_info
= onenand_get_fact_prot_info
;
3544 mtd
->read_fact_prot_reg
= onenand_read_fact_prot_reg
;
3545 mtd
->get_user_prot_info
= onenand_get_user_prot_info
;
3546 mtd
->read_user_prot_reg
= onenand_read_user_prot_reg
;
3547 mtd
->write_user_prot_reg
= onenand_write_user_prot_reg
;
3548 mtd
->lock_user_prot_reg
= onenand_lock_user_prot_reg
;
3550 mtd
->sync
= onenand_sync
;
3551 mtd
->lock
= onenand_lock
;
3552 mtd
->unlock
= onenand_unlock
;
3553 mtd
->suspend
= onenand_suspend
;
3554 mtd
->resume
= onenand_resume
;
3555 mtd
->block_isbad
= onenand_block_isbad
;
3556 mtd
->block_markbad
= onenand_block_markbad
;
3557 mtd
->owner
= THIS_MODULE
;
3559 /* Unlock whole block */
3560 this->unlock_all(mtd
);
3562 ret
= this->scan_bbt(mtd
);
3563 if ((!FLEXONENAND(this)) || ret
)
3566 /* Change Flex-OneNAND boundaries if required */
3567 for (i
= 0; i
< MAX_DIES
; i
++)
3568 flexonenand_set_boundary(mtd
, i
, flex_bdry
[2 * i
],
3569 flex_bdry
[(2 * i
) + 1]);
3575 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3576 * @param mtd MTD device structure
3578 void onenand_release(struct mtd_info
*mtd
)
3580 struct onenand_chip
*this = mtd
->priv
;
3582 #ifdef CONFIG_MTD_PARTITIONS
3583 /* Deregister partitions */
3584 del_mtd_partitions (mtd
);
3586 /* Deregister the device */
3587 del_mtd_device (mtd
);
3589 /* Free bad block table memory, if allocated */
3591 struct bbm_info
*bbm
= this->bbm
;
3595 /* Buffers allocated by onenand_scan */
3596 if (this->options
& ONENAND_PAGEBUF_ALLOC
)
3597 kfree(this->page_buf
);
3598 if (this->options
& ONENAND_OOBBUF_ALLOC
)
3599 kfree(this->oob_buf
);
3600 kfree(mtd
->eraseregions
);
3603 EXPORT_SYMBOL_GPL(onenand_scan
);
3604 EXPORT_SYMBOL_GPL(onenand_release
);
3606 MODULE_LICENSE("GPL");
3607 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
3608 MODULE_DESCRIPTION("Generic OneNAND flash driver code");