2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright © 2005-2009 Samsung Electronics
5 * Copyright © 2007 Nokia Corporation
7 * Kyungmin Park <kyungmin.park@samsung.com>
10 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
11 * auto-placement support, read-while load support, various fixes
13 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
14 * Flex-OneNAND support
15 * Amul Kumar Saha <amul.saha at samsung.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/jiffies.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/onenand.h>
34 #include <linux/mtd/partitions.h>
39 * Multiblock erase if number of blocks to erase is 2 or more.
40 * Maximum number of blocks for simultaneous erase is 64.
42 #define MB_ERASE_MIN_BLK_COUNT 2
43 #define MB_ERASE_MAX_BLK_COUNT 64
45 /* Default Flex-OneNAND boundary and lock respectively */
46 static int flex_bdry
[MAX_DIES
* 2] = { -1, 0, -1, 0 };
48 module_param_array(flex_bdry
, int, NULL
, 0400);
49 MODULE_PARM_DESC(flex_bdry
, "SLC Boundary information for Flex-OneNAND"
50 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
51 "DIE_BDRY: SLC boundary of the die"
52 "LOCK: Locking information for SLC boundary"
53 " : 0->Set boundary in unlocked status"
54 " : 1->Set boundary in locked status");
56 /* Default OneNAND/Flex-OneNAND OTP options*/
59 module_param(otp
, int, 0400);
60 MODULE_PARM_DESC(otp
, "Corresponding behaviour of OneNAND in OTP"
61 "Syntax : otp=LOCK_TYPE"
62 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
63 " : 0 -> Default (No Blocks Locked)"
64 " : 1 -> OTP Block lock"
65 " : 2 -> 1st Block lock"
66 " : 3 -> BOTH OTP Block and 1st Block lock");
69 * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
70 * For now, we expose only 64 out of 80 ecc bytes
72 static struct nand_ecclayout flexonenand_oob_128
= {
75 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
76 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
77 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
78 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
79 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
84 {2, 4}, {18, 4}, {34, 4}, {50, 4},
85 {66, 4}, {82, 4}, {98, 4}, {114, 4}
90 * onenand_oob_128 - oob info for OneNAND with 4KB page
92 * Based on specification:
93 * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
95 * For eccpos we expose only 64 bytes out of 72 (see struct nand_ecclayout)
97 * oobfree uses the spare area fields marked as
98 * "Managed by internal ECC logic for Logical Sector Number area"
100 static struct nand_ecclayout onenand_oob_128
= {
103 7, 8, 9, 10, 11, 12, 13, 14, 15,
104 23, 24, 25, 26, 27, 28, 29, 30, 31,
105 39, 40, 41, 42, 43, 44, 45, 46, 47,
106 55, 56, 57, 58, 59, 60, 61, 62, 63,
107 71, 72, 73, 74, 75, 76, 77, 78, 79,
108 87, 88, 89, 90, 91, 92, 93, 94, 95,
109 103, 104, 105, 106, 107, 108, 109, 110, 111,
113 {2, 3}, {18, 3}, {34, 3}, {50, 3},
114 {66, 3}, {82, 3}, {98, 3}, {114, 3}
119 * onenand_oob_64 - oob info for large (2KB) page
121 static struct nand_ecclayout onenand_oob_64
= {
130 {2, 3}, {14, 2}, {18, 3}, {30, 2},
131 {34, 3}, {46, 2}, {50, 3}, {62, 2}
136 * onenand_oob_32 - oob info for middle (1KB) page
138 static struct nand_ecclayout onenand_oob_32
= {
144 .oobfree
= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
147 static const unsigned char ffchars
[] = {
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
151 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
152 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
153 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
154 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
155 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
156 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
157 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
158 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
160 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
162 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
163 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
167 * onenand_readw - [OneNAND Interface] Read OneNAND register
168 * @param addr address to read
170 * Read OneNAND register
172 static unsigned short onenand_readw(void __iomem
*addr
)
178 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
179 * @param value value to write
180 * @param addr address to write
182 * Write OneNAND register with value
184 static void onenand_writew(unsigned short value
, void __iomem
*addr
)
190 * onenand_block_address - [DEFAULT] Get block address
191 * @param this onenand chip data structure
192 * @param block the block
193 * @return translated block address if DDP, otherwise same
195 * Setup Start Address 1 Register (F100h)
197 static int onenand_block_address(struct onenand_chip
*this, int block
)
199 /* Device Flash Core select, NAND Flash Block Address */
200 if (block
& this->density_mask
)
201 return ONENAND_DDP_CHIP1
| (block
^ this->density_mask
);
207 * onenand_bufferram_address - [DEFAULT] Get bufferram address
208 * @param this onenand chip data structure
209 * @param block the block
210 * @return set DBS value if DDP, otherwise 0
212 * Setup Start Address 2 Register (F101h) for DDP
214 static int onenand_bufferram_address(struct onenand_chip
*this, int block
)
216 /* Device BufferRAM Select */
217 if (block
& this->density_mask
)
218 return ONENAND_DDP_CHIP1
;
220 return ONENAND_DDP_CHIP0
;
224 * onenand_page_address - [DEFAULT] Get page address
225 * @param page the page address
226 * @param sector the sector address
227 * @return combined page and sector address
229 * Setup Start Address 8 Register (F107h)
231 static int onenand_page_address(int page
, int sector
)
233 /* Flash Page Address, Flash Sector Address */
236 fpa
= page
& ONENAND_FPA_MASK
;
237 fsa
= sector
& ONENAND_FSA_MASK
;
239 return ((fpa
<< ONENAND_FPA_SHIFT
) | fsa
);
243 * onenand_buffer_address - [DEFAULT] Get buffer address
244 * @param dataram1 DataRAM index
245 * @param sectors the sector address
246 * @param count the number of sectors
247 * @return the start buffer value
249 * Setup Start Buffer Register (F200h)
251 static int onenand_buffer_address(int dataram1
, int sectors
, int count
)
255 /* BufferRAM Sector Address */
256 bsa
= sectors
& ONENAND_BSA_MASK
;
259 bsa
|= ONENAND_BSA_DATARAM1
; /* DataRAM1 */
261 bsa
|= ONENAND_BSA_DATARAM0
; /* DataRAM0 */
263 /* BufferRAM Sector Count */
264 bsc
= count
& ONENAND_BSC_MASK
;
266 return ((bsa
<< ONENAND_BSA_SHIFT
) | bsc
);
270 * flexonenand_block- For given address return block number
271 * @param this - OneNAND device structure
272 * @param addr - Address for which block number is needed
274 static unsigned flexonenand_block(struct onenand_chip
*this, loff_t addr
)
276 unsigned boundary
, blk
, die
= 0;
278 if (ONENAND_IS_DDP(this) && addr
>= this->diesize
[0]) {
280 addr
-= this->diesize
[0];
283 boundary
= this->boundary
[die
];
285 blk
= addr
>> (this->erase_shift
- 1);
287 blk
= (blk
+ boundary
+ 1) >> 1;
289 blk
+= die
? this->density_mask
: 0;
293 inline unsigned onenand_block(struct onenand_chip
*this, loff_t addr
)
295 if (!FLEXONENAND(this))
296 return addr
>> this->erase_shift
;
297 return flexonenand_block(this, addr
);
301 * flexonenand_addr - Return address of the block
302 * @this: OneNAND device structure
303 * @block: Block number on Flex-OneNAND
305 * Return address of the block
307 static loff_t
flexonenand_addr(struct onenand_chip
*this, int block
)
310 int die
= 0, boundary
;
312 if (ONENAND_IS_DDP(this) && block
>= this->density_mask
) {
313 block
-= this->density_mask
;
315 ofs
= this->diesize
[0];
318 boundary
= this->boundary
[die
];
319 ofs
+= (loff_t
)block
<< (this->erase_shift
- 1);
320 if (block
> (boundary
+ 1))
321 ofs
+= (loff_t
)(block
- boundary
- 1) << (this->erase_shift
- 1);
325 loff_t
onenand_addr(struct onenand_chip
*this, int block
)
327 if (!FLEXONENAND(this))
328 return (loff_t
)block
<< this->erase_shift
;
329 return flexonenand_addr(this, block
);
331 EXPORT_SYMBOL(onenand_addr
);
334 * onenand_get_density - [DEFAULT] Get OneNAND density
335 * @param dev_id OneNAND device ID
337 * Get OneNAND density from device ID
339 static inline int onenand_get_density(int dev_id
)
341 int density
= dev_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
342 return (density
& ONENAND_DEVICE_DENSITY_MASK
);
346 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
347 * @param mtd MTD device structure
348 * @param addr address whose erase region needs to be identified
350 int flexonenand_region(struct mtd_info
*mtd
, loff_t addr
)
354 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
355 if (addr
< mtd
->eraseregions
[i
].offset
)
359 EXPORT_SYMBOL(flexonenand_region
);
362 * onenand_command - [DEFAULT] Send command to OneNAND device
363 * @param mtd MTD device structure
364 * @param cmd the command to be sent
365 * @param addr offset to read from or write to
366 * @param len number of bytes to read or write
368 * Send command to OneNAND device. This function is used for middle/large page
369 * devices (1KB/2KB Bytes per page)
371 static int onenand_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
, size_t len
)
373 struct onenand_chip
*this = mtd
->priv
;
374 int value
, block
, page
;
376 /* Address translation */
378 case ONENAND_CMD_UNLOCK
:
379 case ONENAND_CMD_LOCK
:
380 case ONENAND_CMD_LOCK_TIGHT
:
381 case ONENAND_CMD_UNLOCK_ALL
:
386 case FLEXONENAND_CMD_PI_ACCESS
:
387 /* addr contains die index */
388 block
= addr
* this->density_mask
;
392 case ONENAND_CMD_ERASE
:
393 case ONENAND_CMD_MULTIBLOCK_ERASE
:
394 case ONENAND_CMD_ERASE_VERIFY
:
395 case ONENAND_CMD_BUFFERRAM
:
396 case ONENAND_CMD_OTP_ACCESS
:
397 block
= onenand_block(this, addr
);
401 case FLEXONENAND_CMD_READ_PI
:
402 cmd
= ONENAND_CMD_READ
;
403 block
= addr
* this->density_mask
;
408 block
= onenand_block(this, addr
);
409 if (FLEXONENAND(this))
410 page
= (int) (addr
- onenand_addr(this, block
))>>\
413 page
= (int) (addr
>> this->page_shift
);
414 if (ONENAND_IS_2PLANE(this)) {
415 /* Make the even block number */
417 /* Is it the odd plane? */
418 if (addr
& this->writesize
)
422 page
&= this->page_mask
;
426 /* NOTE: The setting order of the registers is very important! */
427 if (cmd
== ONENAND_CMD_BUFFERRAM
) {
428 /* Select DataRAM for DDP */
429 value
= onenand_bufferram_address(this, block
);
430 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
432 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
433 /* It is always BufferRAM0 */
434 ONENAND_SET_BUFFERRAM0(this);
436 /* Switch to the next data buffer */
437 ONENAND_SET_NEXT_BUFFERRAM(this);
443 /* Write 'DFS, FBA' of Flash */
444 value
= onenand_block_address(this, block
);
445 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
447 /* Select DataRAM for DDP */
448 value
= onenand_bufferram_address(this, block
);
449 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
453 /* Now we use page size operation */
454 int sectors
= 0, count
= 0;
458 case FLEXONENAND_CMD_RECOVER_LSB
:
459 case ONENAND_CMD_READ
:
460 case ONENAND_CMD_READOOB
:
461 if (ONENAND_IS_4KB_PAGE(this))
462 /* It is always BufferRAM0 */
463 dataram
= ONENAND_SET_BUFFERRAM0(this);
465 dataram
= ONENAND_SET_NEXT_BUFFERRAM(this);
469 if (ONENAND_IS_2PLANE(this) && cmd
== ONENAND_CMD_PROG
)
470 cmd
= ONENAND_CMD_2X_PROG
;
471 dataram
= ONENAND_CURRENT_BUFFERRAM(this);
475 /* Write 'FPA, FSA' of Flash */
476 value
= onenand_page_address(page
, sectors
);
477 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS8
);
479 /* Write 'BSA, BSC' of DataRAM */
480 value
= onenand_buffer_address(dataram
, sectors
, count
);
481 this->write_word(value
, this->base
+ ONENAND_REG_START_BUFFER
);
484 /* Interrupt clear */
485 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
488 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
494 * onenand_read_ecc - return ecc status
495 * @param this onenand chip structure
497 static inline int onenand_read_ecc(struct onenand_chip
*this)
499 int ecc
, i
, result
= 0;
501 if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
502 return this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
504 for (i
= 0; i
< 4; i
++) {
505 ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
+ i
*2);
508 if (ecc
& FLEXONENAND_UNCORRECTABLE_ERROR
)
509 return ONENAND_ECC_2BIT_ALL
;
511 result
= ONENAND_ECC_1BIT_ALL
;
518 * onenand_wait - [DEFAULT] wait until the command is done
519 * @param mtd MTD device structure
520 * @param state state to select the max. timeout value
522 * Wait for command done. This applies to all OneNAND command
523 * Read can take up to 30us, erase up to 2ms and program up to 350us
524 * according to general OneNAND specs
526 static int onenand_wait(struct mtd_info
*mtd
, int state
)
528 struct onenand_chip
* this = mtd
->priv
;
529 unsigned long timeout
;
530 unsigned int flags
= ONENAND_INT_MASTER
;
531 unsigned int interrupt
= 0;
534 /* The 20 msec is enough */
535 timeout
= jiffies
+ msecs_to_jiffies(20);
536 while (time_before(jiffies
, timeout
)) {
537 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
539 if (interrupt
& flags
)
542 if (state
!= FL_READING
&& state
!= FL_PREPARING_ERASE
)
545 /* To get correct interrupt status in timeout case */
546 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
548 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
551 * In the Spec. it checks the controller status first
552 * However if you get the correct information in case of
553 * power off recovery (POR) test, it should read ECC status first
555 if (interrupt
& ONENAND_INT_READ
) {
556 int ecc
= onenand_read_ecc(this);
558 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
559 printk(KERN_ERR
"%s: ECC error = 0x%04x\n",
561 mtd
->ecc_stats
.failed
++;
563 } else if (ecc
& ONENAND_ECC_1BIT_ALL
) {
564 printk(KERN_DEBUG
"%s: correctable ECC error = 0x%04x\n",
566 mtd
->ecc_stats
.corrected
++;
569 } else if (state
== FL_READING
) {
570 printk(KERN_ERR
"%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
571 __func__
, ctrl
, interrupt
);
575 if (state
== FL_PREPARING_ERASE
&& !(interrupt
& ONENAND_INT_ERASE
)) {
576 printk(KERN_ERR
"%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
577 __func__
, ctrl
, interrupt
);
581 if (!(interrupt
& ONENAND_INT_MASTER
)) {
582 printk(KERN_ERR
"%s: timeout! ctrl=0x%04x intr=0x%04x\n",
583 __func__
, ctrl
, interrupt
);
587 /* If there's controller error, it's a real error */
588 if (ctrl
& ONENAND_CTRL_ERROR
) {
589 printk(KERN_ERR
"%s: controller error = 0x%04x\n",
591 if (ctrl
& ONENAND_CTRL_LOCK
)
592 printk(KERN_ERR
"%s: it's locked error.\n", __func__
);
600 * onenand_interrupt - [DEFAULT] onenand interrupt handler
601 * @param irq onenand interrupt number
602 * @param dev_id interrupt data
606 static irqreturn_t
onenand_interrupt(int irq
, void *data
)
608 struct onenand_chip
*this = data
;
610 /* To handle shared interrupt */
611 if (!this->complete
.done
)
612 complete(&this->complete
);
618 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
619 * @param mtd MTD device structure
620 * @param state state to select the max. timeout value
622 * Wait for command done.
624 static int onenand_interrupt_wait(struct mtd_info
*mtd
, int state
)
626 struct onenand_chip
*this = mtd
->priv
;
628 wait_for_completion(&this->complete
);
630 return onenand_wait(mtd
, state
);
634 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
635 * @param mtd MTD device structure
636 * @param state state to select the max. timeout value
638 * Try interrupt based wait (It is used one-time)
640 static int onenand_try_interrupt_wait(struct mtd_info
*mtd
, int state
)
642 struct onenand_chip
*this = mtd
->priv
;
643 unsigned long remain
, timeout
;
645 /* We use interrupt wait first */
646 this->wait
= onenand_interrupt_wait
;
648 timeout
= msecs_to_jiffies(100);
649 remain
= wait_for_completion_timeout(&this->complete
, timeout
);
651 printk(KERN_INFO
"OneNAND: There's no interrupt. "
652 "We use the normal wait\n");
654 /* Release the irq */
655 free_irq(this->irq
, this);
657 this->wait
= onenand_wait
;
660 return onenand_wait(mtd
, state
);
664 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
665 * @param mtd MTD device structure
667 * There's two method to wait onenand work
668 * 1. polling - read interrupt status register
669 * 2. interrupt - use the kernel interrupt method
671 static void onenand_setup_wait(struct mtd_info
*mtd
)
673 struct onenand_chip
*this = mtd
->priv
;
676 init_completion(&this->complete
);
678 if (this->irq
<= 0) {
679 this->wait
= onenand_wait
;
683 if (request_irq(this->irq
, &onenand_interrupt
,
684 IRQF_SHARED
, "onenand", this)) {
685 /* If we can't get irq, use the normal wait */
686 this->wait
= onenand_wait
;
690 /* Enable interrupt */
691 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
692 syscfg
|= ONENAND_SYS_CFG1_IOBE
;
693 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
695 this->wait
= onenand_try_interrupt_wait
;
699 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
700 * @param mtd MTD data structure
701 * @param area BufferRAM area
702 * @return offset given area
704 * Return BufferRAM offset given area
706 static inline int onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
708 struct onenand_chip
*this = mtd
->priv
;
710 if (ONENAND_CURRENT_BUFFERRAM(this)) {
711 /* Note: the 'this->writesize' is a real page size */
712 if (area
== ONENAND_DATARAM
)
713 return this->writesize
;
714 if (area
== ONENAND_SPARERAM
)
722 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
723 * @param mtd MTD data structure
724 * @param area BufferRAM area
725 * @param buffer the databuffer to put/get data
726 * @param offset offset to read from or write to
727 * @param count number of bytes to read/write
729 * Read the BufferRAM area
731 static int onenand_read_bufferram(struct mtd_info
*mtd
, int area
,
732 unsigned char *buffer
, int offset
, size_t count
)
734 struct onenand_chip
*this = mtd
->priv
;
735 void __iomem
*bufferram
;
737 bufferram
= this->base
+ area
;
739 bufferram
+= onenand_bufferram_offset(mtd
, area
);
741 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
744 /* Align with word(16-bit) size */
747 /* Read word and save byte */
748 word
= this->read_word(bufferram
+ offset
+ count
);
749 buffer
[count
] = (word
& 0xff);
752 memcpy(buffer
, bufferram
+ offset
, count
);
758 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
759 * @param mtd MTD data structure
760 * @param area BufferRAM area
761 * @param buffer the databuffer to put/get data
762 * @param offset offset to read from or write to
763 * @param count number of bytes to read/write
765 * Read the BufferRAM area with Sync. Burst Mode
767 static int onenand_sync_read_bufferram(struct mtd_info
*mtd
, int area
,
768 unsigned char *buffer
, int offset
, size_t count
)
770 struct onenand_chip
*this = mtd
->priv
;
771 void __iomem
*bufferram
;
773 bufferram
= this->base
+ area
;
775 bufferram
+= onenand_bufferram_offset(mtd
, area
);
777 this->mmcontrol(mtd
, ONENAND_SYS_CFG1_SYNC_READ
);
779 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
782 /* Align with word(16-bit) size */
785 /* Read word and save byte */
786 word
= this->read_word(bufferram
+ offset
+ count
);
787 buffer
[count
] = (word
& 0xff);
790 memcpy(buffer
, bufferram
+ offset
, count
);
792 this->mmcontrol(mtd
, 0);
798 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
799 * @param mtd MTD data structure
800 * @param area BufferRAM area
801 * @param buffer the databuffer to put/get data
802 * @param offset offset to read from or write to
803 * @param count number of bytes to read/write
805 * Write the BufferRAM area
807 static int onenand_write_bufferram(struct mtd_info
*mtd
, int area
,
808 const unsigned char *buffer
, int offset
, size_t count
)
810 struct onenand_chip
*this = mtd
->priv
;
811 void __iomem
*bufferram
;
813 bufferram
= this->base
+ area
;
815 bufferram
+= onenand_bufferram_offset(mtd
, area
);
817 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
821 /* Align with word(16-bit) size */
824 /* Calculate byte access offset */
825 byte_offset
= offset
+ count
;
827 /* Read word and save byte */
828 word
= this->read_word(bufferram
+ byte_offset
);
829 word
= (word
& ~0xff) | buffer
[count
];
830 this->write_word(word
, bufferram
+ byte_offset
);
833 memcpy(bufferram
+ offset
, buffer
, count
);
839 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
840 * @param mtd MTD data structure
841 * @param addr address to check
842 * @return blockpage address
844 * Get blockpage address at 2x program mode
846 static int onenand_get_2x_blockpage(struct mtd_info
*mtd
, loff_t addr
)
848 struct onenand_chip
*this = mtd
->priv
;
849 int blockpage
, block
, page
;
851 /* Calculate the even block number */
852 block
= (int) (addr
>> this->erase_shift
) & ~1;
853 /* Is it the odd plane? */
854 if (addr
& this->writesize
)
856 page
= (int) (addr
>> (this->page_shift
+ 1)) & this->page_mask
;
857 blockpage
= (block
<< 7) | page
;
863 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
864 * @param mtd MTD data structure
865 * @param addr address to check
866 * @return 1 if there are valid data, otherwise 0
868 * Check bufferram if there is data we required
870 static int onenand_check_bufferram(struct mtd_info
*mtd
, loff_t addr
)
872 struct onenand_chip
*this = mtd
->priv
;
873 int blockpage
, found
= 0;
876 if (ONENAND_IS_2PLANE(this))
877 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
879 blockpage
= (int) (addr
>> this->page_shift
);
881 /* Is there valid data? */
882 i
= ONENAND_CURRENT_BUFFERRAM(this);
883 if (this->bufferram
[i
].blockpage
== blockpage
)
886 /* Check another BufferRAM */
887 i
= ONENAND_NEXT_BUFFERRAM(this);
888 if (this->bufferram
[i
].blockpage
== blockpage
) {
889 ONENAND_SET_NEXT_BUFFERRAM(this);
894 if (found
&& ONENAND_IS_DDP(this)) {
895 /* Select DataRAM for DDP */
896 int block
= onenand_block(this, addr
);
897 int value
= onenand_bufferram_address(this, block
);
898 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
905 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
906 * @param mtd MTD data structure
907 * @param addr address to update
908 * @param valid valid flag
910 * Update BufferRAM information
912 static void onenand_update_bufferram(struct mtd_info
*mtd
, loff_t addr
,
915 struct onenand_chip
*this = mtd
->priv
;
919 if (ONENAND_IS_2PLANE(this))
920 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
922 blockpage
= (int) (addr
>> this->page_shift
);
924 /* Invalidate another BufferRAM */
925 i
= ONENAND_NEXT_BUFFERRAM(this);
926 if (this->bufferram
[i
].blockpage
== blockpage
)
927 this->bufferram
[i
].blockpage
= -1;
929 /* Update BufferRAM */
930 i
= ONENAND_CURRENT_BUFFERRAM(this);
932 this->bufferram
[i
].blockpage
= blockpage
;
934 this->bufferram
[i
].blockpage
= -1;
938 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
939 * @param mtd MTD data structure
940 * @param addr start address to invalidate
941 * @param len length to invalidate
943 * Invalidate BufferRAM information
945 static void onenand_invalidate_bufferram(struct mtd_info
*mtd
, loff_t addr
,
948 struct onenand_chip
*this = mtd
->priv
;
950 loff_t end_addr
= addr
+ len
;
952 /* Invalidate BufferRAM */
953 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
954 loff_t buf_addr
= this->bufferram
[i
].blockpage
<< this->page_shift
;
955 if (buf_addr
>= addr
&& buf_addr
< end_addr
)
956 this->bufferram
[i
].blockpage
= -1;
961 * onenand_get_device - [GENERIC] Get chip for selected access
962 * @param mtd MTD device structure
963 * @param new_state the state which is requested
965 * Get the device and lock it for exclusive access
967 static int onenand_get_device(struct mtd_info
*mtd
, int new_state
)
969 struct onenand_chip
*this = mtd
->priv
;
970 DECLARE_WAITQUEUE(wait
, current
);
973 * Grab the lock and see if the device is available
976 spin_lock(&this->chip_lock
);
977 if (this->state
== FL_READY
) {
978 this->state
= new_state
;
979 spin_unlock(&this->chip_lock
);
980 if (new_state
!= FL_PM_SUSPENDED
&& this->enable
)
984 if (new_state
== FL_PM_SUSPENDED
) {
985 spin_unlock(&this->chip_lock
);
986 return (this->state
== FL_PM_SUSPENDED
) ? 0 : -EAGAIN
;
988 set_current_state(TASK_UNINTERRUPTIBLE
);
989 add_wait_queue(&this->wq
, &wait
);
990 spin_unlock(&this->chip_lock
);
992 remove_wait_queue(&this->wq
, &wait
);
999 * onenand_release_device - [GENERIC] release chip
1000 * @param mtd MTD device structure
1002 * Deselect, release chip lock and wake up anyone waiting on the device
1004 static void onenand_release_device(struct mtd_info
*mtd
)
1006 struct onenand_chip
*this = mtd
->priv
;
1008 if (this->state
!= FL_PM_SUSPENDED
&& this->disable
)
1010 /* Release the chip */
1011 spin_lock(&this->chip_lock
);
1012 this->state
= FL_READY
;
1014 spin_unlock(&this->chip_lock
);
1018 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
1019 * @param mtd MTD device structure
1020 * @param buf destination address
1021 * @param column oob offset to read from
1022 * @param thislen oob length to read
1024 static int onenand_transfer_auto_oob(struct mtd_info
*mtd
, uint8_t *buf
, int column
,
1027 struct onenand_chip
*this = mtd
->priv
;
1028 struct nand_oobfree
*free
;
1029 int readcol
= column
;
1030 int readend
= column
+ thislen
;
1033 uint8_t *oob_buf
= this->oob_buf
;
1035 free
= this->ecclayout
->oobfree
;
1036 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1037 if (readcol
>= lastgap
)
1038 readcol
+= free
->offset
- lastgap
;
1039 if (readend
>= lastgap
)
1040 readend
+= free
->offset
- lastgap
;
1041 lastgap
= free
->offset
+ free
->length
;
1043 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
1044 free
= this->ecclayout
->oobfree
;
1045 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1046 int free_end
= free
->offset
+ free
->length
;
1047 if (free
->offset
< readend
&& free_end
> readcol
) {
1048 int st
= max_t(int,free
->offset
,readcol
);
1049 int ed
= min_t(int,free_end
,readend
);
1051 memcpy(buf
, oob_buf
+ st
, n
);
1053 } else if (column
== 0)
1060 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1061 * @param mtd MTD device structure
1062 * @param addr address to recover
1063 * @param status return value from onenand_wait / onenand_bbt_wait
1065 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1066 * lower page address and MSB page has higher page address in paired pages.
1067 * If power off occurs during MSB page program, the paired LSB page data can
1068 * become corrupt. LSB page recovery read is a way to read LSB page though page
1069 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1070 * read after power up, issue LSB page recovery read.
1072 static int onenand_recover_lsb(struct mtd_info
*mtd
, loff_t addr
, int status
)
1074 struct onenand_chip
*this = mtd
->priv
;
1077 /* Recovery is only for Flex-OneNAND */
1078 if (!FLEXONENAND(this))
1081 /* check if we failed due to uncorrectable error */
1082 if (!mtd_is_eccerr(status
) && status
!= ONENAND_BBT_READ_ECC_ERROR
)
1085 /* check if address lies in MLC region */
1086 i
= flexonenand_region(mtd
, addr
);
1087 if (mtd
->eraseregions
[i
].erasesize
< (1 << this->erase_shift
))
1090 /* We are attempting to reread, so decrement stats.failed
1091 * which was incremented by onenand_wait due to read failure
1093 printk(KERN_INFO
"%s: Attempting to recover from uncorrectable read\n",
1095 mtd
->ecc_stats
.failed
--;
1097 /* Issue the LSB page recovery command */
1098 this->command(mtd
, FLEXONENAND_CMD_RECOVER_LSB
, addr
, this->writesize
);
1099 return this->wait(mtd
, FL_READING
);
1103 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1104 * @param mtd MTD device structure
1105 * @param from offset to read from
1106 * @param ops: oob operation description structure
1108 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1109 * So, read-while-load is not present.
1111 static int onenand_mlc_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
1112 struct mtd_oob_ops
*ops
)
1114 struct onenand_chip
*this = mtd
->priv
;
1115 struct mtd_ecc_stats stats
;
1116 size_t len
= ops
->len
;
1117 size_t ooblen
= ops
->ooblen
;
1118 u_char
*buf
= ops
->datbuf
;
1119 u_char
*oobbuf
= ops
->oobbuf
;
1120 int read
= 0, column
, thislen
;
1121 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
1123 int writesize
= this->writesize
;
1125 pr_debug("%s: from = 0x%08x, len = %i\n", __func__
, (unsigned int)from
,
1128 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1129 oobsize
= this->ecclayout
->oobavail
;
1131 oobsize
= mtd
->oobsize
;
1133 oobcolumn
= from
& (mtd
->oobsize
- 1);
1135 /* Do not allow reads past end of device */
1136 if (from
+ len
> mtd
->size
) {
1137 printk(KERN_ERR
"%s: Attempt read beyond end of device\n",
1144 stats
= mtd
->ecc_stats
;
1146 while (read
< len
) {
1149 thislen
= min_t(int, writesize
, len
- read
);
1151 column
= from
& (writesize
- 1);
1152 if (column
+ thislen
> writesize
)
1153 thislen
= writesize
- column
;
1155 if (!onenand_check_bufferram(mtd
, from
)) {
1156 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1158 ret
= this->wait(mtd
, FL_READING
);
1160 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1161 onenand_update_bufferram(mtd
, from
, !ret
);
1162 if (mtd_is_eccerr(ret
))
1168 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
1170 thisooblen
= oobsize
- oobcolumn
;
1171 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
1173 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1174 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
1176 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
1177 oobread
+= thisooblen
;
1178 oobbuf
+= thisooblen
;
1191 * Return success, if no ECC failures, else -EBADMSG
1192 * fs driver will take care of that, because
1193 * retlen == desired len and result == -EBADMSG
1196 ops
->oobretlen
= oobread
;
1201 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1204 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1205 return mtd
->ecc_stats
.corrected
!= stats
.corrected
? 1 : 0;
1209 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1210 * @param mtd MTD device structure
1211 * @param from offset to read from
1212 * @param ops: oob operation description structure
1214 * OneNAND read main and/or out-of-band data
1216 static int onenand_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
1217 struct mtd_oob_ops
*ops
)
1219 struct onenand_chip
*this = mtd
->priv
;
1220 struct mtd_ecc_stats stats
;
1221 size_t len
= ops
->len
;
1222 size_t ooblen
= ops
->ooblen
;
1223 u_char
*buf
= ops
->datbuf
;
1224 u_char
*oobbuf
= ops
->oobbuf
;
1225 int read
= 0, column
, thislen
;
1226 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
1227 int ret
= 0, boundary
= 0;
1228 int writesize
= this->writesize
;
1230 pr_debug("%s: from = 0x%08x, len = %i\n", __func__
, (unsigned int)from
,
1233 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1234 oobsize
= this->ecclayout
->oobavail
;
1236 oobsize
= mtd
->oobsize
;
1238 oobcolumn
= from
& (mtd
->oobsize
- 1);
1240 /* Do not allow reads past end of device */
1241 if ((from
+ len
) > mtd
->size
) {
1242 printk(KERN_ERR
"%s: Attempt read beyond end of device\n",
1249 stats
= mtd
->ecc_stats
;
1251 /* Read-while-load method */
1253 /* Do first load to bufferRAM */
1255 if (!onenand_check_bufferram(mtd
, from
)) {
1256 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1257 ret
= this->wait(mtd
, FL_READING
);
1258 onenand_update_bufferram(mtd
, from
, !ret
);
1259 if (mtd_is_eccerr(ret
))
1264 thislen
= min_t(int, writesize
, len
- read
);
1265 column
= from
& (writesize
- 1);
1266 if (column
+ thislen
> writesize
)
1267 thislen
= writesize
- column
;
1270 /* If there is more to load then start next load */
1272 if (read
+ thislen
< len
) {
1273 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
1275 * Chip boundary handling in DDP
1276 * Now we issued chip 1 read and pointed chip 1
1277 * bufferram so we have to point chip 0 bufferram.
1279 if (ONENAND_IS_DDP(this) &&
1280 unlikely(from
== (this->chipsize
>> 1))) {
1281 this->write_word(ONENAND_DDP_CHIP0
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1285 ONENAND_SET_PREV_BUFFERRAM(this);
1287 /* While load is going, read from last bufferRAM */
1288 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
1290 /* Read oob area if needed */
1292 thisooblen
= oobsize
- oobcolumn
;
1293 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
1295 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1296 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
1298 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
1299 oobread
+= thisooblen
;
1300 oobbuf
+= thisooblen
;
1304 /* See if we are done */
1308 /* Set up for next read from bufferRAM */
1309 if (unlikely(boundary
))
1310 this->write_word(ONENAND_DDP_CHIP1
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1311 ONENAND_SET_NEXT_BUFFERRAM(this);
1313 thislen
= min_t(int, writesize
, len
- read
);
1316 /* Now wait for load */
1317 ret
= this->wait(mtd
, FL_READING
);
1318 onenand_update_bufferram(mtd
, from
, !ret
);
1319 if (mtd_is_eccerr(ret
))
1324 * Return success, if no ECC failures, else -EBADMSG
1325 * fs driver will take care of that, because
1326 * retlen == desired len and result == -EBADMSG
1329 ops
->oobretlen
= oobread
;
1334 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1337 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1338 return mtd
->ecc_stats
.corrected
!= stats
.corrected
? 1 : 0;
1342 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1343 * @param mtd MTD device structure
1344 * @param from offset to read from
1345 * @param ops: oob operation description structure
1347 * OneNAND read out-of-band data from the spare area
1349 static int onenand_read_oob_nolock(struct mtd_info
*mtd
, loff_t from
,
1350 struct mtd_oob_ops
*ops
)
1352 struct onenand_chip
*this = mtd
->priv
;
1353 struct mtd_ecc_stats stats
;
1354 int read
= 0, thislen
, column
, oobsize
;
1355 size_t len
= ops
->ooblen
;
1356 unsigned int mode
= ops
->mode
;
1357 u_char
*buf
= ops
->oobbuf
;
1358 int ret
= 0, readcmd
;
1360 from
+= ops
->ooboffs
;
1362 pr_debug("%s: from = 0x%08x, len = %i\n", __func__
, (unsigned int)from
,
1365 /* Initialize return length value */
1368 if (mode
== MTD_OPS_AUTO_OOB
)
1369 oobsize
= this->ecclayout
->oobavail
;
1371 oobsize
= mtd
->oobsize
;
1373 column
= from
& (mtd
->oobsize
- 1);
1375 if (unlikely(column
>= oobsize
)) {
1376 printk(KERN_ERR
"%s: Attempted to start read outside oob\n",
1381 /* Do not allow reads past end of device */
1382 if (unlikely(from
>= mtd
->size
||
1383 column
+ len
> ((mtd
->size
>> this->page_shift
) -
1384 (from
>> this->page_shift
)) * oobsize
)) {
1385 printk(KERN_ERR
"%s: Attempted to read beyond end of device\n",
1390 stats
= mtd
->ecc_stats
;
1392 readcmd
= ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1394 while (read
< len
) {
1397 thislen
= oobsize
- column
;
1398 thislen
= min_t(int, thislen
, len
);
1400 this->command(mtd
, readcmd
, from
, mtd
->oobsize
);
1402 onenand_update_bufferram(mtd
, from
, 0);
1404 ret
= this->wait(mtd
, FL_READING
);
1406 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1408 if (ret
&& !mtd_is_eccerr(ret
)) {
1409 printk(KERN_ERR
"%s: read failed = 0x%x\n",
1414 if (mode
== MTD_OPS_AUTO_OOB
)
1415 onenand_transfer_auto_oob(mtd
, buf
, column
, thislen
);
1417 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
1429 from
+= mtd
->writesize
;
1434 ops
->oobretlen
= read
;
1439 if (mtd
->ecc_stats
.failed
- stats
.failed
)
1446 * onenand_read - [MTD Interface] Read data from flash
1447 * @param mtd MTD device structure
1448 * @param from offset to read from
1449 * @param len number of bytes to read
1450 * @param retlen pointer to variable to store the number of read bytes
1451 * @param buf the databuffer to put data
1455 static int onenand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1456 size_t *retlen
, u_char
*buf
)
1458 struct onenand_chip
*this = mtd
->priv
;
1459 struct mtd_oob_ops ops
= {
1467 onenand_get_device(mtd
, FL_READING
);
1468 ret
= ONENAND_IS_4KB_PAGE(this) ?
1469 onenand_mlc_read_ops_nolock(mtd
, from
, &ops
) :
1470 onenand_read_ops_nolock(mtd
, from
, &ops
);
1471 onenand_release_device(mtd
);
1473 *retlen
= ops
.retlen
;
1478 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1479 * @param mtd: MTD device structure
1480 * @param from: offset to read from
1481 * @param ops: oob operation description structure
1483 * Read main and/or out-of-band
1485 static int onenand_read_oob(struct mtd_info
*mtd
, loff_t from
,
1486 struct mtd_oob_ops
*ops
)
1488 struct onenand_chip
*this = mtd
->priv
;
1491 switch (ops
->mode
) {
1492 case MTD_OPS_PLACE_OOB
:
1493 case MTD_OPS_AUTO_OOB
:
1496 /* Not implemented yet */
1501 onenand_get_device(mtd
, FL_READING
);
1503 ret
= ONENAND_IS_4KB_PAGE(this) ?
1504 onenand_mlc_read_ops_nolock(mtd
, from
, ops
) :
1505 onenand_read_ops_nolock(mtd
, from
, ops
);
1507 ret
= onenand_read_oob_nolock(mtd
, from
, ops
);
1508 onenand_release_device(mtd
);
1514 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1515 * @param mtd MTD device structure
1516 * @param state state to select the max. timeout value
1518 * Wait for command done.
1520 static int onenand_bbt_wait(struct mtd_info
*mtd
, int state
)
1522 struct onenand_chip
*this = mtd
->priv
;
1523 unsigned long timeout
;
1524 unsigned int interrupt
, ctrl
, ecc
, addr1
, addr8
;
1526 /* The 20 msec is enough */
1527 timeout
= jiffies
+ msecs_to_jiffies(20);
1528 while (time_before(jiffies
, timeout
)) {
1529 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1530 if (interrupt
& ONENAND_INT_MASTER
)
1533 /* To get correct interrupt status in timeout case */
1534 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1535 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
1536 addr1
= this->read_word(this->base
+ ONENAND_REG_START_ADDRESS1
);
1537 addr8
= this->read_word(this->base
+ ONENAND_REG_START_ADDRESS8
);
1539 if (interrupt
& ONENAND_INT_READ
) {
1540 ecc
= onenand_read_ecc(this);
1541 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
1542 printk(KERN_DEBUG
"%s: ecc 0x%04x ctrl 0x%04x "
1543 "intr 0x%04x addr1 %#x addr8 %#x\n",
1544 __func__
, ecc
, ctrl
, interrupt
, addr1
, addr8
);
1545 return ONENAND_BBT_READ_ECC_ERROR
;
1548 printk(KERN_ERR
"%s: read timeout! ctrl 0x%04x "
1549 "intr 0x%04x addr1 %#x addr8 %#x\n",
1550 __func__
, ctrl
, interrupt
, addr1
, addr8
);
1551 return ONENAND_BBT_READ_FATAL_ERROR
;
1554 /* Initial bad block case: 0x2400 or 0x0400 */
1555 if (ctrl
& ONENAND_CTRL_ERROR
) {
1556 printk(KERN_DEBUG
"%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1557 "addr8 %#x\n", __func__
, ctrl
, interrupt
, addr1
, addr8
);
1558 return ONENAND_BBT_READ_ERROR
;
1565 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1566 * @param mtd MTD device structure
1567 * @param from offset to read from
1568 * @param ops oob operation description structure
1570 * OneNAND read out-of-band data from the spare area for bbt scan
1572 int onenand_bbt_read_oob(struct mtd_info
*mtd
, loff_t from
,
1573 struct mtd_oob_ops
*ops
)
1575 struct onenand_chip
*this = mtd
->priv
;
1576 int read
= 0, thislen
, column
;
1577 int ret
= 0, readcmd
;
1578 size_t len
= ops
->ooblen
;
1579 u_char
*buf
= ops
->oobbuf
;
1581 pr_debug("%s: from = 0x%08x, len = %zi\n", __func__
, (unsigned int)from
,
1584 /* Initialize return value */
1587 /* Do not allow reads past end of device */
1588 if (unlikely((from
+ len
) > mtd
->size
)) {
1589 printk(KERN_ERR
"%s: Attempt read beyond end of device\n",
1591 return ONENAND_BBT_READ_FATAL_ERROR
;
1594 /* Grab the lock and see if the device is available */
1595 onenand_get_device(mtd
, FL_READING
);
1597 column
= from
& (mtd
->oobsize
- 1);
1599 readcmd
= ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1601 while (read
< len
) {
1604 thislen
= mtd
->oobsize
- column
;
1605 thislen
= min_t(int, thislen
, len
);
1607 this->command(mtd
, readcmd
, from
, mtd
->oobsize
);
1609 onenand_update_bufferram(mtd
, from
, 0);
1611 ret
= this->bbt_wait(mtd
, FL_READING
);
1613 ret
= onenand_recover_lsb(mtd
, from
, ret
);
1618 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
1627 /* Update Page size */
1628 from
+= this->writesize
;
1633 /* Deselect and wake up anyone waiting on the device */
1634 onenand_release_device(mtd
);
1636 ops
->oobretlen
= read
;
1640 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1642 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1643 * @param mtd MTD device structure
1644 * @param buf the databuffer to verify
1645 * @param to offset to read from
1647 static int onenand_verify_oob(struct mtd_info
*mtd
, const u_char
*buf
, loff_t to
)
1649 struct onenand_chip
*this = mtd
->priv
;
1650 u_char
*oob_buf
= this->oob_buf
;
1651 int status
, i
, readcmd
;
1653 readcmd
= ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ
: ONENAND_CMD_READOOB
;
1655 this->command(mtd
, readcmd
, to
, mtd
->oobsize
);
1656 onenand_update_bufferram(mtd
, to
, 0);
1657 status
= this->wait(mtd
, FL_READING
);
1661 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
1662 for (i
= 0; i
< mtd
->oobsize
; i
++)
1663 if (buf
[i
] != 0xFF && buf
[i
] != oob_buf
[i
])
1670 * onenand_verify - [GENERIC] verify the chip contents after a write
1671 * @param mtd MTD device structure
1672 * @param buf the databuffer to verify
1673 * @param addr offset to read from
1674 * @param len number of bytes to read and compare
1676 static int onenand_verify(struct mtd_info
*mtd
, const u_char
*buf
, loff_t addr
, size_t len
)
1678 struct onenand_chip
*this = mtd
->priv
;
1680 int thislen
, column
;
1682 column
= addr
& (this->writesize
- 1);
1685 thislen
= min_t(int, this->writesize
- column
, len
);
1687 this->command(mtd
, ONENAND_CMD_READ
, addr
, this->writesize
);
1689 onenand_update_bufferram(mtd
, addr
, 0);
1691 ret
= this->wait(mtd
, FL_READING
);
1695 onenand_update_bufferram(mtd
, addr
, 1);
1697 this->read_bufferram(mtd
, ONENAND_DATARAM
, this->verify_buf
, 0, mtd
->writesize
);
1699 if (memcmp(buf
, this->verify_buf
+ column
, thislen
))
1711 #define onenand_verify(...) (0)
1712 #define onenand_verify_oob(...) (0)
1715 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1717 static void onenand_panic_wait(struct mtd_info
*mtd
)
1719 struct onenand_chip
*this = mtd
->priv
;
1720 unsigned int interrupt
;
1723 for (i
= 0; i
< 2000; i
++) {
1724 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
1725 if (interrupt
& ONENAND_INT_MASTER
)
1732 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1733 * @param mtd MTD device structure
1734 * @param to offset to write to
1735 * @param len number of bytes to write
1736 * @param retlen pointer to variable to store the number of written bytes
1737 * @param buf the data to write
1741 static int onenand_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1742 size_t *retlen
, const u_char
*buf
)
1744 struct onenand_chip
*this = mtd
->priv
;
1745 int column
, subpage
;
1749 if (this->state
== FL_PM_SUSPENDED
)
1752 /* Wait for any existing operation to clear */
1753 onenand_panic_wait(mtd
);
1755 pr_debug("%s: to = 0x%08x, len = %i\n", __func__
, (unsigned int)to
,
1758 /* Reject writes, which are not page aligned */
1759 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1760 printk(KERN_ERR
"%s: Attempt to write not page aligned data\n",
1765 column
= to
& (mtd
->writesize
- 1);
1767 /* Loop until all data write */
1768 while (written
< len
) {
1769 int thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1770 u_char
*wbuf
= (u_char
*) buf
;
1772 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1774 /* Partial page write */
1775 subpage
= thislen
< mtd
->writesize
;
1777 memset(this->page_buf
, 0xff, mtd
->writesize
);
1778 memcpy(this->page_buf
+ column
, buf
, thislen
);
1779 wbuf
= this->page_buf
;
1782 this->write_bufferram(mtd
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1783 this->write_bufferram(mtd
, ONENAND_SPARERAM
, ffchars
, 0, mtd
->oobsize
);
1785 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->writesize
);
1787 onenand_panic_wait(mtd
);
1789 /* In partial page write we don't update bufferram */
1790 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
1791 if (ONENAND_IS_2PLANE(this)) {
1792 ONENAND_SET_BUFFERRAM1(this);
1793 onenand_update_bufferram(mtd
, to
+ this->writesize
, !ret
&& !subpage
);
1797 printk(KERN_ERR
"%s: write failed %d\n", __func__
, ret
);
1816 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
1817 * @param mtd MTD device structure
1818 * @param oob_buf oob buffer
1819 * @param buf source address
1820 * @param column oob offset to write to
1821 * @param thislen oob length to write
1823 static int onenand_fill_auto_oob(struct mtd_info
*mtd
, u_char
*oob_buf
,
1824 const u_char
*buf
, int column
, int thislen
)
1826 struct onenand_chip
*this = mtd
->priv
;
1827 struct nand_oobfree
*free
;
1828 int writecol
= column
;
1829 int writeend
= column
+ thislen
;
1833 free
= this->ecclayout
->oobfree
;
1834 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1835 if (writecol
>= lastgap
)
1836 writecol
+= free
->offset
- lastgap
;
1837 if (writeend
>= lastgap
)
1838 writeend
+= free
->offset
- lastgap
;
1839 lastgap
= free
->offset
+ free
->length
;
1841 free
= this->ecclayout
->oobfree
;
1842 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1843 int free_end
= free
->offset
+ free
->length
;
1844 if (free
->offset
< writeend
&& free_end
> writecol
) {
1845 int st
= max_t(int,free
->offset
,writecol
);
1846 int ed
= min_t(int,free_end
,writeend
);
1848 memcpy(oob_buf
+ st
, buf
, n
);
1850 } else if (column
== 0)
1857 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1858 * @param mtd MTD device structure
1859 * @param to offset to write to
1860 * @param ops oob operation description structure
1862 * Write main and/or oob with ECC
1864 static int onenand_write_ops_nolock(struct mtd_info
*mtd
, loff_t to
,
1865 struct mtd_oob_ops
*ops
)
1867 struct onenand_chip
*this = mtd
->priv
;
1868 int written
= 0, column
, thislen
= 0, subpage
= 0;
1869 int prev
= 0, prevlen
= 0, prev_subpage
= 0, first
= 1;
1870 int oobwritten
= 0, oobcolumn
, thisooblen
, oobsize
;
1871 size_t len
= ops
->len
;
1872 size_t ooblen
= ops
->ooblen
;
1873 const u_char
*buf
= ops
->datbuf
;
1874 const u_char
*oob
= ops
->oobbuf
;
1878 pr_debug("%s: to = 0x%08x, len = %i\n", __func__
, (unsigned int)to
,
1881 /* Initialize retlen, in case of early exit */
1885 /* Reject writes, which are not page aligned */
1886 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1887 printk(KERN_ERR
"%s: Attempt to write not page aligned data\n",
1892 /* Check zero length */
1896 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1897 oobsize
= this->ecclayout
->oobavail
;
1899 oobsize
= mtd
->oobsize
;
1901 oobcolumn
= to
& (mtd
->oobsize
- 1);
1903 column
= to
& (mtd
->writesize
- 1);
1905 /* Loop until all data write */
1907 if (written
< len
) {
1908 u_char
*wbuf
= (u_char
*) buf
;
1910 thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1911 thisooblen
= min_t(int, oobsize
- oobcolumn
, ooblen
- oobwritten
);
1915 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1917 /* Partial page write */
1918 subpage
= thislen
< mtd
->writesize
;
1920 memset(this->page_buf
, 0xff, mtd
->writesize
);
1921 memcpy(this->page_buf
+ column
, buf
, thislen
);
1922 wbuf
= this->page_buf
;
1925 this->write_bufferram(mtd
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1928 oobbuf
= this->oob_buf
;
1930 /* We send data to spare ram with oobsize
1931 * to prevent byte access */
1932 memset(oobbuf
, 0xff, mtd
->oobsize
);
1933 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
1934 onenand_fill_auto_oob(mtd
, oobbuf
, oob
, oobcolumn
, thisooblen
);
1936 memcpy(oobbuf
+ oobcolumn
, oob
, thisooblen
);
1938 oobwritten
+= thisooblen
;
1942 oobbuf
= (u_char
*) ffchars
;
1944 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1946 ONENAND_SET_NEXT_BUFFERRAM(this);
1949 * 2 PLANE, MLC, and Flex-OneNAND do not support
1950 * write-while-program feature.
1952 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first
) {
1953 ONENAND_SET_PREV_BUFFERRAM(this);
1955 ret
= this->wait(mtd
, FL_WRITING
);
1957 /* In partial page write we don't update bufferram */
1958 onenand_update_bufferram(mtd
, prev
, !ret
&& !prev_subpage
);
1961 printk(KERN_ERR
"%s: write failed %d\n",
1966 if (written
== len
) {
1967 /* Only check verify write turn on */
1968 ret
= onenand_verify(mtd
, buf
- len
, to
- len
, len
);
1970 printk(KERN_ERR
"%s: verify failed %d\n",
1975 ONENAND_SET_NEXT_BUFFERRAM(this);
1979 cmd
= ONENAND_CMD_PROG
;
1981 /* Exclude 1st OTP and OTP blocks for cache program feature */
1982 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1983 likely(onenand_block(this, to
) != 0) &&
1984 ONENAND_IS_4KB_PAGE(this) &&
1985 ((written
+ thislen
) < len
)) {
1986 cmd
= ONENAND_CMD_2X_CACHE_PROG
;
1990 this->command(mtd
, cmd
, to
, mtd
->writesize
);
1993 * 2 PLANE, MLC, and Flex-OneNAND wait here
1995 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
1996 ret
= this->wait(mtd
, FL_WRITING
);
1998 /* In partial page write we don't update bufferram */
1999 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
2001 printk(KERN_ERR
"%s: write failed %d\n",
2006 /* Only check verify write turn on */
2007 ret
= onenand_verify(mtd
, buf
, to
, thislen
);
2009 printk(KERN_ERR
"%s: verify failed %d\n",
2023 prev_subpage
= subpage
;
2031 /* In error case, clear all bufferrams */
2033 onenand_invalidate_bufferram(mtd
, 0, -1);
2035 ops
->retlen
= written
;
2036 ops
->oobretlen
= oobwritten
;
2043 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
2044 * @param mtd MTD device structure
2045 * @param to offset to write to
2046 * @param len number of bytes to write
2047 * @param retlen pointer to variable to store the number of written bytes
2048 * @param buf the data to write
2049 * @param mode operation mode
2051 * OneNAND write out-of-band
2053 static int onenand_write_oob_nolock(struct mtd_info
*mtd
, loff_t to
,
2054 struct mtd_oob_ops
*ops
)
2056 struct onenand_chip
*this = mtd
->priv
;
2057 int column
, ret
= 0, oobsize
;
2058 int written
= 0, oobcmd
;
2060 size_t len
= ops
->ooblen
;
2061 const u_char
*buf
= ops
->oobbuf
;
2062 unsigned int mode
= ops
->mode
;
2066 pr_debug("%s: to = 0x%08x, len = %i\n", __func__
, (unsigned int)to
,
2069 /* Initialize retlen, in case of early exit */
2072 if (mode
== MTD_OPS_AUTO_OOB
)
2073 oobsize
= this->ecclayout
->oobavail
;
2075 oobsize
= mtd
->oobsize
;
2077 column
= to
& (mtd
->oobsize
- 1);
2079 if (unlikely(column
>= oobsize
)) {
2080 printk(KERN_ERR
"%s: Attempted to start write outside oob\n",
2085 /* For compatibility with NAND: Do not allow write past end of page */
2086 if (unlikely(column
+ len
> oobsize
)) {
2087 printk(KERN_ERR
"%s: Attempt to write past end of page\n",
2092 /* Do not allow reads past end of device */
2093 if (unlikely(to
>= mtd
->size
||
2094 column
+ len
> ((mtd
->size
>> this->page_shift
) -
2095 (to
>> this->page_shift
)) * oobsize
)) {
2096 printk(KERN_ERR
"%s: Attempted to write past end of device\n",
2101 oobbuf
= this->oob_buf
;
2103 oobcmd
= ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG
: ONENAND_CMD_PROGOOB
;
2105 /* Loop until all data write */
2106 while (written
< len
) {
2107 int thislen
= min_t(int, oobsize
, len
- written
);
2111 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobsize
);
2113 /* We send data to spare ram with oobsize
2114 * to prevent byte access */
2115 memset(oobbuf
, 0xff, mtd
->oobsize
);
2116 if (mode
== MTD_OPS_AUTO_OOB
)
2117 onenand_fill_auto_oob(mtd
, oobbuf
, buf
, column
, thislen
);
2119 memcpy(oobbuf
+ column
, buf
, thislen
);
2120 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
2122 if (ONENAND_IS_4KB_PAGE(this)) {
2123 /* Set main area of DataRAM to 0xff*/
2124 memset(this->page_buf
, 0xff, mtd
->writesize
);
2125 this->write_bufferram(mtd
, ONENAND_DATARAM
,
2126 this->page_buf
, 0, mtd
->writesize
);
2129 this->command(mtd
, oobcmd
, to
, mtd
->oobsize
);
2131 onenand_update_bufferram(mtd
, to
, 0);
2132 if (ONENAND_IS_2PLANE(this)) {
2133 ONENAND_SET_BUFFERRAM1(this);
2134 onenand_update_bufferram(mtd
, to
+ this->writesize
, 0);
2137 ret
= this->wait(mtd
, FL_WRITING
);
2139 printk(KERN_ERR
"%s: write failed %d\n", __func__
, ret
);
2143 ret
= onenand_verify_oob(mtd
, oobbuf
, to
);
2145 printk(KERN_ERR
"%s: verify failed %d\n",
2154 to
+= mtd
->writesize
;
2159 ops
->oobretlen
= written
;
2165 * onenand_write - [MTD Interface] write buffer to FLASH
2166 * @param mtd MTD device structure
2167 * @param to offset to write to
2168 * @param len number of bytes to write
2169 * @param retlen pointer to variable to store the number of written bytes
2170 * @param buf the data to write
2174 static int onenand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2175 size_t *retlen
, const u_char
*buf
)
2177 struct mtd_oob_ops ops
= {
2180 .datbuf
= (u_char
*) buf
,
2185 onenand_get_device(mtd
, FL_WRITING
);
2186 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
2187 onenand_release_device(mtd
);
2189 *retlen
= ops
.retlen
;
2194 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2195 * @param mtd: MTD device structure
2196 * @param to: offset to write
2197 * @param ops: oob operation description structure
2199 static int onenand_write_oob(struct mtd_info
*mtd
, loff_t to
,
2200 struct mtd_oob_ops
*ops
)
2204 switch (ops
->mode
) {
2205 case MTD_OPS_PLACE_OOB
:
2206 case MTD_OPS_AUTO_OOB
:
2209 /* Not implemented yet */
2214 onenand_get_device(mtd
, FL_WRITING
);
2216 ret
= onenand_write_ops_nolock(mtd
, to
, ops
);
2218 ret
= onenand_write_oob_nolock(mtd
, to
, ops
);
2219 onenand_release_device(mtd
);
2225 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2226 * @param mtd MTD device structure
2227 * @param ofs offset from device start
2228 * @param allowbbt 1, if its allowed to access the bbt area
2230 * Check, if the block is bad. Either by reading the bad block table or
2231 * calling of the scan function.
2233 static int onenand_block_isbad_nolock(struct mtd_info
*mtd
, loff_t ofs
, int allowbbt
)
2235 struct onenand_chip
*this = mtd
->priv
;
2236 struct bbm_info
*bbm
= this->bbm
;
2238 /* Return info from the table */
2239 return bbm
->isbad_bbt(mtd
, ofs
, allowbbt
);
2243 static int onenand_multiblock_erase_verify(struct mtd_info
*mtd
,
2244 struct erase_info
*instr
)
2246 struct onenand_chip
*this = mtd
->priv
;
2247 loff_t addr
= instr
->addr
;
2248 int len
= instr
->len
;
2249 unsigned int block_size
= (1 << this->erase_shift
);
2253 this->command(mtd
, ONENAND_CMD_ERASE_VERIFY
, addr
, block_size
);
2254 ret
= this->wait(mtd
, FL_VERIFYING_ERASE
);
2256 printk(KERN_ERR
"%s: Failed verify, block %d\n",
2257 __func__
, onenand_block(this, addr
));
2258 instr
->state
= MTD_ERASE_FAILED
;
2259 instr
->fail_addr
= addr
;
2269 * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
2270 * @param mtd MTD device structure
2271 * @param instr erase instruction
2272 * @param region erase region
2274 * Erase one or more blocks up to 64 block at a time
2276 static int onenand_multiblock_erase(struct mtd_info
*mtd
,
2277 struct erase_info
*instr
,
2278 unsigned int block_size
)
2280 struct onenand_chip
*this = mtd
->priv
;
2281 loff_t addr
= instr
->addr
;
2282 int len
= instr
->len
;
2287 instr
->state
= MTD_ERASING
;
2289 if (ONENAND_IS_DDP(this)) {
2290 loff_t bdry_addr
= this->chipsize
>> 1;
2291 if (addr
< bdry_addr
&& (addr
+ len
) > bdry_addr
)
2292 bdry_block
= bdry_addr
>> this->erase_shift
;
2297 /* Check if we have a bad block, we do not erase bad blocks */
2298 if (onenand_block_isbad_nolock(mtd
, addr
, 0)) {
2299 printk(KERN_WARNING
"%s: attempt to erase a bad block "
2300 "at addr 0x%012llx\n",
2301 __func__
, (unsigned long long) addr
);
2302 instr
->state
= MTD_ERASE_FAILED
;
2312 /* loop over 64 eb batches */
2314 struct erase_info verify_instr
= *instr
;
2315 int max_eb_count
= MB_ERASE_MAX_BLK_COUNT
;
2317 verify_instr
.addr
= addr
;
2318 verify_instr
.len
= 0;
2320 /* do not cross chip boundary */
2322 int this_block
= (addr
>> this->erase_shift
);
2324 if (this_block
< bdry_block
) {
2325 max_eb_count
= min(max_eb_count
,
2326 (bdry_block
- this_block
));
2332 while (len
> block_size
&& eb_count
< (max_eb_count
- 1)) {
2333 this->command(mtd
, ONENAND_CMD_MULTIBLOCK_ERASE
,
2335 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
2337 ret
= this->wait(mtd
, FL_PREPARING_ERASE
);
2339 printk(KERN_ERR
"%s: Failed multiblock erase, "
2340 "block %d\n", __func__
,
2341 onenand_block(this, addr
));
2342 instr
->state
= MTD_ERASE_FAILED
;
2343 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
2352 /* last block of 64-eb series */
2354 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
2355 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
2357 ret
= this->wait(mtd
, FL_ERASING
);
2358 /* Check if it is write protected */
2360 printk(KERN_ERR
"%s: Failed erase, block %d\n",
2361 __func__
, onenand_block(this, addr
));
2362 instr
->state
= MTD_ERASE_FAILED
;
2363 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
2372 verify_instr
.len
= eb_count
* block_size
;
2373 if (onenand_multiblock_erase_verify(mtd
, &verify_instr
)) {
2374 instr
->state
= verify_instr
.state
;
2375 instr
->fail_addr
= verify_instr
.fail_addr
;
2385 * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
2386 * @param mtd MTD device structure
2387 * @param instr erase instruction
2388 * @param region erase region
2389 * @param block_size erase block size
2391 * Erase one or more blocks one block at a time
2393 static int onenand_block_by_block_erase(struct mtd_info
*mtd
,
2394 struct erase_info
*instr
,
2395 struct mtd_erase_region_info
*region
,
2396 unsigned int block_size
)
2398 struct onenand_chip
*this = mtd
->priv
;
2399 loff_t addr
= instr
->addr
;
2400 int len
= instr
->len
;
2401 loff_t region_end
= 0;
2405 /* region is set for Flex-OneNAND */
2406 region_end
= region
->offset
+ region
->erasesize
* region
->numblocks
;
2409 instr
->state
= MTD_ERASING
;
2411 /* Loop through the blocks */
2415 /* Check if we have a bad block, we do not erase bad blocks */
2416 if (onenand_block_isbad_nolock(mtd
, addr
, 0)) {
2417 printk(KERN_WARNING
"%s: attempt to erase a bad block "
2418 "at addr 0x%012llx\n",
2419 __func__
, (unsigned long long) addr
);
2420 instr
->state
= MTD_ERASE_FAILED
;
2424 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
2426 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
2428 ret
= this->wait(mtd
, FL_ERASING
);
2429 /* Check, if it is write protected */
2431 printk(KERN_ERR
"%s: Failed erase, block %d\n",
2432 __func__
, onenand_block(this, addr
));
2433 instr
->state
= MTD_ERASE_FAILED
;
2434 instr
->fail_addr
= addr
;
2441 if (region
&& addr
== region_end
) {
2446 block_size
= region
->erasesize
;
2447 region_end
= region
->offset
+ region
->erasesize
* region
->numblocks
;
2449 if (len
& (block_size
- 1)) {
2450 /* FIXME: This should be handled at MTD partitioning level. */
2451 printk(KERN_ERR
"%s: Unaligned address\n",
2461 * onenand_erase - [MTD Interface] erase block(s)
2462 * @param mtd MTD device structure
2463 * @param instr erase instruction
2465 * Erase one or more blocks
2467 static int onenand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
2469 struct onenand_chip
*this = mtd
->priv
;
2470 unsigned int block_size
;
2471 loff_t addr
= instr
->addr
;
2472 loff_t len
= instr
->len
;
2474 struct mtd_erase_region_info
*region
= NULL
;
2475 loff_t region_offset
= 0;
2477 pr_debug("%s: start=0x%012llx, len=%llu\n", __func__
,
2478 (unsigned long long)instr
->addr
,
2479 (unsigned long long)instr
->len
);
2481 if (FLEXONENAND(this)) {
2482 /* Find the eraseregion of this address */
2483 int i
= flexonenand_region(mtd
, addr
);
2485 region
= &mtd
->eraseregions
[i
];
2486 block_size
= region
->erasesize
;
2488 /* Start address within region must align on block boundary.
2489 * Erase region's start offset is always block start address.
2491 region_offset
= region
->offset
;
2493 block_size
= 1 << this->erase_shift
;
2495 /* Start address must align on block boundary */
2496 if (unlikely((addr
- region_offset
) & (block_size
- 1))) {
2497 printk(KERN_ERR
"%s: Unaligned address\n", __func__
);
2501 /* Length must align on block boundary */
2502 if (unlikely(len
& (block_size
- 1))) {
2503 printk(KERN_ERR
"%s: Length not block aligned\n", __func__
);
2507 /* Grab the lock and see if the device is available */
2508 onenand_get_device(mtd
, FL_ERASING
);
2510 if (ONENAND_IS_4KB_PAGE(this) || region
||
2511 instr
->len
< MB_ERASE_MIN_BLK_COUNT
* block_size
) {
2512 /* region is set for Flex-OneNAND (no mb erase) */
2513 ret
= onenand_block_by_block_erase(mtd
, instr
,
2514 region
, block_size
);
2516 ret
= onenand_multiblock_erase(mtd
, instr
, block_size
);
2519 /* Deselect and wake up anyone waiting on the device */
2520 onenand_release_device(mtd
);
2522 /* Do call back function */
2524 instr
->state
= MTD_ERASE_DONE
;
2525 mtd_erase_callback(instr
);
2532 * onenand_sync - [MTD Interface] sync
2533 * @param mtd MTD device structure
2535 * Sync is actually a wait for chip ready function
2537 static void onenand_sync(struct mtd_info
*mtd
)
2539 pr_debug("%s: called\n", __func__
);
2541 /* Grab the lock and see if the device is available */
2542 onenand_get_device(mtd
, FL_SYNCING
);
2544 /* Release it and go back */
2545 onenand_release_device(mtd
);
2549 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2550 * @param mtd MTD device structure
2551 * @param ofs offset relative to mtd start
2553 * Check whether the block is bad
2555 static int onenand_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
2559 /* Check for invalid offset */
2560 if (ofs
> mtd
->size
)
2563 onenand_get_device(mtd
, FL_READING
);
2564 ret
= onenand_block_isbad_nolock(mtd
, ofs
, 0);
2565 onenand_release_device(mtd
);
2570 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2571 * @param mtd MTD device structure
2572 * @param ofs offset from device start
2574 * This is the default implementation, which can be overridden by
2575 * a hardware specific driver.
2577 static int onenand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
2579 struct onenand_chip
*this = mtd
->priv
;
2580 struct bbm_info
*bbm
= this->bbm
;
2581 u_char buf
[2] = {0, 0};
2582 struct mtd_oob_ops ops
= {
2583 .mode
= MTD_OPS_PLACE_OOB
,
2590 /* Get block number */
2591 block
= onenand_block(this, ofs
);
2593 bbm
->bbt
[block
>> 2] |= 0x01 << ((block
& 0x03) << 1);
2595 /* We write two bytes, so we don't have to mess with 16-bit access */
2596 ofs
+= mtd
->oobsize
+ (bbm
->badblockpos
& ~0x01);
2597 /* FIXME : What to do when marking SLC block in partition
2598 * with MLC erasesize? For now, it is not advisable to
2599 * create partitions containing both SLC and MLC regions.
2601 return onenand_write_oob_nolock(mtd
, ofs
, &ops
);
2605 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2606 * @param mtd MTD device structure
2607 * @param ofs offset relative to mtd start
2609 * Mark the block as bad
2611 static int onenand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
2613 struct onenand_chip
*this = mtd
->priv
;
2616 ret
= onenand_block_isbad(mtd
, ofs
);
2618 /* If it was bad already, return success and do nothing */
2624 onenand_get_device(mtd
, FL_WRITING
);
2625 ret
= this->block_markbad(mtd
, ofs
);
2626 onenand_release_device(mtd
);
2631 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2632 * @param mtd MTD device structure
2633 * @param ofs offset relative to mtd start
2634 * @param len number of bytes to lock or unlock
2635 * @param cmd lock or unlock command
2637 * Lock or unlock one or more blocks
2639 static int onenand_do_lock_cmd(struct mtd_info
*mtd
, loff_t ofs
, size_t len
, int cmd
)
2641 struct onenand_chip
*this = mtd
->priv
;
2642 int start
, end
, block
, value
, status
;
2645 start
= onenand_block(this, ofs
);
2646 end
= onenand_block(this, ofs
+ len
) - 1;
2648 if (cmd
== ONENAND_CMD_LOCK
)
2649 wp_status_mask
= ONENAND_WP_LS
;
2651 wp_status_mask
= ONENAND_WP_US
;
2653 /* Continuous lock scheme */
2654 if (this->options
& ONENAND_HAS_CONT_LOCK
) {
2655 /* Set start block address */
2656 this->write_word(start
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2657 /* Set end block address */
2658 this->write_word(end
, this->base
+ ONENAND_REG_END_BLOCK_ADDRESS
);
2659 /* Write lock command */
2660 this->command(mtd
, cmd
, 0, 0);
2662 /* There's no return value */
2663 this->wait(mtd
, FL_LOCKING
);
2666 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2667 & ONENAND_CTRL_ONGO
)
2670 /* Check lock status */
2671 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2672 if (!(status
& wp_status_mask
))
2673 printk(KERN_ERR
"%s: wp status = 0x%x\n",
2679 /* Block lock scheme */
2680 for (block
= start
; block
< end
+ 1; block
++) {
2681 /* Set block address */
2682 value
= onenand_block_address(this, block
);
2683 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
2684 /* Select DataRAM for DDP */
2685 value
= onenand_bufferram_address(this, block
);
2686 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
2687 /* Set start block address */
2688 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2689 /* Write lock command */
2690 this->command(mtd
, cmd
, 0, 0);
2692 /* There's no return value */
2693 this->wait(mtd
, FL_LOCKING
);
2696 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2697 & ONENAND_CTRL_ONGO
)
2700 /* Check lock status */
2701 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2702 if (!(status
& wp_status_mask
))
2703 printk(KERN_ERR
"%s: block = %d, wp status = 0x%x\n",
2704 __func__
, block
, status
);
2711 * onenand_lock - [MTD Interface] Lock block(s)
2712 * @param mtd MTD device structure
2713 * @param ofs offset relative to mtd start
2714 * @param len number of bytes to unlock
2716 * Lock one or more blocks
2718 static int onenand_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2722 onenand_get_device(mtd
, FL_LOCKING
);
2723 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_LOCK
);
2724 onenand_release_device(mtd
);
2729 * onenand_unlock - [MTD Interface] Unlock block(s)
2730 * @param mtd MTD device structure
2731 * @param ofs offset relative to mtd start
2732 * @param len number of bytes to unlock
2734 * Unlock one or more blocks
2736 static int onenand_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2740 onenand_get_device(mtd
, FL_LOCKING
);
2741 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
2742 onenand_release_device(mtd
);
2747 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2748 * @param this onenand chip data structure
2752 static int onenand_check_lock_status(struct onenand_chip
*this)
2754 unsigned int value
, block
, status
;
2757 end
= this->chipsize
>> this->erase_shift
;
2758 for (block
= 0; block
< end
; block
++) {
2759 /* Set block address */
2760 value
= onenand_block_address(this, block
);
2761 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
2762 /* Select DataRAM for DDP */
2763 value
= onenand_bufferram_address(this, block
);
2764 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
2765 /* Set start block address */
2766 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2768 /* Check lock status */
2769 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
2770 if (!(status
& ONENAND_WP_US
)) {
2771 printk(KERN_ERR
"%s: block = %d, wp status = 0x%x\n",
2772 __func__
, block
, status
);
2781 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2782 * @param mtd MTD device structure
2786 static void onenand_unlock_all(struct mtd_info
*mtd
)
2788 struct onenand_chip
*this = mtd
->priv
;
2790 loff_t len
= mtd
->size
;
2792 if (this->options
& ONENAND_HAS_UNLOCK_ALL
) {
2793 /* Set start block address */
2794 this->write_word(0, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
2795 /* Write unlock command */
2796 this->command(mtd
, ONENAND_CMD_UNLOCK_ALL
, 0, 0);
2798 /* There's no return value */
2799 this->wait(mtd
, FL_LOCKING
);
2802 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
2803 & ONENAND_CTRL_ONGO
)
2806 /* Don't check lock status */
2807 if (this->options
& ONENAND_SKIP_UNLOCK_CHECK
)
2810 /* Check lock status */
2811 if (onenand_check_lock_status(this))
2814 /* Workaround for all block unlock in DDP */
2815 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2816 /* All blocks on another chip */
2817 ofs
= this->chipsize
>> 1;
2818 len
= this->chipsize
>> 1;
2822 onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
2825 #ifdef CONFIG_MTD_ONENAND_OTP
2828 * onenand_otp_command - Send OTP specific command to OneNAND device
2829 * @param mtd MTD device structure
2830 * @param cmd the command to be sent
2831 * @param addr offset to read from or write to
2832 * @param len number of bytes to read or write
2834 static int onenand_otp_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
,
2837 struct onenand_chip
*this = mtd
->priv
;
2838 int value
, block
, page
;
2840 /* Address translation */
2842 case ONENAND_CMD_OTP_ACCESS
:
2843 block
= (int) (addr
>> this->erase_shift
);
2848 block
= (int) (addr
>> this->erase_shift
);
2849 page
= (int) (addr
>> this->page_shift
);
2851 if (ONENAND_IS_2PLANE(this)) {
2852 /* Make the even block number */
2854 /* Is it the odd plane? */
2855 if (addr
& this->writesize
)
2859 page
&= this->page_mask
;
2864 /* Write 'DFS, FBA' of Flash */
2865 value
= onenand_block_address(this, block
);
2866 this->write_word(value
, this->base
+
2867 ONENAND_REG_START_ADDRESS1
);
2871 /* Now we use page size operation */
2872 int sectors
= 4, count
= 4;
2877 if (ONENAND_IS_2PLANE(this) && cmd
== ONENAND_CMD_PROG
)
2878 cmd
= ONENAND_CMD_2X_PROG
;
2879 dataram
= ONENAND_CURRENT_BUFFERRAM(this);
2883 /* Write 'FPA, FSA' of Flash */
2884 value
= onenand_page_address(page
, sectors
);
2885 this->write_word(value
, this->base
+
2886 ONENAND_REG_START_ADDRESS8
);
2888 /* Write 'BSA, BSC' of DataRAM */
2889 value
= onenand_buffer_address(dataram
, sectors
, count
);
2890 this->write_word(value
, this->base
+ ONENAND_REG_START_BUFFER
);
2893 /* Interrupt clear */
2894 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
2897 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
2903 * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
2904 * @param mtd MTD device structure
2905 * @param to offset to write to
2906 * @param len number of bytes to write
2907 * @param retlen pointer to variable to store the number of written bytes
2908 * @param buf the data to write
2910 * OneNAND write out-of-band only for OTP
2912 static int onenand_otp_write_oob_nolock(struct mtd_info
*mtd
, loff_t to
,
2913 struct mtd_oob_ops
*ops
)
2915 struct onenand_chip
*this = mtd
->priv
;
2916 int column
, ret
= 0, oobsize
;
2919 size_t len
= ops
->ooblen
;
2920 const u_char
*buf
= ops
->oobbuf
;
2921 int block
, value
, status
;
2925 /* Initialize retlen, in case of early exit */
2928 oobsize
= mtd
->oobsize
;
2930 column
= to
& (mtd
->oobsize
- 1);
2932 oobbuf
= this->oob_buf
;
2934 /* Loop until all data write */
2935 while (written
< len
) {
2936 int thislen
= min_t(int, oobsize
, len
- written
);
2940 block
= (int) (to
>> this->erase_shift
);
2942 * Write 'DFS, FBA' of Flash
2943 * Add: F100h DQ=DFS, FBA
2946 value
= onenand_block_address(this, block
);
2947 this->write_word(value
, this->base
+
2948 ONENAND_REG_START_ADDRESS1
);
2951 * Select DataRAM for DDP
2955 value
= onenand_bufferram_address(this, block
);
2956 this->write_word(value
, this->base
+
2957 ONENAND_REG_START_ADDRESS2
);
2958 ONENAND_SET_NEXT_BUFFERRAM(this);
2961 * Enter OTP access mode
2963 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
2964 this->wait(mtd
, FL_OTPING
);
2966 /* We send data to spare ram with oobsize
2967 * to prevent byte access */
2968 memcpy(oobbuf
+ column
, buf
, thislen
);
2971 * Write Data into DataRAM
2973 * in sector0/spare/page0
2976 this->write_bufferram(mtd
, ONENAND_SPARERAM
,
2977 oobbuf
, 0, mtd
->oobsize
);
2979 onenand_otp_command(mtd
, ONENAND_CMD_PROGOOB
, to
, mtd
->oobsize
);
2980 onenand_update_bufferram(mtd
, to
, 0);
2981 if (ONENAND_IS_2PLANE(this)) {
2982 ONENAND_SET_BUFFERRAM1(this);
2983 onenand_update_bufferram(mtd
, to
+ this->writesize
, 0);
2986 ret
= this->wait(mtd
, FL_WRITING
);
2988 printk(KERN_ERR
"%s: write failed %d\n", __func__
, ret
);
2992 /* Exit OTP access mode */
2993 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
2994 this->wait(mtd
, FL_RESETING
);
2996 status
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
2999 if (status
== 0x60) {
3000 printk(KERN_DEBUG
"\nBLOCK\tSTATUS\n");
3001 printk(KERN_DEBUG
"1st Block\tLOCKED\n");
3002 printk(KERN_DEBUG
"OTP Block\tLOCKED\n");
3003 } else if (status
== 0x20) {
3004 printk(KERN_DEBUG
"\nBLOCK\tSTATUS\n");
3005 printk(KERN_DEBUG
"1st Block\tLOCKED\n");
3006 printk(KERN_DEBUG
"OTP Block\tUN-LOCKED\n");
3007 } else if (status
== 0x40) {
3008 printk(KERN_DEBUG
"\nBLOCK\tSTATUS\n");
3009 printk(KERN_DEBUG
"1st Block\tUN-LOCKED\n");
3010 printk(KERN_DEBUG
"OTP Block\tLOCKED\n");
3012 printk(KERN_DEBUG
"Reboot to check\n");
3019 to
+= mtd
->writesize
;
3024 ops
->oobretlen
= written
;
3029 /* Internal OTP operation */
3030 typedef int (*otp_op_t
)(struct mtd_info
*mtd
, loff_t form
, size_t len
,
3031 size_t *retlen
, u_char
*buf
);
3034 * do_otp_read - [DEFAULT] Read OTP block area
3035 * @param mtd MTD device structure
3036 * @param from The offset to read
3037 * @param len number of bytes to read
3038 * @param retlen pointer to variable to store the number of readbytes
3039 * @param buf the databuffer to put/get data
3041 * Read OTP block area.
3043 static int do_otp_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
3044 size_t *retlen
, u_char
*buf
)
3046 struct onenand_chip
*this = mtd
->priv
;
3047 struct mtd_oob_ops ops
= {
3055 /* Enter OTP access mode */
3056 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
3057 this->wait(mtd
, FL_OTPING
);
3059 ret
= ONENAND_IS_4KB_PAGE(this) ?
3060 onenand_mlc_read_ops_nolock(mtd
, from
, &ops
) :
3061 onenand_read_ops_nolock(mtd
, from
, &ops
);
3063 /* Exit OTP access mode */
3064 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
3065 this->wait(mtd
, FL_RESETING
);
3071 * do_otp_write - [DEFAULT] Write OTP block area
3072 * @param mtd MTD device structure
3073 * @param to The offset to write
3074 * @param len number of bytes to write
3075 * @param retlen pointer to variable to store the number of write bytes
3076 * @param buf the databuffer to put/get data
3078 * Write OTP block area.
3080 static int do_otp_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
3081 size_t *retlen
, u_char
*buf
)
3083 struct onenand_chip
*this = mtd
->priv
;
3084 unsigned char *pbuf
= buf
;
3086 struct mtd_oob_ops ops
;
3088 /* Force buffer page aligned */
3089 if (len
< mtd
->writesize
) {
3090 memcpy(this->page_buf
, buf
, len
);
3091 memset(this->page_buf
+ len
, 0xff, mtd
->writesize
- len
);
3092 pbuf
= this->page_buf
;
3093 len
= mtd
->writesize
;
3096 /* Enter OTP access mode */
3097 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
3098 this->wait(mtd
, FL_OTPING
);
3104 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
3105 *retlen
= ops
.retlen
;
3107 /* Exit OTP access mode */
3108 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
3109 this->wait(mtd
, FL_RESETING
);
3115 * do_otp_lock - [DEFAULT] Lock OTP block area
3116 * @param mtd MTD device structure
3117 * @param from The offset to lock
3118 * @param len number of bytes to lock
3119 * @param retlen pointer to variable to store the number of lock bytes
3120 * @param buf the databuffer to put/get data
3122 * Lock OTP block area.
3124 static int do_otp_lock(struct mtd_info
*mtd
, loff_t from
, size_t len
,
3125 size_t *retlen
, u_char
*buf
)
3127 struct onenand_chip
*this = mtd
->priv
;
3128 struct mtd_oob_ops ops
;
3131 if (FLEXONENAND(this)) {
3133 /* Enter OTP access mode */
3134 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
3135 this->wait(mtd
, FL_OTPING
);
3137 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3138 * main area of page 49.
3140 ops
.len
= mtd
->writesize
;
3144 ret
= onenand_write_ops_nolock(mtd
, mtd
->writesize
* 49, &ops
);
3145 *retlen
= ops
.retlen
;
3147 /* Exit OTP access mode */
3148 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
3149 this->wait(mtd
, FL_RESETING
);
3151 ops
.mode
= MTD_OPS_PLACE_OOB
;
3155 ret
= onenand_otp_write_oob_nolock(mtd
, from
, &ops
);
3156 *retlen
= ops
.oobretlen
;
3163 * onenand_otp_walk - [DEFAULT] Handle OTP operation
3164 * @param mtd MTD device structure
3165 * @param from The offset to read/write
3166 * @param len number of bytes to read/write
3167 * @param retlen pointer to variable to store the number of read bytes
3168 * @param buf the databuffer to put/get data
3169 * @param action do given action
3170 * @param mode specify user and factory
3172 * Handle OTP operation.
3174 static int onenand_otp_walk(struct mtd_info
*mtd
, loff_t from
, size_t len
,
3175 size_t *retlen
, u_char
*buf
,
3176 otp_op_t action
, int mode
)
3178 struct onenand_chip
*this = mtd
->priv
;
3185 density
= onenand_get_density(this->device_id
);
3186 if (density
< ONENAND_DEVICE_DENSITY_512Mb
)
3191 if (mode
== MTD_OTP_FACTORY
) {
3192 from
+= mtd
->writesize
* otp_pages
;
3193 otp_pages
= ONENAND_PAGES_PER_BLOCK
- otp_pages
;
3196 /* Check User/Factory boundary */
3197 if (mode
== MTD_OTP_USER
) {
3198 if (mtd
->writesize
* otp_pages
< from
+ len
)
3201 if (mtd
->writesize
* otp_pages
< len
)
3205 onenand_get_device(mtd
, FL_OTPING
);
3206 while (len
> 0 && otp_pages
> 0) {
3207 if (!action
) { /* OTP Info functions */
3208 struct otp_info
*otpinfo
;
3210 len
-= sizeof(struct otp_info
);
3216 otpinfo
= (struct otp_info
*) buf
;
3217 otpinfo
->start
= from
;
3218 otpinfo
->length
= mtd
->writesize
;
3219 otpinfo
->locked
= 0;
3221 from
+= mtd
->writesize
;
3222 buf
+= sizeof(struct otp_info
);
3223 *retlen
+= sizeof(struct otp_info
);
3227 ret
= action(mtd
, from
, len
, &tmp_retlen
, buf
);
3231 *retlen
+= tmp_retlen
;
3238 onenand_release_device(mtd
);
3244 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3245 * @param mtd MTD device structure
3246 * @param buf the databuffer to put/get data
3247 * @param len number of bytes to read
3249 * Read factory OTP info.
3251 static int onenand_get_fact_prot_info(struct mtd_info
*mtd
,
3252 struct otp_info
*buf
, size_t len
)
3257 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_FACTORY
);
3259 return ret
? : retlen
;
3263 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3264 * @param mtd MTD device structure
3265 * @param from The offset to read
3266 * @param len number of bytes to read
3267 * @param retlen pointer to variable to store the number of read bytes
3268 * @param buf the databuffer to put/get data
3270 * Read factory OTP area.
3272 static int onenand_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
3273 size_t len
, size_t *retlen
, u_char
*buf
)
3275 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_FACTORY
);
3279 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3280 * @param mtd MTD device structure
3281 * @param buf the databuffer to put/get data
3282 * @param len number of bytes to read
3284 * Read user OTP info.
3286 static int onenand_get_user_prot_info(struct mtd_info
*mtd
,
3287 struct otp_info
*buf
, size_t len
)
3292 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_USER
);
3294 return ret
? : retlen
;
3298 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3299 * @param mtd MTD device structure
3300 * @param from The offset to read
3301 * @param len number of bytes to read
3302 * @param retlen pointer to variable to store the number of read bytes
3303 * @param buf the databuffer to put/get data
3305 * Read user OTP area.
3307 static int onenand_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
3308 size_t len
, size_t *retlen
, u_char
*buf
)
3310 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_USER
);
3314 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3315 * @param mtd MTD device structure
3316 * @param from The offset to write
3317 * @param len number of bytes to write
3318 * @param retlen pointer to variable to store the number of write bytes
3319 * @param buf the databuffer to put/get data
3321 * Write user OTP area.
3323 static int onenand_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
3324 size_t len
, size_t *retlen
, u_char
*buf
)
3326 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_write
, MTD_OTP_USER
);
3330 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3331 * @param mtd MTD device structure
3332 * @param from The offset to lock
3333 * @param len number of bytes to unlock
3335 * Write lock mark on spare area in page 0 in OTP block
3337 static int onenand_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
3340 struct onenand_chip
*this = mtd
->priv
;
3341 u_char
*buf
= FLEXONENAND(this) ? this->page_buf
: this->oob_buf
;
3344 unsigned int otp_lock_offset
= ONENAND_OTP_LOCK_OFFSET
;
3346 memset(buf
, 0xff, FLEXONENAND(this) ? this->writesize
3349 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3350 * We write 16 bytes spare area instead of 2 bytes.
3351 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3352 * main area of page 49.
3356 len
= FLEXONENAND(this) ? mtd
->writesize
: 16;
3359 * Note: OTP lock operation
3360 * OTP block : 0xXXFC XX 1111 1100
3361 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3362 * Both : 0xXXF0 (If chip support) XX 1111 0000
3364 if (FLEXONENAND(this))
3365 otp_lock_offset
= FLEXONENAND_OTP_LOCK_OFFSET
;
3367 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3369 buf
[otp_lock_offset
] = 0xFC;
3371 buf
[otp_lock_offset
] = 0xF3;
3373 buf
[otp_lock_offset
] = 0xF0;
3375 printk(KERN_DEBUG
"[OneNAND] Invalid option selected for OTP\n");
3377 ret
= onenand_otp_walk(mtd
, from
, len
, &retlen
, buf
, do_otp_lock
, MTD_OTP_USER
);
3379 return ret
? : retlen
;
3382 #endif /* CONFIG_MTD_ONENAND_OTP */
3385 * onenand_check_features - Check and set OneNAND features
3386 * @param mtd MTD data structure
3388 * Check and set OneNAND features
3392 static void onenand_check_features(struct mtd_info
*mtd
)
3394 struct onenand_chip
*this = mtd
->priv
;
3395 unsigned int density
, process
, numbufs
;
3397 /* Lock scheme depends on density and process */
3398 density
= onenand_get_density(this->device_id
);
3399 process
= this->version_id
>> ONENAND_VERSION_PROCESS_SHIFT
;
3400 numbufs
= this->read_word(this->base
+ ONENAND_REG_NUM_BUFFERS
) >> 8;
3404 case ONENAND_DEVICE_DENSITY_4Gb
:
3405 if (ONENAND_IS_DDP(this))
3406 this->options
|= ONENAND_HAS_2PLANE
;
3407 else if (numbufs
== 1) {
3408 this->options
|= ONENAND_HAS_4KB_PAGE
;
3409 this->options
|= ONENAND_HAS_CACHE_PROGRAM
;
3411 * There are two different 4KiB pagesize chips
3412 * and no way to detect it by H/W config values.
3414 * To detect the correct NOP for each chips,
3415 * It should check the version ID as workaround.
3417 * Now it has as following
3418 * KFM4G16Q4M has NOP 4 with version ID 0x0131
3419 * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
3421 if ((this->version_id
& 0xf) == 0xe)
3422 this->options
|= ONENAND_HAS_NOP_1
;
3425 case ONENAND_DEVICE_DENSITY_2Gb
:
3426 /* 2Gb DDP does not have 2 plane */
3427 if (!ONENAND_IS_DDP(this))
3428 this->options
|= ONENAND_HAS_2PLANE
;
3429 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
3431 case ONENAND_DEVICE_DENSITY_1Gb
:
3432 /* A-Die has all block unlock */
3434 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
3438 /* Some OneNAND has continuous lock scheme */
3440 this->options
|= ONENAND_HAS_CONT_LOCK
;
3444 /* The MLC has 4KiB pagesize. */
3445 if (ONENAND_IS_MLC(this))
3446 this->options
|= ONENAND_HAS_4KB_PAGE
;
3448 if (ONENAND_IS_4KB_PAGE(this))
3449 this->options
&= ~ONENAND_HAS_2PLANE
;
3451 if (FLEXONENAND(this)) {
3452 this->options
&= ~ONENAND_HAS_CONT_LOCK
;
3453 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
3456 if (this->options
& ONENAND_HAS_CONT_LOCK
)
3457 printk(KERN_DEBUG
"Lock scheme is Continuous Lock\n");
3458 if (this->options
& ONENAND_HAS_UNLOCK_ALL
)
3459 printk(KERN_DEBUG
"Chip support all block unlock\n");
3460 if (this->options
& ONENAND_HAS_2PLANE
)
3461 printk(KERN_DEBUG
"Chip has 2 plane\n");
3462 if (this->options
& ONENAND_HAS_4KB_PAGE
)
3463 printk(KERN_DEBUG
"Chip has 4KiB pagesize\n");
3464 if (this->options
& ONENAND_HAS_CACHE_PROGRAM
)
3465 printk(KERN_DEBUG
"Chip has cache program feature\n");
3469 * onenand_print_device_info - Print device & version ID
3470 * @param device device ID
3471 * @param version version ID
3473 * Print device & version ID
3475 static void onenand_print_device_info(int device
, int version
)
3477 int vcc
, demuxed
, ddp
, density
, flexonenand
;
3479 vcc
= device
& ONENAND_DEVICE_VCC_MASK
;
3480 demuxed
= device
& ONENAND_DEVICE_IS_DEMUX
;
3481 ddp
= device
& ONENAND_DEVICE_IS_DDP
;
3482 density
= onenand_get_density(device
);
3483 flexonenand
= device
& DEVICE_IS_FLEXONENAND
;
3484 printk(KERN_INFO
"%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3485 demuxed
? "" : "Muxed ",
3486 flexonenand
? "Flex-" : "",
3489 vcc
? "2.65/3.3" : "1.8",
3491 printk(KERN_INFO
"OneNAND version = 0x%04x\n", version
);
3494 static const struct onenand_manufacturers onenand_manuf_ids
[] = {
3495 {ONENAND_MFR_SAMSUNG
, "Samsung"},
3496 {ONENAND_MFR_NUMONYX
, "Numonyx"},
3500 * onenand_check_maf - Check manufacturer ID
3501 * @param manuf manufacturer ID
3503 * Check manufacturer ID
3505 static int onenand_check_maf(int manuf
)
3507 int size
= ARRAY_SIZE(onenand_manuf_ids
);
3511 for (i
= 0; i
< size
; i
++)
3512 if (manuf
== onenand_manuf_ids
[i
].id
)
3516 name
= onenand_manuf_ids
[i
].name
;
3520 printk(KERN_DEBUG
"OneNAND Manufacturer: %s (0x%0x)\n", name
, manuf
);
3526 * flexonenand_get_boundary - Reads the SLC boundary
3527 * @param onenand_info - onenand info structure
3529 static int flexonenand_get_boundary(struct mtd_info
*mtd
)
3531 struct onenand_chip
*this = mtd
->priv
;
3533 int ret
, syscfg
, locked
;
3536 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
3537 this->write_word((syscfg
| 0x0100), this->base
+ ONENAND_REG_SYS_CFG1
);
3539 for (die
= 0; die
< this->dies
; die
++) {
3540 this->command(mtd
, FLEXONENAND_CMD_PI_ACCESS
, die
, 0);
3541 this->wait(mtd
, FL_SYNCING
);
3543 this->command(mtd
, FLEXONENAND_CMD_READ_PI
, die
, 0);
3544 ret
= this->wait(mtd
, FL_READING
);
3546 bdry
= this->read_word(this->base
+ ONENAND_DATARAM
);
3547 if ((bdry
>> FLEXONENAND_PI_UNLOCK_SHIFT
) == 3)
3551 this->boundary
[die
] = bdry
& FLEXONENAND_PI_MASK
;
3553 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
3554 ret
= this->wait(mtd
, FL_RESETING
);
3556 printk(KERN_INFO
"Die %d boundary: %d%s\n", die
,
3557 this->boundary
[die
], locked
? "(Locked)" : "(Unlocked)");
3561 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
3566 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3567 * boundary[], diesize[], mtd->size, mtd->erasesize
3568 * @param mtd - MTD device structure
3570 static void flexonenand_get_size(struct mtd_info
*mtd
)
3572 struct onenand_chip
*this = mtd
->priv
;
3573 int die
, i
, eraseshift
, density
;
3574 int blksperdie
, maxbdry
;
3577 density
= onenand_get_density(this->device_id
);
3578 blksperdie
= ((loff_t
)(16 << density
) << 20) >> (this->erase_shift
);
3579 blksperdie
>>= ONENAND_IS_DDP(this) ? 1 : 0;
3580 maxbdry
= blksperdie
- 1;
3581 eraseshift
= this->erase_shift
- 1;
3583 mtd
->numeraseregions
= this->dies
<< 1;
3585 /* This fills up the device boundary */
3586 flexonenand_get_boundary(mtd
);
3589 for (; die
< this->dies
; die
++) {
3590 if (!die
|| this->boundary
[die
-1] != maxbdry
) {
3592 mtd
->eraseregions
[i
].offset
= ofs
;
3593 mtd
->eraseregions
[i
].erasesize
= 1 << eraseshift
;
3594 mtd
->eraseregions
[i
].numblocks
=
3595 this->boundary
[die
] + 1;
3596 ofs
+= mtd
->eraseregions
[i
].numblocks
<< eraseshift
;
3599 mtd
->numeraseregions
-= 1;
3600 mtd
->eraseregions
[i
].numblocks
+=
3601 this->boundary
[die
] + 1;
3602 ofs
+= (this->boundary
[die
] + 1) << (eraseshift
- 1);
3604 if (this->boundary
[die
] != maxbdry
) {
3606 mtd
->eraseregions
[i
].offset
= ofs
;
3607 mtd
->eraseregions
[i
].erasesize
= 1 << eraseshift
;
3608 mtd
->eraseregions
[i
].numblocks
= maxbdry
^
3609 this->boundary
[die
];
3610 ofs
+= mtd
->eraseregions
[i
].numblocks
<< eraseshift
;
3613 mtd
->numeraseregions
-= 1;
3616 /* Expose MLC erase size except when all blocks are SLC */
3617 mtd
->erasesize
= 1 << this->erase_shift
;
3618 if (mtd
->numeraseregions
== 1)
3619 mtd
->erasesize
>>= 1;
3621 printk(KERN_INFO
"Device has %d eraseregions\n", mtd
->numeraseregions
);
3622 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
3623 printk(KERN_INFO
"[offset: 0x%08x, erasesize: 0x%05x,"
3624 " numblocks: %04u]\n",
3625 (unsigned int) mtd
->eraseregions
[i
].offset
,
3626 mtd
->eraseregions
[i
].erasesize
,
3627 mtd
->eraseregions
[i
].numblocks
);
3629 for (die
= 0, mtd
->size
= 0; die
< this->dies
; die
++) {
3630 this->diesize
[die
] = (loff_t
)blksperdie
<< this->erase_shift
;
3631 this->diesize
[die
] -= (loff_t
)(this->boundary
[die
] + 1)
3632 << (this->erase_shift
- 1);
3633 mtd
->size
+= this->diesize
[die
];
3638 * flexonenand_check_blocks_erased - Check if blocks are erased
3639 * @param mtd_info - mtd info structure
3640 * @param start - first erase block to check
3641 * @param end - last erase block to check
3643 * Converting an unerased block from MLC to SLC
3644 * causes byte values to change. Since both data and its ECC
3645 * have changed, reads on the block give uncorrectable error.
3646 * This might lead to the block being detected as bad.
3648 * Avoid this by ensuring that the block to be converted is
3651 static int flexonenand_check_blocks_erased(struct mtd_info
*mtd
, int start
, int end
)
3653 struct onenand_chip
*this = mtd
->priv
;
3656 struct mtd_oob_ops ops
= {
3657 .mode
= MTD_OPS_PLACE_OOB
,
3659 .ooblen
= mtd
->oobsize
,
3661 .oobbuf
= this->oob_buf
,
3665 printk(KERN_DEBUG
"Check blocks from %d to %d\n", start
, end
);
3667 for (block
= start
; block
<= end
; block
++) {
3668 addr
= flexonenand_addr(this, block
);
3669 if (onenand_block_isbad_nolock(mtd
, addr
, 0))
3673 * Since main area write results in ECC write to spare,
3674 * it is sufficient to check only ECC bytes for change.
3676 ret
= onenand_read_oob_nolock(mtd
, addr
, &ops
);
3680 for (i
= 0; i
< mtd
->oobsize
; i
++)
3681 if (this->oob_buf
[i
] != 0xff)
3684 if (i
!= mtd
->oobsize
) {
3685 printk(KERN_WARNING
"%s: Block %d not erased.\n",
3695 * flexonenand_set_boundary - Writes the SLC boundary
3696 * @param mtd - mtd info structure
3698 static int flexonenand_set_boundary(struct mtd_info
*mtd
, int die
,
3699 int boundary
, int lock
)
3701 struct onenand_chip
*this = mtd
->priv
;
3702 int ret
, density
, blksperdie
, old
, new, thisboundary
;
3705 /* Change only once for SDP Flex-OneNAND */
3706 if (die
&& (!ONENAND_IS_DDP(this)))
3709 /* boundary value of -1 indicates no required change */
3710 if (boundary
< 0 || boundary
== this->boundary
[die
])
3713 density
= onenand_get_density(this->device_id
);
3714 blksperdie
= ((16 << density
) << 20) >> this->erase_shift
;
3715 blksperdie
>>= ONENAND_IS_DDP(this) ? 1 : 0;
3717 if (boundary
>= blksperdie
) {
3718 printk(KERN_ERR
"%s: Invalid boundary value. "
3719 "Boundary not changed.\n", __func__
);
3723 /* Check if converting blocks are erased */
3724 old
= this->boundary
[die
] + (die
* this->density_mask
);
3725 new = boundary
+ (die
* this->density_mask
);
3726 ret
= flexonenand_check_blocks_erased(mtd
, min(old
, new) + 1, max(old
, new));
3728 printk(KERN_ERR
"%s: Please erase blocks "
3729 "before boundary change\n", __func__
);
3733 this->command(mtd
, FLEXONENAND_CMD_PI_ACCESS
, die
, 0);
3734 this->wait(mtd
, FL_SYNCING
);
3736 /* Check is boundary is locked */
3737 this->command(mtd
, FLEXONENAND_CMD_READ_PI
, die
, 0);
3738 ret
= this->wait(mtd
, FL_READING
);
3740 thisboundary
= this->read_word(this->base
+ ONENAND_DATARAM
);
3741 if ((thisboundary
>> FLEXONENAND_PI_UNLOCK_SHIFT
) != 3) {
3742 printk(KERN_ERR
"%s: boundary locked\n", __func__
);
3747 printk(KERN_INFO
"Changing die %d boundary: %d%s\n",
3748 die
, boundary
, lock
? "(Locked)" : "(Unlocked)");
3750 addr
= die
? this->diesize
[0] : 0;
3752 boundary
&= FLEXONENAND_PI_MASK
;
3753 boundary
|= lock
? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT
);
3755 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, 0);
3756 ret
= this->wait(mtd
, FL_ERASING
);
3758 printk(KERN_ERR
"%s: Failed PI erase for Die %d\n",
3763 this->write_word(boundary
, this->base
+ ONENAND_DATARAM
);
3764 this->command(mtd
, ONENAND_CMD_PROG
, addr
, 0);
3765 ret
= this->wait(mtd
, FL_WRITING
);
3767 printk(KERN_ERR
"%s: Failed PI write for Die %d\n",
3772 this->command(mtd
, FLEXONENAND_CMD_PI_UPDATE
, die
, 0);
3773 ret
= this->wait(mtd
, FL_WRITING
);
3775 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_REG_COMMAND
);
3776 this->wait(mtd
, FL_RESETING
);
3778 /* Recalculate device size on boundary change*/
3779 flexonenand_get_size(mtd
);
3785 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3786 * @param mtd MTD device structure
3788 * OneNAND detection method:
3789 * Compare the values from command with ones from register
3791 static int onenand_chip_probe(struct mtd_info
*mtd
)
3793 struct onenand_chip
*this = mtd
->priv
;
3794 int bram_maf_id
, bram_dev_id
, maf_id
, dev_id
;
3797 /* Save system configuration 1 */
3798 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
3799 /* Clear Sync. Burst Read mode to read BootRAM */
3800 this->write_word((syscfg
& ~ONENAND_SYS_CFG1_SYNC_READ
& ~ONENAND_SYS_CFG1_SYNC_WRITE
), this->base
+ ONENAND_REG_SYS_CFG1
);
3802 /* Send the command for reading device ID from BootRAM */
3803 this->write_word(ONENAND_CMD_READID
, this->base
+ ONENAND_BOOTRAM
);
3805 /* Read manufacturer and device IDs from BootRAM */
3806 bram_maf_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x0);
3807 bram_dev_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x2);
3809 /* Reset OneNAND to read default register values */
3810 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_BOOTRAM
);
3812 this->wait(mtd
, FL_RESETING
);
3814 /* Restore system configuration 1 */
3815 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
3817 /* Check manufacturer ID */
3818 if (onenand_check_maf(bram_maf_id
))
3821 /* Read manufacturer and device IDs from Register */
3822 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
3823 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
3825 /* Check OneNAND device */
3826 if (maf_id
!= bram_maf_id
|| dev_id
!= bram_dev_id
)
3833 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3834 * @param mtd MTD device structure
3836 static int onenand_probe(struct mtd_info
*mtd
)
3838 struct onenand_chip
*this = mtd
->priv
;
3839 int maf_id
, dev_id
, ver_id
;
3843 ret
= this->chip_probe(mtd
);
3847 /* Read manufacturer and device IDs from Register */
3848 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
3849 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
3850 ver_id
= this->read_word(this->base
+ ONENAND_REG_VERSION_ID
);
3851 this->technology
= this->read_word(this->base
+ ONENAND_REG_TECHNOLOGY
);
3853 /* Flash device information */
3854 onenand_print_device_info(dev_id
, ver_id
);
3855 this->device_id
= dev_id
;
3856 this->version_id
= ver_id
;
3858 /* Check OneNAND features */
3859 onenand_check_features(mtd
);
3861 density
= onenand_get_density(dev_id
);
3862 if (FLEXONENAND(this)) {
3863 this->dies
= ONENAND_IS_DDP(this) ? 2 : 1;
3864 /* Maximum possible erase regions */
3865 mtd
->numeraseregions
= this->dies
<< 1;
3866 mtd
->eraseregions
= kzalloc(sizeof(struct mtd_erase_region_info
)
3867 * (this->dies
<< 1), GFP_KERNEL
);
3868 if (!mtd
->eraseregions
)
3873 * For Flex-OneNAND, chipsize represents maximum possible device size.
3874 * mtd->size represents the actual device size.
3876 this->chipsize
= (16 << density
) << 20;
3878 /* OneNAND page size & block size */
3879 /* The data buffer size is equal to page size */
3880 mtd
->writesize
= this->read_word(this->base
+ ONENAND_REG_DATA_BUFFER_SIZE
);
3881 /* We use the full BufferRAM */
3882 if (ONENAND_IS_4KB_PAGE(this))
3883 mtd
->writesize
<<= 1;
3885 mtd
->oobsize
= mtd
->writesize
>> 5;
3886 /* Pages per a block are always 64 in OneNAND */
3887 mtd
->erasesize
= mtd
->writesize
<< 6;
3889 * Flex-OneNAND SLC area has 64 pages per block.
3890 * Flex-OneNAND MLC area has 128 pages per block.
3891 * Expose MLC erase size to find erase_shift and page_mask.
3893 if (FLEXONENAND(this))
3894 mtd
->erasesize
<<= 1;
3896 this->erase_shift
= ffs(mtd
->erasesize
) - 1;
3897 this->page_shift
= ffs(mtd
->writesize
) - 1;
3898 this->page_mask
= (1 << (this->erase_shift
- this->page_shift
)) - 1;
3899 /* Set density mask. it is used for DDP */
3900 if (ONENAND_IS_DDP(this))
3901 this->density_mask
= this->chipsize
>> (this->erase_shift
+ 1);
3902 /* It's real page size */
3903 this->writesize
= mtd
->writesize
;
3905 /* REVISIT: Multichip handling */
3907 if (FLEXONENAND(this))
3908 flexonenand_get_size(mtd
);
3910 mtd
->size
= this->chipsize
;
3913 * We emulate the 4KiB page and 256KiB erase block size
3914 * But oobsize is still 64 bytes.
3915 * It is only valid if you turn on 2X program support,
3916 * Otherwise it will be ignored by compiler.
3918 if (ONENAND_IS_2PLANE(this)) {
3919 mtd
->writesize
<<= 1;
3920 mtd
->erasesize
<<= 1;
3927 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3928 * @param mtd MTD device structure
3930 static int onenand_suspend(struct mtd_info
*mtd
)
3932 return onenand_get_device(mtd
, FL_PM_SUSPENDED
);
3936 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3937 * @param mtd MTD device structure
3939 static void onenand_resume(struct mtd_info
*mtd
)
3941 struct onenand_chip
*this = mtd
->priv
;
3943 if (this->state
== FL_PM_SUSPENDED
)
3944 onenand_release_device(mtd
);
3946 printk(KERN_ERR
"%s: resume() called for the chip which is not "
3947 "in suspended state\n", __func__
);
3951 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3952 * @param mtd MTD device structure
3953 * @param maxchips Number of chips to scan for
3955 * This fills out all the not initialized function pointers
3956 * with the defaults.
3957 * The flash ID is read and the mtd/chip structures are
3958 * filled with the appropriate values.
3960 int onenand_scan(struct mtd_info
*mtd
, int maxchips
)
3963 struct onenand_chip
*this = mtd
->priv
;
3965 if (!this->read_word
)
3966 this->read_word
= onenand_readw
;
3967 if (!this->write_word
)
3968 this->write_word
= onenand_writew
;
3971 this->command
= onenand_command
;
3973 onenand_setup_wait(mtd
);
3974 if (!this->bbt_wait
)
3975 this->bbt_wait
= onenand_bbt_wait
;
3976 if (!this->unlock_all
)
3977 this->unlock_all
= onenand_unlock_all
;
3979 if (!this->chip_probe
)
3980 this->chip_probe
= onenand_chip_probe
;
3982 if (!this->read_bufferram
)
3983 this->read_bufferram
= onenand_read_bufferram
;
3984 if (!this->write_bufferram
)
3985 this->write_bufferram
= onenand_write_bufferram
;
3987 if (!this->block_markbad
)
3988 this->block_markbad
= onenand_default_block_markbad
;
3989 if (!this->scan_bbt
)
3990 this->scan_bbt
= onenand_default_bbt
;
3992 if (onenand_probe(mtd
))
3995 /* Set Sync. Burst Read after probing */
3996 if (this->mmcontrol
) {
3997 printk(KERN_INFO
"OneNAND Sync. Burst Read support\n");
3998 this->read_bufferram
= onenand_sync_read_bufferram
;
4001 /* Allocate buffers, if necessary */
4002 if (!this->page_buf
) {
4003 this->page_buf
= kzalloc(mtd
->writesize
, GFP_KERNEL
);
4004 if (!this->page_buf
) {
4005 printk(KERN_ERR
"%s: Can't allocate page_buf\n",
4009 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4010 this->verify_buf
= kzalloc(mtd
->writesize
, GFP_KERNEL
);
4011 if (!this->verify_buf
) {
4012 kfree(this->page_buf
);
4016 this->options
|= ONENAND_PAGEBUF_ALLOC
;
4018 if (!this->oob_buf
) {
4019 this->oob_buf
= kzalloc(mtd
->oobsize
, GFP_KERNEL
);
4020 if (!this->oob_buf
) {
4021 printk(KERN_ERR
"%s: Can't allocate oob_buf\n",
4023 if (this->options
& ONENAND_PAGEBUF_ALLOC
) {
4024 this->options
&= ~ONENAND_PAGEBUF_ALLOC
;
4025 kfree(this->page_buf
);
4029 this->options
|= ONENAND_OOBBUF_ALLOC
;
4032 this->state
= FL_READY
;
4033 init_waitqueue_head(&this->wq
);
4034 spin_lock_init(&this->chip_lock
);
4037 * Allow subpage writes up to oobsize.
4039 switch (mtd
->oobsize
) {
4041 if (FLEXONENAND(this)) {
4042 this->ecclayout
= &flexonenand_oob_128
;
4043 mtd
->subpage_sft
= 0;
4045 this->ecclayout
= &onenand_oob_128
;
4046 mtd
->subpage_sft
= 2;
4048 if (ONENAND_IS_NOP_1(this))
4049 mtd
->subpage_sft
= 0;
4052 this->ecclayout
= &onenand_oob_64
;
4053 mtd
->subpage_sft
= 2;
4057 this->ecclayout
= &onenand_oob_32
;
4058 mtd
->subpage_sft
= 1;
4062 printk(KERN_WARNING
"%s: No OOB scheme defined for oobsize %d\n",
4063 __func__
, mtd
->oobsize
);
4064 mtd
->subpage_sft
= 0;
4065 /* To prevent kernel oops */
4066 this->ecclayout
= &onenand_oob_32
;
4070 this->subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
4073 * The number of bytes available for a client to place data into
4074 * the out of band area
4076 this->ecclayout
->oobavail
= 0;
4077 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&&
4078 this->ecclayout
->oobfree
[i
].length
; i
++)
4079 this->ecclayout
->oobavail
+=
4080 this->ecclayout
->oobfree
[i
].length
;
4081 mtd
->oobavail
= this->ecclayout
->oobavail
;
4083 mtd
->ecclayout
= this->ecclayout
;
4084 mtd
->ecc_strength
= 1;
4086 /* Fill in remaining MTD driver data */
4087 mtd
->type
= ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH
: MTD_NANDFLASH
;
4088 mtd
->flags
= MTD_CAP_NANDFLASH
;
4089 mtd
->_erase
= onenand_erase
;
4091 mtd
->_unpoint
= NULL
;
4092 mtd
->_read
= onenand_read
;
4093 mtd
->_write
= onenand_write
;
4094 mtd
->_read_oob
= onenand_read_oob
;
4095 mtd
->_write_oob
= onenand_write_oob
;
4096 mtd
->_panic_write
= onenand_panic_write
;
4097 #ifdef CONFIG_MTD_ONENAND_OTP
4098 mtd
->_get_fact_prot_info
= onenand_get_fact_prot_info
;
4099 mtd
->_read_fact_prot_reg
= onenand_read_fact_prot_reg
;
4100 mtd
->_get_user_prot_info
= onenand_get_user_prot_info
;
4101 mtd
->_read_user_prot_reg
= onenand_read_user_prot_reg
;
4102 mtd
->_write_user_prot_reg
= onenand_write_user_prot_reg
;
4103 mtd
->_lock_user_prot_reg
= onenand_lock_user_prot_reg
;
4105 mtd
->_sync
= onenand_sync
;
4106 mtd
->_lock
= onenand_lock
;
4107 mtd
->_unlock
= onenand_unlock
;
4108 mtd
->_suspend
= onenand_suspend
;
4109 mtd
->_resume
= onenand_resume
;
4110 mtd
->_block_isbad
= onenand_block_isbad
;
4111 mtd
->_block_markbad
= onenand_block_markbad
;
4112 mtd
->owner
= THIS_MODULE
;
4113 mtd
->writebufsize
= mtd
->writesize
;
4115 /* Unlock whole block */
4116 if (!(this->options
& ONENAND_SKIP_INITIAL_UNLOCKING
))
4117 this->unlock_all(mtd
);
4119 ret
= this->scan_bbt(mtd
);
4120 if ((!FLEXONENAND(this)) || ret
)
4123 /* Change Flex-OneNAND boundaries if required */
4124 for (i
= 0; i
< MAX_DIES
; i
++)
4125 flexonenand_set_boundary(mtd
, i
, flex_bdry
[2 * i
],
4126 flex_bdry
[(2 * i
) + 1]);
4132 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
4133 * @param mtd MTD device structure
4135 void onenand_release(struct mtd_info
*mtd
)
4137 struct onenand_chip
*this = mtd
->priv
;
4139 /* Deregister partitions */
4140 mtd_device_unregister(mtd
);
4142 /* Free bad block table memory, if allocated */
4144 struct bbm_info
*bbm
= this->bbm
;
4148 /* Buffers allocated by onenand_scan */
4149 if (this->options
& ONENAND_PAGEBUF_ALLOC
) {
4150 kfree(this->page_buf
);
4151 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4152 kfree(this->verify_buf
);
4155 if (this->options
& ONENAND_OOBBUF_ALLOC
)
4156 kfree(this->oob_buf
);
4157 kfree(mtd
->eraseregions
);
4160 EXPORT_SYMBOL_GPL(onenand_scan
);
4161 EXPORT_SYMBOL_GPL(onenand_release
);
4163 MODULE_LICENSE("GPL");
4164 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4165 MODULE_DESCRIPTION("Generic OneNAND flash driver code");