1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
226777d37SBoris Brezillon /*
326777d37SBoris Brezillon * Copyright © 2005-2009 Samsung Electronics
426777d37SBoris Brezillon * Copyright © 2007 Nokia Corporation
526777d37SBoris Brezillon *
626777d37SBoris Brezillon * Kyungmin Park <kyungmin.park@samsung.com>
726777d37SBoris Brezillon *
826777d37SBoris Brezillon * Credits:
926777d37SBoris Brezillon * Adrian Hunter <ext-adrian.hunter@nokia.com>:
1026777d37SBoris Brezillon * auto-placement support, read-while load support, various fixes
1126777d37SBoris Brezillon *
1226777d37SBoris Brezillon * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
1326777d37SBoris Brezillon * Flex-OneNAND support
1426777d37SBoris Brezillon * Amul Kumar Saha <amul.saha at samsung.com>
1526777d37SBoris Brezillon * OTP support
1626777d37SBoris Brezillon */
1726777d37SBoris Brezillon
1826777d37SBoris Brezillon #include <linux/kernel.h>
1926777d37SBoris Brezillon #include <linux/module.h>
2026777d37SBoris Brezillon #include <linux/moduleparam.h>
2126777d37SBoris Brezillon #include <linux/slab.h>
2226777d37SBoris Brezillon #include <linux/sched.h>
2326777d37SBoris Brezillon #include <linux/delay.h>
2426777d37SBoris Brezillon #include <linux/interrupt.h>
2526777d37SBoris Brezillon #include <linux/jiffies.h>
2626777d37SBoris Brezillon #include <linux/mtd/mtd.h>
2726777d37SBoris Brezillon #include <linux/mtd/onenand.h>
2826777d37SBoris Brezillon #include <linux/mtd/partitions.h>
2926777d37SBoris Brezillon
3026777d37SBoris Brezillon #include <asm/io.h>
3126777d37SBoris Brezillon
3226777d37SBoris Brezillon /*
3326777d37SBoris Brezillon * Multiblock erase if number of blocks to erase is 2 or more.
3426777d37SBoris Brezillon * Maximum number of blocks for simultaneous erase is 64.
3526777d37SBoris Brezillon */
3626777d37SBoris Brezillon #define MB_ERASE_MIN_BLK_COUNT 2
3726777d37SBoris Brezillon #define MB_ERASE_MAX_BLK_COUNT 64
3826777d37SBoris Brezillon
3926777d37SBoris Brezillon /* Default Flex-OneNAND boundary and lock respectively */
4026777d37SBoris Brezillon static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
4126777d37SBoris Brezillon
4226777d37SBoris Brezillon module_param_array(flex_bdry, int, NULL, 0400);
4326777d37SBoris Brezillon MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
4426777d37SBoris Brezillon "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
4526777d37SBoris Brezillon "DIE_BDRY: SLC boundary of the die"
4626777d37SBoris Brezillon "LOCK: Locking information for SLC boundary"
4726777d37SBoris Brezillon " : 0->Set boundary in unlocked status"
4826777d37SBoris Brezillon " : 1->Set boundary in locked status");
4926777d37SBoris Brezillon
5026777d37SBoris Brezillon /* Default OneNAND/Flex-OneNAND OTP options*/
5126777d37SBoris Brezillon static int otp;
5226777d37SBoris Brezillon
5326777d37SBoris Brezillon module_param(otp, int, 0400);
5426777d37SBoris Brezillon MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
5526777d37SBoris Brezillon "Syntax : otp=LOCK_TYPE"
5626777d37SBoris Brezillon "LOCK_TYPE : Keys issued, for specific OTP Lock type"
5726777d37SBoris Brezillon " : 0 -> Default (No Blocks Locked)"
5826777d37SBoris Brezillon " : 1 -> OTP Block lock"
5926777d37SBoris Brezillon " : 2 -> 1st Block lock"
6026777d37SBoris Brezillon " : 3 -> BOTH OTP Block and 1st Block lock");
6126777d37SBoris Brezillon
6226777d37SBoris Brezillon /*
6326777d37SBoris Brezillon * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
6426777d37SBoris Brezillon * For now, we expose only 64 out of 80 ecc bytes
6526777d37SBoris Brezillon */
flexonenand_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)6626777d37SBoris Brezillon static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section,
6726777d37SBoris Brezillon struct mtd_oob_region *oobregion)
6826777d37SBoris Brezillon {
6926777d37SBoris Brezillon if (section > 7)
7026777d37SBoris Brezillon return -ERANGE;
7126777d37SBoris Brezillon
7226777d37SBoris Brezillon oobregion->offset = (section * 16) + 6;
7326777d37SBoris Brezillon oobregion->length = 10;
7426777d37SBoris Brezillon
7526777d37SBoris Brezillon return 0;
7626777d37SBoris Brezillon }
7726777d37SBoris Brezillon
flexonenand_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)7826777d37SBoris Brezillon static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section,
7926777d37SBoris Brezillon struct mtd_oob_region *oobregion)
8026777d37SBoris Brezillon {
8126777d37SBoris Brezillon if (section > 7)
8226777d37SBoris Brezillon return -ERANGE;
8326777d37SBoris Brezillon
8426777d37SBoris Brezillon oobregion->offset = (section * 16) + 2;
8526777d37SBoris Brezillon oobregion->length = 4;
8626777d37SBoris Brezillon
8726777d37SBoris Brezillon return 0;
8826777d37SBoris Brezillon }
8926777d37SBoris Brezillon
9026777d37SBoris Brezillon static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = {
9126777d37SBoris Brezillon .ecc = flexonenand_ooblayout_ecc,
9226777d37SBoris Brezillon .free = flexonenand_ooblayout_free,
9326777d37SBoris Brezillon };
9426777d37SBoris Brezillon
9526777d37SBoris Brezillon /*
9626777d37SBoris Brezillon * onenand_oob_128 - oob info for OneNAND with 4KB page
9726777d37SBoris Brezillon *
9826777d37SBoris Brezillon * Based on specification:
9926777d37SBoris Brezillon * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
10026777d37SBoris Brezillon *
10126777d37SBoris Brezillon */
onenand_ooblayout_128_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)10226777d37SBoris Brezillon static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section,
10326777d37SBoris Brezillon struct mtd_oob_region *oobregion)
10426777d37SBoris Brezillon {
10526777d37SBoris Brezillon if (section > 7)
10626777d37SBoris Brezillon return -ERANGE;
10726777d37SBoris Brezillon
10826777d37SBoris Brezillon oobregion->offset = (section * 16) + 7;
10926777d37SBoris Brezillon oobregion->length = 9;
11026777d37SBoris Brezillon
11126777d37SBoris Brezillon return 0;
11226777d37SBoris Brezillon }
11326777d37SBoris Brezillon
onenand_ooblayout_128_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)11426777d37SBoris Brezillon static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section,
11526777d37SBoris Brezillon struct mtd_oob_region *oobregion)
11626777d37SBoris Brezillon {
11726777d37SBoris Brezillon if (section >= 8)
11826777d37SBoris Brezillon return -ERANGE;
11926777d37SBoris Brezillon
12026777d37SBoris Brezillon /*
12126777d37SBoris Brezillon * free bytes are using the spare area fields marked as
12226777d37SBoris Brezillon * "Managed by internal ECC logic for Logical Sector Number area"
12326777d37SBoris Brezillon */
12426777d37SBoris Brezillon oobregion->offset = (section * 16) + 2;
12526777d37SBoris Brezillon oobregion->length = 3;
12626777d37SBoris Brezillon
12726777d37SBoris Brezillon return 0;
12826777d37SBoris Brezillon }
12926777d37SBoris Brezillon
13026777d37SBoris Brezillon static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = {
13126777d37SBoris Brezillon .ecc = onenand_ooblayout_128_ecc,
13226777d37SBoris Brezillon .free = onenand_ooblayout_128_free,
13326777d37SBoris Brezillon };
13426777d37SBoris Brezillon
1352dc3d1e0SLee Jones /*
13626777d37SBoris Brezillon * onenand_oob_32_64 - oob info for large (2KB) page
13726777d37SBoris Brezillon */
onenand_ooblayout_32_64_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)13826777d37SBoris Brezillon static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section,
13926777d37SBoris Brezillon struct mtd_oob_region *oobregion)
14026777d37SBoris Brezillon {
14126777d37SBoris Brezillon if (section > 3)
14226777d37SBoris Brezillon return -ERANGE;
14326777d37SBoris Brezillon
14426777d37SBoris Brezillon oobregion->offset = (section * 16) + 8;
14526777d37SBoris Brezillon oobregion->length = 5;
14626777d37SBoris Brezillon
14726777d37SBoris Brezillon return 0;
14826777d37SBoris Brezillon }
14926777d37SBoris Brezillon
onenand_ooblayout_32_64_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)15026777d37SBoris Brezillon static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section,
15126777d37SBoris Brezillon struct mtd_oob_region *oobregion)
15226777d37SBoris Brezillon {
15326777d37SBoris Brezillon int sections = (mtd->oobsize / 32) * 2;
15426777d37SBoris Brezillon
15526777d37SBoris Brezillon if (section >= sections)
15626777d37SBoris Brezillon return -ERANGE;
15726777d37SBoris Brezillon
15826777d37SBoris Brezillon if (section & 1) {
15926777d37SBoris Brezillon oobregion->offset = ((section - 1) * 16) + 14;
16026777d37SBoris Brezillon oobregion->length = 2;
16126777d37SBoris Brezillon } else {
16226777d37SBoris Brezillon oobregion->offset = (section * 16) + 2;
16326777d37SBoris Brezillon oobregion->length = 3;
16426777d37SBoris Brezillon }
16526777d37SBoris Brezillon
16626777d37SBoris Brezillon return 0;
16726777d37SBoris Brezillon }
16826777d37SBoris Brezillon
16926777d37SBoris Brezillon static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = {
17026777d37SBoris Brezillon .ecc = onenand_ooblayout_32_64_ecc,
17126777d37SBoris Brezillon .free = onenand_ooblayout_32_64_free,
17226777d37SBoris Brezillon };
17326777d37SBoris Brezillon
17426777d37SBoris Brezillon static const unsigned char ffchars[] = {
17526777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17626777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
17726777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17826777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
17926777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18026777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
18126777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18226777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
18326777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18426777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
18526777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18626777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
18726777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18826777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
18926777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
19026777d37SBoris Brezillon 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
19126777d37SBoris Brezillon };
19226777d37SBoris Brezillon
19326777d37SBoris Brezillon /**
19426777d37SBoris Brezillon * onenand_readw - [OneNAND Interface] Read OneNAND register
195720ae74dSLee Jones * @addr: address to read
19626777d37SBoris Brezillon *
19726777d37SBoris Brezillon * Read OneNAND register
19826777d37SBoris Brezillon */
onenand_readw(void __iomem * addr)19926777d37SBoris Brezillon static unsigned short onenand_readw(void __iomem *addr)
20026777d37SBoris Brezillon {
20126777d37SBoris Brezillon return readw(addr);
20226777d37SBoris Brezillon }
20326777d37SBoris Brezillon
20426777d37SBoris Brezillon /**
20526777d37SBoris Brezillon * onenand_writew - [OneNAND Interface] Write OneNAND register with value
206720ae74dSLee Jones * @value: value to write
207720ae74dSLee Jones * @addr: address to write
20826777d37SBoris Brezillon *
20926777d37SBoris Brezillon * Write OneNAND register with value
21026777d37SBoris Brezillon */
onenand_writew(unsigned short value,void __iomem * addr)21126777d37SBoris Brezillon static void onenand_writew(unsigned short value, void __iomem *addr)
21226777d37SBoris Brezillon {
21326777d37SBoris Brezillon writew(value, addr);
21426777d37SBoris Brezillon }
21526777d37SBoris Brezillon
21626777d37SBoris Brezillon /**
21726777d37SBoris Brezillon * onenand_block_address - [DEFAULT] Get block address
218720ae74dSLee Jones * @this: onenand chip data structure
219720ae74dSLee Jones * @block: the block
22026777d37SBoris Brezillon * @return translated block address if DDP, otherwise same
22126777d37SBoris Brezillon *
22226777d37SBoris Brezillon * Setup Start Address 1 Register (F100h)
22326777d37SBoris Brezillon */
onenand_block_address(struct onenand_chip * this,int block)22426777d37SBoris Brezillon static int onenand_block_address(struct onenand_chip *this, int block)
22526777d37SBoris Brezillon {
22626777d37SBoris Brezillon /* Device Flash Core select, NAND Flash Block Address */
22726777d37SBoris Brezillon if (block & this->density_mask)
22826777d37SBoris Brezillon return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
22926777d37SBoris Brezillon
23026777d37SBoris Brezillon return block;
23126777d37SBoris Brezillon }
23226777d37SBoris Brezillon
23326777d37SBoris Brezillon /**
23426777d37SBoris Brezillon * onenand_bufferram_address - [DEFAULT] Get bufferram address
235720ae74dSLee Jones * @this: onenand chip data structure
236720ae74dSLee Jones * @block: the block
23726777d37SBoris Brezillon * @return set DBS value if DDP, otherwise 0
23826777d37SBoris Brezillon *
23926777d37SBoris Brezillon * Setup Start Address 2 Register (F101h) for DDP
24026777d37SBoris Brezillon */
onenand_bufferram_address(struct onenand_chip * this,int block)24126777d37SBoris Brezillon static int onenand_bufferram_address(struct onenand_chip *this, int block)
24226777d37SBoris Brezillon {
24326777d37SBoris Brezillon /* Device BufferRAM Select */
24426777d37SBoris Brezillon if (block & this->density_mask)
24526777d37SBoris Brezillon return ONENAND_DDP_CHIP1;
24626777d37SBoris Brezillon
24726777d37SBoris Brezillon return ONENAND_DDP_CHIP0;
24826777d37SBoris Brezillon }
24926777d37SBoris Brezillon
25026777d37SBoris Brezillon /**
25126777d37SBoris Brezillon * onenand_page_address - [DEFAULT] Get page address
252720ae74dSLee Jones * @page: the page address
253720ae74dSLee Jones * @sector: the sector address
25426777d37SBoris Brezillon * @return combined page and sector address
25526777d37SBoris Brezillon *
25626777d37SBoris Brezillon * Setup Start Address 8 Register (F107h)
25726777d37SBoris Brezillon */
onenand_page_address(int page,int sector)25826777d37SBoris Brezillon static int onenand_page_address(int page, int sector)
25926777d37SBoris Brezillon {
26026777d37SBoris Brezillon /* Flash Page Address, Flash Sector Address */
26126777d37SBoris Brezillon int fpa, fsa;
26226777d37SBoris Brezillon
26326777d37SBoris Brezillon fpa = page & ONENAND_FPA_MASK;
26426777d37SBoris Brezillon fsa = sector & ONENAND_FSA_MASK;
26526777d37SBoris Brezillon
26626777d37SBoris Brezillon return ((fpa << ONENAND_FPA_SHIFT) | fsa);
26726777d37SBoris Brezillon }
26826777d37SBoris Brezillon
26926777d37SBoris Brezillon /**
27026777d37SBoris Brezillon * onenand_buffer_address - [DEFAULT] Get buffer address
271720ae74dSLee Jones * @dataram1: DataRAM index
272720ae74dSLee Jones * @sectors: the sector address
273720ae74dSLee Jones * @count: the number of sectors
2742dc3d1e0SLee Jones * Return: the start buffer value
27526777d37SBoris Brezillon *
27626777d37SBoris Brezillon * Setup Start Buffer Register (F200h)
27726777d37SBoris Brezillon */
onenand_buffer_address(int dataram1,int sectors,int count)27826777d37SBoris Brezillon static int onenand_buffer_address(int dataram1, int sectors, int count)
27926777d37SBoris Brezillon {
28026777d37SBoris Brezillon int bsa, bsc;
28126777d37SBoris Brezillon
28226777d37SBoris Brezillon /* BufferRAM Sector Address */
28326777d37SBoris Brezillon bsa = sectors & ONENAND_BSA_MASK;
28426777d37SBoris Brezillon
28526777d37SBoris Brezillon if (dataram1)
28626777d37SBoris Brezillon bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
28726777d37SBoris Brezillon else
28826777d37SBoris Brezillon bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
28926777d37SBoris Brezillon
29026777d37SBoris Brezillon /* BufferRAM Sector Count */
29126777d37SBoris Brezillon bsc = count & ONENAND_BSC_MASK;
29226777d37SBoris Brezillon
29326777d37SBoris Brezillon return ((bsa << ONENAND_BSA_SHIFT) | bsc);
29426777d37SBoris Brezillon }
29526777d37SBoris Brezillon
29626777d37SBoris Brezillon /**
29726777d37SBoris Brezillon * flexonenand_block- For given address return block number
298720ae74dSLee Jones * @this: - OneNAND device structure
299720ae74dSLee Jones * @addr: - Address for which block number is needed
30026777d37SBoris Brezillon */
flexonenand_block(struct onenand_chip * this,loff_t addr)30126777d37SBoris Brezillon static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
30226777d37SBoris Brezillon {
30326777d37SBoris Brezillon unsigned boundary, blk, die = 0;
30426777d37SBoris Brezillon
30526777d37SBoris Brezillon if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
30626777d37SBoris Brezillon die = 1;
30726777d37SBoris Brezillon addr -= this->diesize[0];
30826777d37SBoris Brezillon }
30926777d37SBoris Brezillon
31026777d37SBoris Brezillon boundary = this->boundary[die];
31126777d37SBoris Brezillon
31226777d37SBoris Brezillon blk = addr >> (this->erase_shift - 1);
31326777d37SBoris Brezillon if (blk > boundary)
31426777d37SBoris Brezillon blk = (blk + boundary + 1) >> 1;
31526777d37SBoris Brezillon
31626777d37SBoris Brezillon blk += die ? this->density_mask : 0;
31726777d37SBoris Brezillon return blk;
31826777d37SBoris Brezillon }
31926777d37SBoris Brezillon
onenand_block(struct onenand_chip * this,loff_t addr)32026777d37SBoris Brezillon inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
32126777d37SBoris Brezillon {
32226777d37SBoris Brezillon if (!FLEXONENAND(this))
32326777d37SBoris Brezillon return addr >> this->erase_shift;
32426777d37SBoris Brezillon return flexonenand_block(this, addr);
32526777d37SBoris Brezillon }
32626777d37SBoris Brezillon
32726777d37SBoris Brezillon /**
32826777d37SBoris Brezillon * flexonenand_addr - Return address of the block
32926777d37SBoris Brezillon * @this: OneNAND device structure
33026777d37SBoris Brezillon * @block: Block number on Flex-OneNAND
33126777d37SBoris Brezillon *
33226777d37SBoris Brezillon * Return address of the block
33326777d37SBoris Brezillon */
flexonenand_addr(struct onenand_chip * this,int block)33426777d37SBoris Brezillon static loff_t flexonenand_addr(struct onenand_chip *this, int block)
33526777d37SBoris Brezillon {
33626777d37SBoris Brezillon loff_t ofs = 0;
33726777d37SBoris Brezillon int die = 0, boundary;
33826777d37SBoris Brezillon
33926777d37SBoris Brezillon if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
34026777d37SBoris Brezillon block -= this->density_mask;
34126777d37SBoris Brezillon die = 1;
34226777d37SBoris Brezillon ofs = this->diesize[0];
34326777d37SBoris Brezillon }
34426777d37SBoris Brezillon
34526777d37SBoris Brezillon boundary = this->boundary[die];
34626777d37SBoris Brezillon ofs += (loff_t)block << (this->erase_shift - 1);
34726777d37SBoris Brezillon if (block > (boundary + 1))
34826777d37SBoris Brezillon ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
34926777d37SBoris Brezillon return ofs;
35026777d37SBoris Brezillon }
35126777d37SBoris Brezillon
onenand_addr(struct onenand_chip * this,int block)35226777d37SBoris Brezillon loff_t onenand_addr(struct onenand_chip *this, int block)
35326777d37SBoris Brezillon {
35426777d37SBoris Brezillon if (!FLEXONENAND(this))
35526777d37SBoris Brezillon return (loff_t)block << this->erase_shift;
35626777d37SBoris Brezillon return flexonenand_addr(this, block);
35726777d37SBoris Brezillon }
35826777d37SBoris Brezillon EXPORT_SYMBOL(onenand_addr);
35926777d37SBoris Brezillon
36026777d37SBoris Brezillon /**
36126777d37SBoris Brezillon * onenand_get_density - [DEFAULT] Get OneNAND density
362720ae74dSLee Jones * @dev_id: OneNAND device ID
36326777d37SBoris Brezillon *
36426777d37SBoris Brezillon * Get OneNAND density from device ID
36526777d37SBoris Brezillon */
onenand_get_density(int dev_id)36626777d37SBoris Brezillon static inline int onenand_get_density(int dev_id)
36726777d37SBoris Brezillon {
36826777d37SBoris Brezillon int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
36926777d37SBoris Brezillon return (density & ONENAND_DEVICE_DENSITY_MASK);
37026777d37SBoris Brezillon }
37126777d37SBoris Brezillon
37226777d37SBoris Brezillon /**
37326777d37SBoris Brezillon * flexonenand_region - [Flex-OneNAND] Return erase region of addr
374720ae74dSLee Jones * @mtd: MTD device structure
375720ae74dSLee Jones * @addr: address whose erase region needs to be identified
37626777d37SBoris Brezillon */
flexonenand_region(struct mtd_info * mtd,loff_t addr)37726777d37SBoris Brezillon int flexonenand_region(struct mtd_info *mtd, loff_t addr)
37826777d37SBoris Brezillon {
37926777d37SBoris Brezillon int i;
38026777d37SBoris Brezillon
38126777d37SBoris Brezillon for (i = 0; i < mtd->numeraseregions; i++)
38226777d37SBoris Brezillon if (addr < mtd->eraseregions[i].offset)
38326777d37SBoris Brezillon break;
38426777d37SBoris Brezillon return i - 1;
38526777d37SBoris Brezillon }
38626777d37SBoris Brezillon EXPORT_SYMBOL(flexonenand_region);
38726777d37SBoris Brezillon
38826777d37SBoris Brezillon /**
38926777d37SBoris Brezillon * onenand_command - [DEFAULT] Send command to OneNAND device
390720ae74dSLee Jones * @mtd: MTD device structure
391720ae74dSLee Jones * @cmd: the command to be sent
392720ae74dSLee Jones * @addr: offset to read from or write to
393720ae74dSLee Jones * @len: number of bytes to read or write
39426777d37SBoris Brezillon *
39526777d37SBoris Brezillon * Send command to OneNAND device. This function is used for middle/large page
39626777d37SBoris Brezillon * devices (1KB/2KB Bytes per page)
39726777d37SBoris Brezillon */
onenand_command(struct mtd_info * mtd,int cmd,loff_t addr,size_t len)39826777d37SBoris Brezillon static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
39926777d37SBoris Brezillon {
40026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
40126777d37SBoris Brezillon int value, block, page;
40226777d37SBoris Brezillon
40326777d37SBoris Brezillon /* Address translation */
40426777d37SBoris Brezillon switch (cmd) {
40526777d37SBoris Brezillon case ONENAND_CMD_UNLOCK:
40626777d37SBoris Brezillon case ONENAND_CMD_LOCK:
40726777d37SBoris Brezillon case ONENAND_CMD_LOCK_TIGHT:
40826777d37SBoris Brezillon case ONENAND_CMD_UNLOCK_ALL:
40926777d37SBoris Brezillon block = -1;
41026777d37SBoris Brezillon page = -1;
41126777d37SBoris Brezillon break;
41226777d37SBoris Brezillon
41326777d37SBoris Brezillon case FLEXONENAND_CMD_PI_ACCESS:
41426777d37SBoris Brezillon /* addr contains die index */
41526777d37SBoris Brezillon block = addr * this->density_mask;
41626777d37SBoris Brezillon page = -1;
41726777d37SBoris Brezillon break;
41826777d37SBoris Brezillon
41926777d37SBoris Brezillon case ONENAND_CMD_ERASE:
42026777d37SBoris Brezillon case ONENAND_CMD_MULTIBLOCK_ERASE:
42126777d37SBoris Brezillon case ONENAND_CMD_ERASE_VERIFY:
42226777d37SBoris Brezillon case ONENAND_CMD_BUFFERRAM:
42326777d37SBoris Brezillon case ONENAND_CMD_OTP_ACCESS:
42426777d37SBoris Brezillon block = onenand_block(this, addr);
42526777d37SBoris Brezillon page = -1;
42626777d37SBoris Brezillon break;
42726777d37SBoris Brezillon
42826777d37SBoris Brezillon case FLEXONENAND_CMD_READ_PI:
42926777d37SBoris Brezillon cmd = ONENAND_CMD_READ;
43026777d37SBoris Brezillon block = addr * this->density_mask;
43126777d37SBoris Brezillon page = 0;
43226777d37SBoris Brezillon break;
43326777d37SBoris Brezillon
43426777d37SBoris Brezillon default:
43526777d37SBoris Brezillon block = onenand_block(this, addr);
43626777d37SBoris Brezillon if (FLEXONENAND(this))
43726777d37SBoris Brezillon page = (int) (addr - onenand_addr(this, block))>>\
43826777d37SBoris Brezillon this->page_shift;
43926777d37SBoris Brezillon else
44026777d37SBoris Brezillon page = (int) (addr >> this->page_shift);
44126777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
44226777d37SBoris Brezillon /* Make the even block number */
44326777d37SBoris Brezillon block &= ~1;
44426777d37SBoris Brezillon /* Is it the odd plane? */
44526777d37SBoris Brezillon if (addr & this->writesize)
44626777d37SBoris Brezillon block++;
44726777d37SBoris Brezillon page >>= 1;
44826777d37SBoris Brezillon }
44926777d37SBoris Brezillon page &= this->page_mask;
45026777d37SBoris Brezillon break;
45126777d37SBoris Brezillon }
45226777d37SBoris Brezillon
45326777d37SBoris Brezillon /* NOTE: The setting order of the registers is very important! */
45426777d37SBoris Brezillon if (cmd == ONENAND_CMD_BUFFERRAM) {
45526777d37SBoris Brezillon /* Select DataRAM for DDP */
45626777d37SBoris Brezillon value = onenand_bufferram_address(this, block);
45726777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
45826777d37SBoris Brezillon
45926777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
46026777d37SBoris Brezillon /* It is always BufferRAM0 */
46126777d37SBoris Brezillon ONENAND_SET_BUFFERRAM0(this);
46226777d37SBoris Brezillon else
46326777d37SBoris Brezillon /* Switch to the next data buffer */
46426777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
46526777d37SBoris Brezillon
46626777d37SBoris Brezillon return 0;
46726777d37SBoris Brezillon }
46826777d37SBoris Brezillon
46926777d37SBoris Brezillon if (block != -1) {
47026777d37SBoris Brezillon /* Write 'DFS, FBA' of Flash */
47126777d37SBoris Brezillon value = onenand_block_address(this, block);
47226777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
47326777d37SBoris Brezillon
47426777d37SBoris Brezillon /* Select DataRAM for DDP */
47526777d37SBoris Brezillon value = onenand_bufferram_address(this, block);
47626777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
47726777d37SBoris Brezillon }
47826777d37SBoris Brezillon
47926777d37SBoris Brezillon if (page != -1) {
48026777d37SBoris Brezillon /* Now we use page size operation */
48126777d37SBoris Brezillon int sectors = 0, count = 0;
48226777d37SBoris Brezillon int dataram;
48326777d37SBoris Brezillon
48426777d37SBoris Brezillon switch (cmd) {
48526777d37SBoris Brezillon case FLEXONENAND_CMD_RECOVER_LSB:
48626777d37SBoris Brezillon case ONENAND_CMD_READ:
48726777d37SBoris Brezillon case ONENAND_CMD_READOOB:
48826777d37SBoris Brezillon if (ONENAND_IS_4KB_PAGE(this))
48926777d37SBoris Brezillon /* It is always BufferRAM0 */
49026777d37SBoris Brezillon dataram = ONENAND_SET_BUFFERRAM0(this);
49126777d37SBoris Brezillon else
49226777d37SBoris Brezillon dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
49326777d37SBoris Brezillon break;
49426777d37SBoris Brezillon
49526777d37SBoris Brezillon default:
49626777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
49726777d37SBoris Brezillon cmd = ONENAND_CMD_2X_PROG;
49826777d37SBoris Brezillon dataram = ONENAND_CURRENT_BUFFERRAM(this);
49926777d37SBoris Brezillon break;
50026777d37SBoris Brezillon }
50126777d37SBoris Brezillon
50226777d37SBoris Brezillon /* Write 'FPA, FSA' of Flash */
50326777d37SBoris Brezillon value = onenand_page_address(page, sectors);
50426777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
50526777d37SBoris Brezillon
50626777d37SBoris Brezillon /* Write 'BSA, BSC' of DataRAM */
50726777d37SBoris Brezillon value = onenand_buffer_address(dataram, sectors, count);
50826777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
50926777d37SBoris Brezillon }
51026777d37SBoris Brezillon
51126777d37SBoris Brezillon /* Interrupt clear */
51226777d37SBoris Brezillon this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
51326777d37SBoris Brezillon
51426777d37SBoris Brezillon /* Write command */
51526777d37SBoris Brezillon this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
51626777d37SBoris Brezillon
51726777d37SBoris Brezillon return 0;
51826777d37SBoris Brezillon }
51926777d37SBoris Brezillon
52026777d37SBoris Brezillon /**
52126777d37SBoris Brezillon * onenand_read_ecc - return ecc status
522720ae74dSLee Jones * @this: onenand chip structure
52326777d37SBoris Brezillon */
onenand_read_ecc(struct onenand_chip * this)52426777d37SBoris Brezillon static inline int onenand_read_ecc(struct onenand_chip *this)
52526777d37SBoris Brezillon {
52626777d37SBoris Brezillon int ecc, i, result = 0;
52726777d37SBoris Brezillon
52826777d37SBoris Brezillon if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
52926777d37SBoris Brezillon return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
53026777d37SBoris Brezillon
53126777d37SBoris Brezillon for (i = 0; i < 4; i++) {
53226777d37SBoris Brezillon ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
53326777d37SBoris Brezillon if (likely(!ecc))
53426777d37SBoris Brezillon continue;
53526777d37SBoris Brezillon if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
53626777d37SBoris Brezillon return ONENAND_ECC_2BIT_ALL;
53726777d37SBoris Brezillon else
53826777d37SBoris Brezillon result = ONENAND_ECC_1BIT_ALL;
53926777d37SBoris Brezillon }
54026777d37SBoris Brezillon
54126777d37SBoris Brezillon return result;
54226777d37SBoris Brezillon }
54326777d37SBoris Brezillon
54426777d37SBoris Brezillon /**
54526777d37SBoris Brezillon * onenand_wait - [DEFAULT] wait until the command is done
546720ae74dSLee Jones * @mtd: MTD device structure
547720ae74dSLee Jones * @state: state to select the max. timeout value
54826777d37SBoris Brezillon *
54926777d37SBoris Brezillon * Wait for command done. This applies to all OneNAND command
55026777d37SBoris Brezillon * Read can take up to 30us, erase up to 2ms and program up to 350us
55126777d37SBoris Brezillon * according to general OneNAND specs
55226777d37SBoris Brezillon */
onenand_wait(struct mtd_info * mtd,int state)55326777d37SBoris Brezillon static int onenand_wait(struct mtd_info *mtd, int state)
55426777d37SBoris Brezillon {
55526777d37SBoris Brezillon struct onenand_chip * this = mtd->priv;
55626777d37SBoris Brezillon unsigned long timeout;
55726777d37SBoris Brezillon unsigned int flags = ONENAND_INT_MASTER;
55826777d37SBoris Brezillon unsigned int interrupt = 0;
55926777d37SBoris Brezillon unsigned int ctrl;
56026777d37SBoris Brezillon
56126777d37SBoris Brezillon /* The 20 msec is enough */
56226777d37SBoris Brezillon timeout = jiffies + msecs_to_jiffies(20);
56326777d37SBoris Brezillon while (time_before(jiffies, timeout)) {
56426777d37SBoris Brezillon interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
56526777d37SBoris Brezillon
56626777d37SBoris Brezillon if (interrupt & flags)
56726777d37SBoris Brezillon break;
56826777d37SBoris Brezillon
56926777d37SBoris Brezillon if (state != FL_READING && state != FL_PREPARING_ERASE)
57026777d37SBoris Brezillon cond_resched();
57126777d37SBoris Brezillon }
57226777d37SBoris Brezillon /* To get correct interrupt status in timeout case */
57326777d37SBoris Brezillon interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
57426777d37SBoris Brezillon
57526777d37SBoris Brezillon ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
57626777d37SBoris Brezillon
57726777d37SBoris Brezillon /*
57826777d37SBoris Brezillon * In the Spec. it checks the controller status first
57926777d37SBoris Brezillon * However if you get the correct information in case of
58026777d37SBoris Brezillon * power off recovery (POR) test, it should read ECC status first
58126777d37SBoris Brezillon */
58226777d37SBoris Brezillon if (interrupt & ONENAND_INT_READ) {
58326777d37SBoris Brezillon int ecc = onenand_read_ecc(this);
58426777d37SBoris Brezillon if (ecc) {
58526777d37SBoris Brezillon if (ecc & ONENAND_ECC_2BIT_ALL) {
58626777d37SBoris Brezillon printk(KERN_ERR "%s: ECC error = 0x%04x\n",
58726777d37SBoris Brezillon __func__, ecc);
58826777d37SBoris Brezillon mtd->ecc_stats.failed++;
58926777d37SBoris Brezillon return -EBADMSG;
59026777d37SBoris Brezillon } else if (ecc & ONENAND_ECC_1BIT_ALL) {
59126777d37SBoris Brezillon printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
59226777d37SBoris Brezillon __func__, ecc);
59326777d37SBoris Brezillon mtd->ecc_stats.corrected++;
59426777d37SBoris Brezillon }
59526777d37SBoris Brezillon }
59626777d37SBoris Brezillon } else if (state == FL_READING) {
59726777d37SBoris Brezillon printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
59826777d37SBoris Brezillon __func__, ctrl, interrupt);
59926777d37SBoris Brezillon return -EIO;
60026777d37SBoris Brezillon }
60126777d37SBoris Brezillon
60226777d37SBoris Brezillon if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
60326777d37SBoris Brezillon printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
60426777d37SBoris Brezillon __func__, ctrl, interrupt);
60526777d37SBoris Brezillon return -EIO;
60626777d37SBoris Brezillon }
60726777d37SBoris Brezillon
60826777d37SBoris Brezillon if (!(interrupt & ONENAND_INT_MASTER)) {
60926777d37SBoris Brezillon printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
61026777d37SBoris Brezillon __func__, ctrl, interrupt);
61126777d37SBoris Brezillon return -EIO;
61226777d37SBoris Brezillon }
61326777d37SBoris Brezillon
61426777d37SBoris Brezillon /* If there's controller error, it's a real error */
61526777d37SBoris Brezillon if (ctrl & ONENAND_CTRL_ERROR) {
61626777d37SBoris Brezillon printk(KERN_ERR "%s: controller error = 0x%04x\n",
61726777d37SBoris Brezillon __func__, ctrl);
61826777d37SBoris Brezillon if (ctrl & ONENAND_CTRL_LOCK)
61926777d37SBoris Brezillon printk(KERN_ERR "%s: it's locked error.\n", __func__);
62026777d37SBoris Brezillon return -EIO;
62126777d37SBoris Brezillon }
62226777d37SBoris Brezillon
62326777d37SBoris Brezillon return 0;
62426777d37SBoris Brezillon }
62526777d37SBoris Brezillon
62626777d37SBoris Brezillon /*
62726777d37SBoris Brezillon * onenand_interrupt - [DEFAULT] onenand interrupt handler
628720ae74dSLee Jones * @irq: onenand interrupt number
629720ae74dSLee Jones * @dev_id: interrupt data
63026777d37SBoris Brezillon *
63126777d37SBoris Brezillon * complete the work
63226777d37SBoris Brezillon */
onenand_interrupt(int irq,void * data)63326777d37SBoris Brezillon static irqreturn_t onenand_interrupt(int irq, void *data)
63426777d37SBoris Brezillon {
63526777d37SBoris Brezillon struct onenand_chip *this = data;
63626777d37SBoris Brezillon
63726777d37SBoris Brezillon /* To handle shared interrupt */
63826777d37SBoris Brezillon if (!this->complete.done)
63926777d37SBoris Brezillon complete(&this->complete);
64026777d37SBoris Brezillon
64126777d37SBoris Brezillon return IRQ_HANDLED;
64226777d37SBoris Brezillon }
64326777d37SBoris Brezillon
64426777d37SBoris Brezillon /*
64526777d37SBoris Brezillon * onenand_interrupt_wait - [DEFAULT] wait until the command is done
646720ae74dSLee Jones * @mtd: MTD device structure
647720ae74dSLee Jones * @state: state to select the max. timeout value
64826777d37SBoris Brezillon *
64926777d37SBoris Brezillon * Wait for command done.
65026777d37SBoris Brezillon */
onenand_interrupt_wait(struct mtd_info * mtd,int state)65126777d37SBoris Brezillon static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
65226777d37SBoris Brezillon {
65326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
65426777d37SBoris Brezillon
65526777d37SBoris Brezillon wait_for_completion(&this->complete);
65626777d37SBoris Brezillon
65726777d37SBoris Brezillon return onenand_wait(mtd, state);
65826777d37SBoris Brezillon }
65926777d37SBoris Brezillon
66026777d37SBoris Brezillon /*
66126777d37SBoris Brezillon * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
662720ae74dSLee Jones * @mtd: MTD device structure
663720ae74dSLee Jones * @state: state to select the max. timeout value
66426777d37SBoris Brezillon *
66526777d37SBoris Brezillon * Try interrupt based wait (It is used one-time)
66626777d37SBoris Brezillon */
onenand_try_interrupt_wait(struct mtd_info * mtd,int state)66726777d37SBoris Brezillon static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
66826777d37SBoris Brezillon {
66926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
67026777d37SBoris Brezillon unsigned long remain, timeout;
67126777d37SBoris Brezillon
67226777d37SBoris Brezillon /* We use interrupt wait first */
67326777d37SBoris Brezillon this->wait = onenand_interrupt_wait;
67426777d37SBoris Brezillon
67526777d37SBoris Brezillon timeout = msecs_to_jiffies(100);
67626777d37SBoris Brezillon remain = wait_for_completion_timeout(&this->complete, timeout);
67726777d37SBoris Brezillon if (!remain) {
67826777d37SBoris Brezillon printk(KERN_INFO "OneNAND: There's no interrupt. "
67926777d37SBoris Brezillon "We use the normal wait\n");
68026777d37SBoris Brezillon
68126777d37SBoris Brezillon /* Release the irq */
68226777d37SBoris Brezillon free_irq(this->irq, this);
68326777d37SBoris Brezillon
68426777d37SBoris Brezillon this->wait = onenand_wait;
68526777d37SBoris Brezillon }
68626777d37SBoris Brezillon
68726777d37SBoris Brezillon return onenand_wait(mtd, state);
68826777d37SBoris Brezillon }
68926777d37SBoris Brezillon
69026777d37SBoris Brezillon /*
69126777d37SBoris Brezillon * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
692720ae74dSLee Jones * @mtd: MTD device structure
69326777d37SBoris Brezillon *
69426777d37SBoris Brezillon * There's two method to wait onenand work
69526777d37SBoris Brezillon * 1. polling - read interrupt status register
69626777d37SBoris Brezillon * 2. interrupt - use the kernel interrupt method
69726777d37SBoris Brezillon */
onenand_setup_wait(struct mtd_info * mtd)69826777d37SBoris Brezillon static void onenand_setup_wait(struct mtd_info *mtd)
69926777d37SBoris Brezillon {
70026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
70126777d37SBoris Brezillon int syscfg;
70226777d37SBoris Brezillon
70326777d37SBoris Brezillon init_completion(&this->complete);
70426777d37SBoris Brezillon
70526777d37SBoris Brezillon if (this->irq <= 0) {
70626777d37SBoris Brezillon this->wait = onenand_wait;
70726777d37SBoris Brezillon return;
70826777d37SBoris Brezillon }
70926777d37SBoris Brezillon
71026777d37SBoris Brezillon if (request_irq(this->irq, &onenand_interrupt,
71126777d37SBoris Brezillon IRQF_SHARED, "onenand", this)) {
71226777d37SBoris Brezillon /* If we can't get irq, use the normal wait */
71326777d37SBoris Brezillon this->wait = onenand_wait;
71426777d37SBoris Brezillon return;
71526777d37SBoris Brezillon }
71626777d37SBoris Brezillon
71726777d37SBoris Brezillon /* Enable interrupt */
71826777d37SBoris Brezillon syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
71926777d37SBoris Brezillon syscfg |= ONENAND_SYS_CFG1_IOBE;
72026777d37SBoris Brezillon this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
72126777d37SBoris Brezillon
72226777d37SBoris Brezillon this->wait = onenand_try_interrupt_wait;
72326777d37SBoris Brezillon }
72426777d37SBoris Brezillon
72526777d37SBoris Brezillon /**
72626777d37SBoris Brezillon * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
727720ae74dSLee Jones * @mtd: MTD data structure
728720ae74dSLee Jones * @area: BufferRAM area
72926777d37SBoris Brezillon * @return offset given area
73026777d37SBoris Brezillon *
73126777d37SBoris Brezillon * Return BufferRAM offset given area
73226777d37SBoris Brezillon */
onenand_bufferram_offset(struct mtd_info * mtd,int area)73326777d37SBoris Brezillon static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
73426777d37SBoris Brezillon {
73526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
73626777d37SBoris Brezillon
73726777d37SBoris Brezillon if (ONENAND_CURRENT_BUFFERRAM(this)) {
73826777d37SBoris Brezillon /* Note: the 'this->writesize' is a real page size */
73926777d37SBoris Brezillon if (area == ONENAND_DATARAM)
74026777d37SBoris Brezillon return this->writesize;
74126777d37SBoris Brezillon if (area == ONENAND_SPARERAM)
74226777d37SBoris Brezillon return mtd->oobsize;
74326777d37SBoris Brezillon }
74426777d37SBoris Brezillon
74526777d37SBoris Brezillon return 0;
74626777d37SBoris Brezillon }
74726777d37SBoris Brezillon
74826777d37SBoris Brezillon /**
74926777d37SBoris Brezillon * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
750720ae74dSLee Jones * @mtd: MTD data structure
751720ae74dSLee Jones * @area: BufferRAM area
752720ae74dSLee Jones * @buffer: the databuffer to put/get data
753720ae74dSLee Jones * @offset: offset to read from or write to
754720ae74dSLee Jones * @count: number of bytes to read/write
75526777d37SBoris Brezillon *
75626777d37SBoris Brezillon * Read the BufferRAM area
75726777d37SBoris Brezillon */
onenand_read_bufferram(struct mtd_info * mtd,int area,unsigned char * buffer,int offset,size_t count)75826777d37SBoris Brezillon static int onenand_read_bufferram(struct mtd_info *mtd, int area,
75926777d37SBoris Brezillon unsigned char *buffer, int offset, size_t count)
76026777d37SBoris Brezillon {
76126777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
76226777d37SBoris Brezillon void __iomem *bufferram;
76326777d37SBoris Brezillon
76426777d37SBoris Brezillon bufferram = this->base + area;
76526777d37SBoris Brezillon
76626777d37SBoris Brezillon bufferram += onenand_bufferram_offset(mtd, area);
76726777d37SBoris Brezillon
76826777d37SBoris Brezillon if (ONENAND_CHECK_BYTE_ACCESS(count)) {
76926777d37SBoris Brezillon unsigned short word;
77026777d37SBoris Brezillon
77126777d37SBoris Brezillon /* Align with word(16-bit) size */
77226777d37SBoris Brezillon count--;
77326777d37SBoris Brezillon
77426777d37SBoris Brezillon /* Read word and save byte */
77526777d37SBoris Brezillon word = this->read_word(bufferram + offset + count);
77626777d37SBoris Brezillon buffer[count] = (word & 0xff);
77726777d37SBoris Brezillon }
77826777d37SBoris Brezillon
77926777d37SBoris Brezillon memcpy(buffer, bufferram + offset, count);
78026777d37SBoris Brezillon
78126777d37SBoris Brezillon return 0;
78226777d37SBoris Brezillon }
78326777d37SBoris Brezillon
78426777d37SBoris Brezillon /**
78526777d37SBoris Brezillon * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
786720ae74dSLee Jones * @mtd: MTD data structure
787720ae74dSLee Jones * @area: BufferRAM area
788720ae74dSLee Jones * @buffer: the databuffer to put/get data
789720ae74dSLee Jones * @offset: offset to read from or write to
790720ae74dSLee Jones * @count: number of bytes to read/write
79126777d37SBoris Brezillon *
79226777d37SBoris Brezillon * Read the BufferRAM area with Sync. Burst Mode
79326777d37SBoris Brezillon */
onenand_sync_read_bufferram(struct mtd_info * mtd,int area,unsigned char * buffer,int offset,size_t count)79426777d37SBoris Brezillon static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
79526777d37SBoris Brezillon unsigned char *buffer, int offset, size_t count)
79626777d37SBoris Brezillon {
79726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
79826777d37SBoris Brezillon void __iomem *bufferram;
79926777d37SBoris Brezillon
80026777d37SBoris Brezillon bufferram = this->base + area;
80126777d37SBoris Brezillon
80226777d37SBoris Brezillon bufferram += onenand_bufferram_offset(mtd, area);
80326777d37SBoris Brezillon
80426777d37SBoris Brezillon this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
80526777d37SBoris Brezillon
80626777d37SBoris Brezillon if (ONENAND_CHECK_BYTE_ACCESS(count)) {
80726777d37SBoris Brezillon unsigned short word;
80826777d37SBoris Brezillon
80926777d37SBoris Brezillon /* Align with word(16-bit) size */
81026777d37SBoris Brezillon count--;
81126777d37SBoris Brezillon
81226777d37SBoris Brezillon /* Read word and save byte */
81326777d37SBoris Brezillon word = this->read_word(bufferram + offset + count);
81426777d37SBoris Brezillon buffer[count] = (word & 0xff);
81526777d37SBoris Brezillon }
81626777d37SBoris Brezillon
81726777d37SBoris Brezillon memcpy(buffer, bufferram + offset, count);
81826777d37SBoris Brezillon
81926777d37SBoris Brezillon this->mmcontrol(mtd, 0);
82026777d37SBoris Brezillon
82126777d37SBoris Brezillon return 0;
82226777d37SBoris Brezillon }
82326777d37SBoris Brezillon
82426777d37SBoris Brezillon /**
82526777d37SBoris Brezillon * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
826720ae74dSLee Jones * @mtd: MTD data structure
827720ae74dSLee Jones * @area: BufferRAM area
828720ae74dSLee Jones * @buffer: the databuffer to put/get data
829720ae74dSLee Jones * @offset: offset to read from or write to
830720ae74dSLee Jones * @count: number of bytes to read/write
83126777d37SBoris Brezillon *
83226777d37SBoris Brezillon * Write the BufferRAM area
83326777d37SBoris Brezillon */
onenand_write_bufferram(struct mtd_info * mtd,int area,const unsigned char * buffer,int offset,size_t count)83426777d37SBoris Brezillon static int onenand_write_bufferram(struct mtd_info *mtd, int area,
83526777d37SBoris Brezillon const unsigned char *buffer, int offset, size_t count)
83626777d37SBoris Brezillon {
83726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
83826777d37SBoris Brezillon void __iomem *bufferram;
83926777d37SBoris Brezillon
84026777d37SBoris Brezillon bufferram = this->base + area;
84126777d37SBoris Brezillon
84226777d37SBoris Brezillon bufferram += onenand_bufferram_offset(mtd, area);
84326777d37SBoris Brezillon
84426777d37SBoris Brezillon if (ONENAND_CHECK_BYTE_ACCESS(count)) {
84526777d37SBoris Brezillon unsigned short word;
84626777d37SBoris Brezillon int byte_offset;
84726777d37SBoris Brezillon
84826777d37SBoris Brezillon /* Align with word(16-bit) size */
84926777d37SBoris Brezillon count--;
85026777d37SBoris Brezillon
85126777d37SBoris Brezillon /* Calculate byte access offset */
85226777d37SBoris Brezillon byte_offset = offset + count;
85326777d37SBoris Brezillon
85426777d37SBoris Brezillon /* Read word and save byte */
85526777d37SBoris Brezillon word = this->read_word(bufferram + byte_offset);
85626777d37SBoris Brezillon word = (word & ~0xff) | buffer[count];
85726777d37SBoris Brezillon this->write_word(word, bufferram + byte_offset);
85826777d37SBoris Brezillon }
85926777d37SBoris Brezillon
86026777d37SBoris Brezillon memcpy(bufferram + offset, buffer, count);
86126777d37SBoris Brezillon
86226777d37SBoris Brezillon return 0;
86326777d37SBoris Brezillon }
86426777d37SBoris Brezillon
86526777d37SBoris Brezillon /**
86626777d37SBoris Brezillon * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
867720ae74dSLee Jones * @mtd: MTD data structure
868720ae74dSLee Jones * @addr: address to check
86926777d37SBoris Brezillon * @return blockpage address
87026777d37SBoris Brezillon *
87126777d37SBoris Brezillon * Get blockpage address at 2x program mode
87226777d37SBoris Brezillon */
onenand_get_2x_blockpage(struct mtd_info * mtd,loff_t addr)87326777d37SBoris Brezillon static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
87426777d37SBoris Brezillon {
87526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
87626777d37SBoris Brezillon int blockpage, block, page;
87726777d37SBoris Brezillon
87826777d37SBoris Brezillon /* Calculate the even block number */
87926777d37SBoris Brezillon block = (int) (addr >> this->erase_shift) & ~1;
88026777d37SBoris Brezillon /* Is it the odd plane? */
88126777d37SBoris Brezillon if (addr & this->writesize)
88226777d37SBoris Brezillon block++;
88326777d37SBoris Brezillon page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
88426777d37SBoris Brezillon blockpage = (block << 7) | page;
88526777d37SBoris Brezillon
88626777d37SBoris Brezillon return blockpage;
88726777d37SBoris Brezillon }
88826777d37SBoris Brezillon
88926777d37SBoris Brezillon /**
89026777d37SBoris Brezillon * onenand_check_bufferram - [GENERIC] Check BufferRAM information
891720ae74dSLee Jones * @mtd: MTD data structure
892720ae74dSLee Jones * @addr: address to check
89326777d37SBoris Brezillon * @return 1 if there are valid data, otherwise 0
89426777d37SBoris Brezillon *
89526777d37SBoris Brezillon * Check bufferram if there is data we required
89626777d37SBoris Brezillon */
onenand_check_bufferram(struct mtd_info * mtd,loff_t addr)89726777d37SBoris Brezillon static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
89826777d37SBoris Brezillon {
89926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
90026777d37SBoris Brezillon int blockpage, found = 0;
90126777d37SBoris Brezillon unsigned int i;
90226777d37SBoris Brezillon
90326777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this))
90426777d37SBoris Brezillon blockpage = onenand_get_2x_blockpage(mtd, addr);
90526777d37SBoris Brezillon else
90626777d37SBoris Brezillon blockpage = (int) (addr >> this->page_shift);
90726777d37SBoris Brezillon
90826777d37SBoris Brezillon /* Is there valid data? */
90926777d37SBoris Brezillon i = ONENAND_CURRENT_BUFFERRAM(this);
91026777d37SBoris Brezillon if (this->bufferram[i].blockpage == blockpage)
91126777d37SBoris Brezillon found = 1;
91226777d37SBoris Brezillon else {
91326777d37SBoris Brezillon /* Check another BufferRAM */
91426777d37SBoris Brezillon i = ONENAND_NEXT_BUFFERRAM(this);
91526777d37SBoris Brezillon if (this->bufferram[i].blockpage == blockpage) {
91626777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
91726777d37SBoris Brezillon found = 1;
91826777d37SBoris Brezillon }
91926777d37SBoris Brezillon }
92026777d37SBoris Brezillon
92126777d37SBoris Brezillon if (found && ONENAND_IS_DDP(this)) {
92226777d37SBoris Brezillon /* Select DataRAM for DDP */
92326777d37SBoris Brezillon int block = onenand_block(this, addr);
92426777d37SBoris Brezillon int value = onenand_bufferram_address(this, block);
92526777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
92626777d37SBoris Brezillon }
92726777d37SBoris Brezillon
92826777d37SBoris Brezillon return found;
92926777d37SBoris Brezillon }
93026777d37SBoris Brezillon
93126777d37SBoris Brezillon /**
93226777d37SBoris Brezillon * onenand_update_bufferram - [GENERIC] Update BufferRAM information
933720ae74dSLee Jones * @mtd: MTD data structure
934720ae74dSLee Jones * @addr: address to update
935720ae74dSLee Jones * @valid: valid flag
93626777d37SBoris Brezillon *
93726777d37SBoris Brezillon * Update BufferRAM information
93826777d37SBoris Brezillon */
onenand_update_bufferram(struct mtd_info * mtd,loff_t addr,int valid)93926777d37SBoris Brezillon static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
94026777d37SBoris Brezillon int valid)
94126777d37SBoris Brezillon {
94226777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
94326777d37SBoris Brezillon int blockpage;
94426777d37SBoris Brezillon unsigned int i;
94526777d37SBoris Brezillon
94626777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this))
94726777d37SBoris Brezillon blockpage = onenand_get_2x_blockpage(mtd, addr);
94826777d37SBoris Brezillon else
94926777d37SBoris Brezillon blockpage = (int) (addr >> this->page_shift);
95026777d37SBoris Brezillon
95126777d37SBoris Brezillon /* Invalidate another BufferRAM */
95226777d37SBoris Brezillon i = ONENAND_NEXT_BUFFERRAM(this);
95326777d37SBoris Brezillon if (this->bufferram[i].blockpage == blockpage)
95426777d37SBoris Brezillon this->bufferram[i].blockpage = -1;
95526777d37SBoris Brezillon
95626777d37SBoris Brezillon /* Update BufferRAM */
95726777d37SBoris Brezillon i = ONENAND_CURRENT_BUFFERRAM(this);
95826777d37SBoris Brezillon if (valid)
95926777d37SBoris Brezillon this->bufferram[i].blockpage = blockpage;
96026777d37SBoris Brezillon else
96126777d37SBoris Brezillon this->bufferram[i].blockpage = -1;
96226777d37SBoris Brezillon }
96326777d37SBoris Brezillon
96426777d37SBoris Brezillon /**
96526777d37SBoris Brezillon * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
966720ae74dSLee Jones * @mtd: MTD data structure
967720ae74dSLee Jones * @addr: start address to invalidate
968720ae74dSLee Jones * @len: length to invalidate
96926777d37SBoris Brezillon *
97026777d37SBoris Brezillon * Invalidate BufferRAM information
97126777d37SBoris Brezillon */
onenand_invalidate_bufferram(struct mtd_info * mtd,loff_t addr,unsigned int len)97226777d37SBoris Brezillon static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
97326777d37SBoris Brezillon unsigned int len)
97426777d37SBoris Brezillon {
97526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
97626777d37SBoris Brezillon int i;
97726777d37SBoris Brezillon loff_t end_addr = addr + len;
97826777d37SBoris Brezillon
97926777d37SBoris Brezillon /* Invalidate BufferRAM */
98026777d37SBoris Brezillon for (i = 0; i < MAX_BUFFERRAM; i++) {
98126777d37SBoris Brezillon loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
98226777d37SBoris Brezillon if (buf_addr >= addr && buf_addr < end_addr)
98326777d37SBoris Brezillon this->bufferram[i].blockpage = -1;
98426777d37SBoris Brezillon }
98526777d37SBoris Brezillon }
98626777d37SBoris Brezillon
98726777d37SBoris Brezillon /**
98826777d37SBoris Brezillon * onenand_get_device - [GENERIC] Get chip for selected access
989720ae74dSLee Jones * @mtd: MTD device structure
990720ae74dSLee Jones * @new_state: the state which is requested
99126777d37SBoris Brezillon *
99226777d37SBoris Brezillon * Get the device and lock it for exclusive access
99326777d37SBoris Brezillon */
onenand_get_device(struct mtd_info * mtd,int new_state)99426777d37SBoris Brezillon static int onenand_get_device(struct mtd_info *mtd, int new_state)
99526777d37SBoris Brezillon {
99626777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
99726777d37SBoris Brezillon DECLARE_WAITQUEUE(wait, current);
99826777d37SBoris Brezillon
99926777d37SBoris Brezillon /*
100026777d37SBoris Brezillon * Grab the lock and see if the device is available
100126777d37SBoris Brezillon */
100226777d37SBoris Brezillon while (1) {
100326777d37SBoris Brezillon spin_lock(&this->chip_lock);
100426777d37SBoris Brezillon if (this->state == FL_READY) {
100526777d37SBoris Brezillon this->state = new_state;
100626777d37SBoris Brezillon spin_unlock(&this->chip_lock);
100726777d37SBoris Brezillon if (new_state != FL_PM_SUSPENDED && this->enable)
100826777d37SBoris Brezillon this->enable(mtd);
100926777d37SBoris Brezillon break;
101026777d37SBoris Brezillon }
101126777d37SBoris Brezillon if (new_state == FL_PM_SUSPENDED) {
101226777d37SBoris Brezillon spin_unlock(&this->chip_lock);
101326777d37SBoris Brezillon return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
101426777d37SBoris Brezillon }
101526777d37SBoris Brezillon set_current_state(TASK_UNINTERRUPTIBLE);
101626777d37SBoris Brezillon add_wait_queue(&this->wq, &wait);
101726777d37SBoris Brezillon spin_unlock(&this->chip_lock);
101826777d37SBoris Brezillon schedule();
101926777d37SBoris Brezillon remove_wait_queue(&this->wq, &wait);
102026777d37SBoris Brezillon }
102126777d37SBoris Brezillon
102226777d37SBoris Brezillon return 0;
102326777d37SBoris Brezillon }
102426777d37SBoris Brezillon
102526777d37SBoris Brezillon /**
102626777d37SBoris Brezillon * onenand_release_device - [GENERIC] release chip
1027720ae74dSLee Jones * @mtd: MTD device structure
102826777d37SBoris Brezillon *
102926777d37SBoris Brezillon * Deselect, release chip lock and wake up anyone waiting on the device
103026777d37SBoris Brezillon */
onenand_release_device(struct mtd_info * mtd)103126777d37SBoris Brezillon static void onenand_release_device(struct mtd_info *mtd)
103226777d37SBoris Brezillon {
103326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
103426777d37SBoris Brezillon
103526777d37SBoris Brezillon if (this->state != FL_PM_SUSPENDED && this->disable)
103626777d37SBoris Brezillon this->disable(mtd);
103726777d37SBoris Brezillon /* Release the chip */
103826777d37SBoris Brezillon spin_lock(&this->chip_lock);
103926777d37SBoris Brezillon this->state = FL_READY;
104026777d37SBoris Brezillon wake_up(&this->wq);
104126777d37SBoris Brezillon spin_unlock(&this->chip_lock);
104226777d37SBoris Brezillon }
104326777d37SBoris Brezillon
104426777d37SBoris Brezillon /**
104526777d37SBoris Brezillon * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
1046720ae74dSLee Jones * @mtd: MTD device structure
1047720ae74dSLee Jones * @buf: destination address
1048720ae74dSLee Jones * @column: oob offset to read from
1049720ae74dSLee Jones * @thislen: oob length to read
105026777d37SBoris Brezillon */
onenand_transfer_auto_oob(struct mtd_info * mtd,uint8_t * buf,int column,int thislen)105126777d37SBoris Brezillon static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
105226777d37SBoris Brezillon int thislen)
105326777d37SBoris Brezillon {
105426777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
105526777d37SBoris Brezillon
105626777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
105726777d37SBoris Brezillon mtd->oobsize);
105888e315d1SLiu Shixin return mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
105926777d37SBoris Brezillon column, thislen);
106026777d37SBoris Brezillon }
106126777d37SBoris Brezillon
106226777d37SBoris Brezillon /**
106326777d37SBoris Brezillon * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1064720ae74dSLee Jones * @mtd: MTD device structure
1065720ae74dSLee Jones * @addr: address to recover
1066720ae74dSLee Jones * @status: return value from onenand_wait / onenand_bbt_wait
106726777d37SBoris Brezillon *
106826777d37SBoris Brezillon * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
106926777d37SBoris Brezillon * lower page address and MSB page has higher page address in paired pages.
107026777d37SBoris Brezillon * If power off occurs during MSB page program, the paired LSB page data can
107126777d37SBoris Brezillon * become corrupt. LSB page recovery read is a way to read LSB page though page
107226777d37SBoris Brezillon * data are corrupted. When uncorrectable error occurs as a result of LSB page
107326777d37SBoris Brezillon * read after power up, issue LSB page recovery read.
107426777d37SBoris Brezillon */
onenand_recover_lsb(struct mtd_info * mtd,loff_t addr,int status)107526777d37SBoris Brezillon static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
107626777d37SBoris Brezillon {
107726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
107826777d37SBoris Brezillon int i;
107926777d37SBoris Brezillon
108026777d37SBoris Brezillon /* Recovery is only for Flex-OneNAND */
108126777d37SBoris Brezillon if (!FLEXONENAND(this))
108226777d37SBoris Brezillon return status;
108326777d37SBoris Brezillon
108426777d37SBoris Brezillon /* check if we failed due to uncorrectable error */
108526777d37SBoris Brezillon if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
108626777d37SBoris Brezillon return status;
108726777d37SBoris Brezillon
108826777d37SBoris Brezillon /* check if address lies in MLC region */
108926777d37SBoris Brezillon i = flexonenand_region(mtd, addr);
109026777d37SBoris Brezillon if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
109126777d37SBoris Brezillon return status;
109226777d37SBoris Brezillon
109326777d37SBoris Brezillon /* We are attempting to reread, so decrement stats.failed
109426777d37SBoris Brezillon * which was incremented by onenand_wait due to read failure
109526777d37SBoris Brezillon */
109626777d37SBoris Brezillon printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
109726777d37SBoris Brezillon __func__);
109826777d37SBoris Brezillon mtd->ecc_stats.failed--;
109926777d37SBoris Brezillon
110026777d37SBoris Brezillon /* Issue the LSB page recovery command */
110126777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
110226777d37SBoris Brezillon return this->wait(mtd, FL_READING);
110326777d37SBoris Brezillon }
110426777d37SBoris Brezillon
110526777d37SBoris Brezillon /**
110626777d37SBoris Brezillon * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1107720ae74dSLee Jones * @mtd: MTD device structure
1108720ae74dSLee Jones * @from: offset to read from
1109720ae74dSLee Jones * @ops: oob operation description structure
111026777d37SBoris Brezillon *
111126777d37SBoris Brezillon * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
111226777d37SBoris Brezillon * So, read-while-load is not present.
111326777d37SBoris Brezillon */
onenand_mlc_read_ops_nolock(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)111426777d37SBoris Brezillon static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
111526777d37SBoris Brezillon struct mtd_oob_ops *ops)
111626777d37SBoris Brezillon {
111726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
111826777d37SBoris Brezillon struct mtd_ecc_stats stats;
111926777d37SBoris Brezillon size_t len = ops->len;
112026777d37SBoris Brezillon size_t ooblen = ops->ooblen;
112126777d37SBoris Brezillon u_char *buf = ops->datbuf;
112226777d37SBoris Brezillon u_char *oobbuf = ops->oobbuf;
112326777d37SBoris Brezillon int read = 0, column, thislen;
112426777d37SBoris Brezillon int oobread = 0, oobcolumn, thisooblen, oobsize;
112526777d37SBoris Brezillon int ret = 0;
112626777d37SBoris Brezillon int writesize = this->writesize;
112726777d37SBoris Brezillon
112826777d37SBoris Brezillon pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
112926777d37SBoris Brezillon (int)len);
113026777d37SBoris Brezillon
113126777d37SBoris Brezillon oobsize = mtd_oobavail(mtd, ops);
113226777d37SBoris Brezillon oobcolumn = from & (mtd->oobsize - 1);
113326777d37SBoris Brezillon
113426777d37SBoris Brezillon /* Do not allow reads past end of device */
113526777d37SBoris Brezillon if (from + len > mtd->size) {
113626777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt read beyond end of device\n",
113726777d37SBoris Brezillon __func__);
113826777d37SBoris Brezillon ops->retlen = 0;
113926777d37SBoris Brezillon ops->oobretlen = 0;
114026777d37SBoris Brezillon return -EINVAL;
114126777d37SBoris Brezillon }
114226777d37SBoris Brezillon
114326777d37SBoris Brezillon stats = mtd->ecc_stats;
114426777d37SBoris Brezillon
114526777d37SBoris Brezillon while (read < len) {
114626777d37SBoris Brezillon cond_resched();
114726777d37SBoris Brezillon
114826777d37SBoris Brezillon thislen = min_t(int, writesize, len - read);
114926777d37SBoris Brezillon
115026777d37SBoris Brezillon column = from & (writesize - 1);
115126777d37SBoris Brezillon if (column + thislen > writesize)
115226777d37SBoris Brezillon thislen = writesize - column;
115326777d37SBoris Brezillon
115426777d37SBoris Brezillon if (!onenand_check_bufferram(mtd, from)) {
115526777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_READ, from, writesize);
115626777d37SBoris Brezillon
115726777d37SBoris Brezillon ret = this->wait(mtd, FL_READING);
115826777d37SBoris Brezillon if (unlikely(ret))
115926777d37SBoris Brezillon ret = onenand_recover_lsb(mtd, from, ret);
116026777d37SBoris Brezillon onenand_update_bufferram(mtd, from, !ret);
116126777d37SBoris Brezillon if (mtd_is_eccerr(ret))
116226777d37SBoris Brezillon ret = 0;
116326777d37SBoris Brezillon if (ret)
116426777d37SBoris Brezillon break;
116526777d37SBoris Brezillon }
116626777d37SBoris Brezillon
116726777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
116826777d37SBoris Brezillon if (oobbuf) {
116926777d37SBoris Brezillon thisooblen = oobsize - oobcolumn;
117026777d37SBoris Brezillon thisooblen = min_t(int, thisooblen, ooblen - oobread);
117126777d37SBoris Brezillon
117226777d37SBoris Brezillon if (ops->mode == MTD_OPS_AUTO_OOB)
117326777d37SBoris Brezillon onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
117426777d37SBoris Brezillon else
117526777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
117626777d37SBoris Brezillon oobread += thisooblen;
117726777d37SBoris Brezillon oobbuf += thisooblen;
117826777d37SBoris Brezillon oobcolumn = 0;
117926777d37SBoris Brezillon }
118026777d37SBoris Brezillon
118126777d37SBoris Brezillon read += thislen;
118226777d37SBoris Brezillon if (read == len)
118326777d37SBoris Brezillon break;
118426777d37SBoris Brezillon
118526777d37SBoris Brezillon from += thislen;
118626777d37SBoris Brezillon buf += thislen;
118726777d37SBoris Brezillon }
118826777d37SBoris Brezillon
118926777d37SBoris Brezillon /*
119026777d37SBoris Brezillon * Return success, if no ECC failures, else -EBADMSG
119126777d37SBoris Brezillon * fs driver will take care of that, because
119226777d37SBoris Brezillon * retlen == desired len and result == -EBADMSG
119326777d37SBoris Brezillon */
119426777d37SBoris Brezillon ops->retlen = read;
119526777d37SBoris Brezillon ops->oobretlen = oobread;
119626777d37SBoris Brezillon
119726777d37SBoris Brezillon if (ret)
119826777d37SBoris Brezillon return ret;
119926777d37SBoris Brezillon
120026777d37SBoris Brezillon if (mtd->ecc_stats.failed - stats.failed)
120126777d37SBoris Brezillon return -EBADMSG;
120226777d37SBoris Brezillon
120326777d37SBoris Brezillon /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
120426777d37SBoris Brezillon return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
120526777d37SBoris Brezillon }
120626777d37SBoris Brezillon
120726777d37SBoris Brezillon /**
120826777d37SBoris Brezillon * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1209720ae74dSLee Jones * @mtd: MTD device structure
1210720ae74dSLee Jones * @from: offset to read from
1211720ae74dSLee Jones * @ops: oob operation description structure
121226777d37SBoris Brezillon *
121326777d37SBoris Brezillon * OneNAND read main and/or out-of-band data
121426777d37SBoris Brezillon */
onenand_read_ops_nolock(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)121526777d37SBoris Brezillon static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
121626777d37SBoris Brezillon struct mtd_oob_ops *ops)
121726777d37SBoris Brezillon {
121826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
121926777d37SBoris Brezillon struct mtd_ecc_stats stats;
122026777d37SBoris Brezillon size_t len = ops->len;
122126777d37SBoris Brezillon size_t ooblen = ops->ooblen;
122226777d37SBoris Brezillon u_char *buf = ops->datbuf;
122326777d37SBoris Brezillon u_char *oobbuf = ops->oobbuf;
122426777d37SBoris Brezillon int read = 0, column, thislen;
122526777d37SBoris Brezillon int oobread = 0, oobcolumn, thisooblen, oobsize;
122626777d37SBoris Brezillon int ret = 0, boundary = 0;
122726777d37SBoris Brezillon int writesize = this->writesize;
122826777d37SBoris Brezillon
122926777d37SBoris Brezillon pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
123026777d37SBoris Brezillon (int)len);
123126777d37SBoris Brezillon
123226777d37SBoris Brezillon oobsize = mtd_oobavail(mtd, ops);
123326777d37SBoris Brezillon oobcolumn = from & (mtd->oobsize - 1);
123426777d37SBoris Brezillon
123526777d37SBoris Brezillon /* Do not allow reads past end of device */
123626777d37SBoris Brezillon if ((from + len) > mtd->size) {
123726777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt read beyond end of device\n",
123826777d37SBoris Brezillon __func__);
123926777d37SBoris Brezillon ops->retlen = 0;
124026777d37SBoris Brezillon ops->oobretlen = 0;
124126777d37SBoris Brezillon return -EINVAL;
124226777d37SBoris Brezillon }
124326777d37SBoris Brezillon
124426777d37SBoris Brezillon stats = mtd->ecc_stats;
124526777d37SBoris Brezillon
124626777d37SBoris Brezillon /* Read-while-load method */
124726777d37SBoris Brezillon
124826777d37SBoris Brezillon /* Do first load to bufferRAM */
124926777d37SBoris Brezillon if (read < len) {
125026777d37SBoris Brezillon if (!onenand_check_bufferram(mtd, from)) {
125126777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_READ, from, writesize);
125226777d37SBoris Brezillon ret = this->wait(mtd, FL_READING);
125326777d37SBoris Brezillon onenand_update_bufferram(mtd, from, !ret);
125426777d37SBoris Brezillon if (mtd_is_eccerr(ret))
125526777d37SBoris Brezillon ret = 0;
125626777d37SBoris Brezillon }
125726777d37SBoris Brezillon }
125826777d37SBoris Brezillon
125926777d37SBoris Brezillon thislen = min_t(int, writesize, len - read);
126026777d37SBoris Brezillon column = from & (writesize - 1);
126126777d37SBoris Brezillon if (column + thislen > writesize)
126226777d37SBoris Brezillon thislen = writesize - column;
126326777d37SBoris Brezillon
126426777d37SBoris Brezillon while (!ret) {
126526777d37SBoris Brezillon /* If there is more to load then start next load */
126626777d37SBoris Brezillon from += thislen;
126726777d37SBoris Brezillon if (read + thislen < len) {
126826777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_READ, from, writesize);
126926777d37SBoris Brezillon /*
127026777d37SBoris Brezillon * Chip boundary handling in DDP
127126777d37SBoris Brezillon * Now we issued chip 1 read and pointed chip 1
127226777d37SBoris Brezillon * bufferram so we have to point chip 0 bufferram.
127326777d37SBoris Brezillon */
127426777d37SBoris Brezillon if (ONENAND_IS_DDP(this) &&
127526777d37SBoris Brezillon unlikely(from == (this->chipsize >> 1))) {
127626777d37SBoris Brezillon this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
127726777d37SBoris Brezillon boundary = 1;
127826777d37SBoris Brezillon } else
127926777d37SBoris Brezillon boundary = 0;
128026777d37SBoris Brezillon ONENAND_SET_PREV_BUFFERRAM(this);
128126777d37SBoris Brezillon }
128226777d37SBoris Brezillon /* While load is going, read from last bufferRAM */
128326777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
128426777d37SBoris Brezillon
128526777d37SBoris Brezillon /* Read oob area if needed */
128626777d37SBoris Brezillon if (oobbuf) {
128726777d37SBoris Brezillon thisooblen = oobsize - oobcolumn;
128826777d37SBoris Brezillon thisooblen = min_t(int, thisooblen, ooblen - oobread);
128926777d37SBoris Brezillon
129026777d37SBoris Brezillon if (ops->mode == MTD_OPS_AUTO_OOB)
129126777d37SBoris Brezillon onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
129226777d37SBoris Brezillon else
129326777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
129426777d37SBoris Brezillon oobread += thisooblen;
129526777d37SBoris Brezillon oobbuf += thisooblen;
129626777d37SBoris Brezillon oobcolumn = 0;
129726777d37SBoris Brezillon }
129826777d37SBoris Brezillon
129926777d37SBoris Brezillon /* See if we are done */
130026777d37SBoris Brezillon read += thislen;
130126777d37SBoris Brezillon if (read == len)
130226777d37SBoris Brezillon break;
130326777d37SBoris Brezillon /* Set up for next read from bufferRAM */
130426777d37SBoris Brezillon if (unlikely(boundary))
130526777d37SBoris Brezillon this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
130626777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
130726777d37SBoris Brezillon buf += thislen;
130826777d37SBoris Brezillon thislen = min_t(int, writesize, len - read);
130926777d37SBoris Brezillon column = 0;
131026777d37SBoris Brezillon cond_resched();
131126777d37SBoris Brezillon /* Now wait for load */
131226777d37SBoris Brezillon ret = this->wait(mtd, FL_READING);
131326777d37SBoris Brezillon onenand_update_bufferram(mtd, from, !ret);
131426777d37SBoris Brezillon if (mtd_is_eccerr(ret))
131526777d37SBoris Brezillon ret = 0;
131626777d37SBoris Brezillon }
131726777d37SBoris Brezillon
131826777d37SBoris Brezillon /*
131926777d37SBoris Brezillon * Return success, if no ECC failures, else -EBADMSG
132026777d37SBoris Brezillon * fs driver will take care of that, because
132126777d37SBoris Brezillon * retlen == desired len and result == -EBADMSG
132226777d37SBoris Brezillon */
132326777d37SBoris Brezillon ops->retlen = read;
132426777d37SBoris Brezillon ops->oobretlen = oobread;
132526777d37SBoris Brezillon
132626777d37SBoris Brezillon if (ret)
132726777d37SBoris Brezillon return ret;
132826777d37SBoris Brezillon
132926777d37SBoris Brezillon if (mtd->ecc_stats.failed - stats.failed)
133026777d37SBoris Brezillon return -EBADMSG;
133126777d37SBoris Brezillon
133226777d37SBoris Brezillon /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
133326777d37SBoris Brezillon return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
133426777d37SBoris Brezillon }
133526777d37SBoris Brezillon
133626777d37SBoris Brezillon /**
133726777d37SBoris Brezillon * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1338720ae74dSLee Jones * @mtd: MTD device structure
1339720ae74dSLee Jones * @from: offset to read from
1340720ae74dSLee Jones * @ops: oob operation description structure
134126777d37SBoris Brezillon *
134226777d37SBoris Brezillon * OneNAND read out-of-band data from the spare area
134326777d37SBoris Brezillon */
onenand_read_oob_nolock(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)134426777d37SBoris Brezillon static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
134526777d37SBoris Brezillon struct mtd_oob_ops *ops)
134626777d37SBoris Brezillon {
134726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
134826777d37SBoris Brezillon struct mtd_ecc_stats stats;
134926777d37SBoris Brezillon int read = 0, thislen, column, oobsize;
135026777d37SBoris Brezillon size_t len = ops->ooblen;
135126777d37SBoris Brezillon unsigned int mode = ops->mode;
135226777d37SBoris Brezillon u_char *buf = ops->oobbuf;
135326777d37SBoris Brezillon int ret = 0, readcmd;
135426777d37SBoris Brezillon
135526777d37SBoris Brezillon from += ops->ooboffs;
135626777d37SBoris Brezillon
135726777d37SBoris Brezillon pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
135826777d37SBoris Brezillon (int)len);
135926777d37SBoris Brezillon
136026777d37SBoris Brezillon /* Initialize return length value */
136126777d37SBoris Brezillon ops->oobretlen = 0;
136226777d37SBoris Brezillon
136326777d37SBoris Brezillon if (mode == MTD_OPS_AUTO_OOB)
136426777d37SBoris Brezillon oobsize = mtd->oobavail;
136526777d37SBoris Brezillon else
136626777d37SBoris Brezillon oobsize = mtd->oobsize;
136726777d37SBoris Brezillon
136826777d37SBoris Brezillon column = from & (mtd->oobsize - 1);
136926777d37SBoris Brezillon
137026777d37SBoris Brezillon if (unlikely(column >= oobsize)) {
137126777d37SBoris Brezillon printk(KERN_ERR "%s: Attempted to start read outside oob\n",
137226777d37SBoris Brezillon __func__);
137326777d37SBoris Brezillon return -EINVAL;
137426777d37SBoris Brezillon }
137526777d37SBoris Brezillon
137626777d37SBoris Brezillon stats = mtd->ecc_stats;
137726777d37SBoris Brezillon
137826777d37SBoris Brezillon readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
137926777d37SBoris Brezillon
138026777d37SBoris Brezillon while (read < len) {
138126777d37SBoris Brezillon cond_resched();
138226777d37SBoris Brezillon
138326777d37SBoris Brezillon thislen = oobsize - column;
138426777d37SBoris Brezillon thislen = min_t(int, thislen, len);
138526777d37SBoris Brezillon
138626777d37SBoris Brezillon this->command(mtd, readcmd, from, mtd->oobsize);
138726777d37SBoris Brezillon
138826777d37SBoris Brezillon onenand_update_bufferram(mtd, from, 0);
138926777d37SBoris Brezillon
139026777d37SBoris Brezillon ret = this->wait(mtd, FL_READING);
139126777d37SBoris Brezillon if (unlikely(ret))
139226777d37SBoris Brezillon ret = onenand_recover_lsb(mtd, from, ret);
139326777d37SBoris Brezillon
139426777d37SBoris Brezillon if (ret && !mtd_is_eccerr(ret)) {
139526777d37SBoris Brezillon printk(KERN_ERR "%s: read failed = 0x%x\n",
139626777d37SBoris Brezillon __func__, ret);
139726777d37SBoris Brezillon break;
139826777d37SBoris Brezillon }
139926777d37SBoris Brezillon
140026777d37SBoris Brezillon if (mode == MTD_OPS_AUTO_OOB)
140126777d37SBoris Brezillon onenand_transfer_auto_oob(mtd, buf, column, thislen);
140226777d37SBoris Brezillon else
140326777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
140426777d37SBoris Brezillon
140526777d37SBoris Brezillon read += thislen;
140626777d37SBoris Brezillon
140726777d37SBoris Brezillon if (read == len)
140826777d37SBoris Brezillon break;
140926777d37SBoris Brezillon
141026777d37SBoris Brezillon buf += thislen;
141126777d37SBoris Brezillon
141226777d37SBoris Brezillon /* Read more? */
141326777d37SBoris Brezillon if (read < len) {
141426777d37SBoris Brezillon /* Page size */
141526777d37SBoris Brezillon from += mtd->writesize;
141626777d37SBoris Brezillon column = 0;
141726777d37SBoris Brezillon }
141826777d37SBoris Brezillon }
141926777d37SBoris Brezillon
142026777d37SBoris Brezillon ops->oobretlen = read;
142126777d37SBoris Brezillon
142226777d37SBoris Brezillon if (ret)
142326777d37SBoris Brezillon return ret;
142426777d37SBoris Brezillon
142526777d37SBoris Brezillon if (mtd->ecc_stats.failed - stats.failed)
142626777d37SBoris Brezillon return -EBADMSG;
142726777d37SBoris Brezillon
142826777d37SBoris Brezillon return 0;
142926777d37SBoris Brezillon }
143026777d37SBoris Brezillon
143126777d37SBoris Brezillon /**
143226777d37SBoris Brezillon * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1433720ae74dSLee Jones * @mtd: MTD device structure
1434720ae74dSLee Jones * @from: offset to read from
1435720ae74dSLee Jones * @ops: oob operation description structure
14362dc3d1e0SLee Jones *
143726777d37SBoris Brezillon * Read main and/or out-of-band
143826777d37SBoris Brezillon */
onenand_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)143926777d37SBoris Brezillon static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
144026777d37SBoris Brezillon struct mtd_oob_ops *ops)
144126777d37SBoris Brezillon {
144226777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
14437bea6056SMichał Kępień struct mtd_ecc_stats old_stats;
144426777d37SBoris Brezillon int ret;
144526777d37SBoris Brezillon
144626777d37SBoris Brezillon switch (ops->mode) {
144726777d37SBoris Brezillon case MTD_OPS_PLACE_OOB:
144826777d37SBoris Brezillon case MTD_OPS_AUTO_OOB:
144926777d37SBoris Brezillon break;
145026777d37SBoris Brezillon case MTD_OPS_RAW:
145126777d37SBoris Brezillon /* Not implemented yet */
145226777d37SBoris Brezillon default:
145326777d37SBoris Brezillon return -EINVAL;
145426777d37SBoris Brezillon }
145526777d37SBoris Brezillon
145626777d37SBoris Brezillon onenand_get_device(mtd, FL_READING);
14577bea6056SMichał Kępień
14587bea6056SMichał Kępień old_stats = mtd->ecc_stats;
14597bea6056SMichał Kępień
146026777d37SBoris Brezillon if (ops->datbuf)
146126777d37SBoris Brezillon ret = ONENAND_IS_4KB_PAGE(this) ?
146226777d37SBoris Brezillon onenand_mlc_read_ops_nolock(mtd, from, ops) :
146326777d37SBoris Brezillon onenand_read_ops_nolock(mtd, from, ops);
146426777d37SBoris Brezillon else
146526777d37SBoris Brezillon ret = onenand_read_oob_nolock(mtd, from, ops);
14667bea6056SMichał Kępień
14677bea6056SMichał Kępień if (ops->stats) {
14687bea6056SMichał Kępień ops->stats->uncorrectable_errors +=
14697bea6056SMichał Kępień mtd->ecc_stats.failed - old_stats.failed;
14707bea6056SMichał Kępień ops->stats->corrected_bitflips +=
14717bea6056SMichał Kępień mtd->ecc_stats.corrected - old_stats.corrected;
14727bea6056SMichał Kępień }
14737bea6056SMichał Kępień
147426777d37SBoris Brezillon onenand_release_device(mtd);
147526777d37SBoris Brezillon
147626777d37SBoris Brezillon return ret;
147726777d37SBoris Brezillon }
147826777d37SBoris Brezillon
147926777d37SBoris Brezillon /**
148026777d37SBoris Brezillon * onenand_bbt_wait - [DEFAULT] wait until the command is done
1481720ae74dSLee Jones * @mtd: MTD device structure
1482720ae74dSLee Jones * @state: state to select the max. timeout value
148326777d37SBoris Brezillon *
148426777d37SBoris Brezillon * Wait for command done.
148526777d37SBoris Brezillon */
onenand_bbt_wait(struct mtd_info * mtd,int state)148626777d37SBoris Brezillon static int onenand_bbt_wait(struct mtd_info *mtd, int state)
148726777d37SBoris Brezillon {
148826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
148926777d37SBoris Brezillon unsigned long timeout;
149026777d37SBoris Brezillon unsigned int interrupt, ctrl, ecc, addr1, addr8;
149126777d37SBoris Brezillon
149226777d37SBoris Brezillon /* The 20 msec is enough */
149326777d37SBoris Brezillon timeout = jiffies + msecs_to_jiffies(20);
149426777d37SBoris Brezillon while (time_before(jiffies, timeout)) {
149526777d37SBoris Brezillon interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
149626777d37SBoris Brezillon if (interrupt & ONENAND_INT_MASTER)
149726777d37SBoris Brezillon break;
149826777d37SBoris Brezillon }
149926777d37SBoris Brezillon /* To get correct interrupt status in timeout case */
150026777d37SBoris Brezillon interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
150126777d37SBoris Brezillon ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
150226777d37SBoris Brezillon addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
150326777d37SBoris Brezillon addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
150426777d37SBoris Brezillon
150526777d37SBoris Brezillon if (interrupt & ONENAND_INT_READ) {
150626777d37SBoris Brezillon ecc = onenand_read_ecc(this);
150726777d37SBoris Brezillon if (ecc & ONENAND_ECC_2BIT_ALL) {
150826777d37SBoris Brezillon printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
150926777d37SBoris Brezillon "intr 0x%04x addr1 %#x addr8 %#x\n",
151026777d37SBoris Brezillon __func__, ecc, ctrl, interrupt, addr1, addr8);
151126777d37SBoris Brezillon return ONENAND_BBT_READ_ECC_ERROR;
151226777d37SBoris Brezillon }
151326777d37SBoris Brezillon } else {
151426777d37SBoris Brezillon printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
151526777d37SBoris Brezillon "intr 0x%04x addr1 %#x addr8 %#x\n",
151626777d37SBoris Brezillon __func__, ctrl, interrupt, addr1, addr8);
151726777d37SBoris Brezillon return ONENAND_BBT_READ_FATAL_ERROR;
151826777d37SBoris Brezillon }
151926777d37SBoris Brezillon
152026777d37SBoris Brezillon /* Initial bad block case: 0x2400 or 0x0400 */
152126777d37SBoris Brezillon if (ctrl & ONENAND_CTRL_ERROR) {
152226777d37SBoris Brezillon printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
152326777d37SBoris Brezillon "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
152426777d37SBoris Brezillon return ONENAND_BBT_READ_ERROR;
152526777d37SBoris Brezillon }
152626777d37SBoris Brezillon
152726777d37SBoris Brezillon return 0;
152826777d37SBoris Brezillon }
152926777d37SBoris Brezillon
153026777d37SBoris Brezillon /**
153126777d37SBoris Brezillon * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1532720ae74dSLee Jones * @mtd: MTD device structure
1533720ae74dSLee Jones * @from: offset to read from
1534720ae74dSLee Jones * @ops: oob operation description structure
153526777d37SBoris Brezillon *
153626777d37SBoris Brezillon * OneNAND read out-of-band data from the spare area for bbt scan
153726777d37SBoris Brezillon */
onenand_bbt_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)153826777d37SBoris Brezillon int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
153926777d37SBoris Brezillon struct mtd_oob_ops *ops)
154026777d37SBoris Brezillon {
154126777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
154226777d37SBoris Brezillon int read = 0, thislen, column;
154326777d37SBoris Brezillon int ret = 0, readcmd;
154426777d37SBoris Brezillon size_t len = ops->ooblen;
154526777d37SBoris Brezillon u_char *buf = ops->oobbuf;
154626777d37SBoris Brezillon
154726777d37SBoris Brezillon pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
154826777d37SBoris Brezillon len);
154926777d37SBoris Brezillon
155026777d37SBoris Brezillon /* Initialize return value */
155126777d37SBoris Brezillon ops->oobretlen = 0;
155226777d37SBoris Brezillon
155326777d37SBoris Brezillon /* Do not allow reads past end of device */
155426777d37SBoris Brezillon if (unlikely((from + len) > mtd->size)) {
155526777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt read beyond end of device\n",
155626777d37SBoris Brezillon __func__);
155726777d37SBoris Brezillon return ONENAND_BBT_READ_FATAL_ERROR;
155826777d37SBoris Brezillon }
155926777d37SBoris Brezillon
156026777d37SBoris Brezillon /* Grab the lock and see if the device is available */
156126777d37SBoris Brezillon onenand_get_device(mtd, FL_READING);
156226777d37SBoris Brezillon
156326777d37SBoris Brezillon column = from & (mtd->oobsize - 1);
156426777d37SBoris Brezillon
156526777d37SBoris Brezillon readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
156626777d37SBoris Brezillon
156726777d37SBoris Brezillon while (read < len) {
156826777d37SBoris Brezillon cond_resched();
156926777d37SBoris Brezillon
157026777d37SBoris Brezillon thislen = mtd->oobsize - column;
157126777d37SBoris Brezillon thislen = min_t(int, thislen, len);
157226777d37SBoris Brezillon
157326777d37SBoris Brezillon this->command(mtd, readcmd, from, mtd->oobsize);
157426777d37SBoris Brezillon
157526777d37SBoris Brezillon onenand_update_bufferram(mtd, from, 0);
157626777d37SBoris Brezillon
157726777d37SBoris Brezillon ret = this->bbt_wait(mtd, FL_READING);
157826777d37SBoris Brezillon if (unlikely(ret))
157926777d37SBoris Brezillon ret = onenand_recover_lsb(mtd, from, ret);
158026777d37SBoris Brezillon
158126777d37SBoris Brezillon if (ret)
158226777d37SBoris Brezillon break;
158326777d37SBoris Brezillon
158426777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
158526777d37SBoris Brezillon read += thislen;
158626777d37SBoris Brezillon if (read == len)
158726777d37SBoris Brezillon break;
158826777d37SBoris Brezillon
158926777d37SBoris Brezillon buf += thislen;
159026777d37SBoris Brezillon
159126777d37SBoris Brezillon /* Read more? */
159226777d37SBoris Brezillon if (read < len) {
159326777d37SBoris Brezillon /* Update Page size */
159426777d37SBoris Brezillon from += this->writesize;
159526777d37SBoris Brezillon column = 0;
159626777d37SBoris Brezillon }
159726777d37SBoris Brezillon }
159826777d37SBoris Brezillon
159926777d37SBoris Brezillon /* Deselect and wake up anyone waiting on the device */
160026777d37SBoris Brezillon onenand_release_device(mtd);
160126777d37SBoris Brezillon
160226777d37SBoris Brezillon ops->oobretlen = read;
160326777d37SBoris Brezillon return ret;
160426777d37SBoris Brezillon }
160526777d37SBoris Brezillon
160626777d37SBoris Brezillon #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
160726777d37SBoris Brezillon /**
160826777d37SBoris Brezillon * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1609720ae74dSLee Jones * @mtd: MTD device structure
1610720ae74dSLee Jones * @buf: the databuffer to verify
1611720ae74dSLee Jones * @to: offset to read from
161226777d37SBoris Brezillon */
onenand_verify_oob(struct mtd_info * mtd,const u_char * buf,loff_t to)161326777d37SBoris Brezillon static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
161426777d37SBoris Brezillon {
161526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
161626777d37SBoris Brezillon u_char *oob_buf = this->oob_buf;
161726777d37SBoris Brezillon int status, i, readcmd;
161826777d37SBoris Brezillon
161926777d37SBoris Brezillon readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
162026777d37SBoris Brezillon
162126777d37SBoris Brezillon this->command(mtd, readcmd, to, mtd->oobsize);
162226777d37SBoris Brezillon onenand_update_bufferram(mtd, to, 0);
162326777d37SBoris Brezillon status = this->wait(mtd, FL_READING);
162426777d37SBoris Brezillon if (status)
162526777d37SBoris Brezillon return status;
162626777d37SBoris Brezillon
162726777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
162826777d37SBoris Brezillon for (i = 0; i < mtd->oobsize; i++)
162926777d37SBoris Brezillon if (buf[i] != 0xFF && buf[i] != oob_buf[i])
163026777d37SBoris Brezillon return -EBADMSG;
163126777d37SBoris Brezillon
163226777d37SBoris Brezillon return 0;
163326777d37SBoris Brezillon }
163426777d37SBoris Brezillon
163526777d37SBoris Brezillon /**
163626777d37SBoris Brezillon * onenand_verify - [GENERIC] verify the chip contents after a write
1637720ae74dSLee Jones * @mtd: MTD device structure
1638720ae74dSLee Jones * @buf: the databuffer to verify
1639720ae74dSLee Jones * @addr: offset to read from
1640720ae74dSLee Jones * @len: number of bytes to read and compare
164126777d37SBoris Brezillon */
onenand_verify(struct mtd_info * mtd,const u_char * buf,loff_t addr,size_t len)164226777d37SBoris Brezillon static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
164326777d37SBoris Brezillon {
164426777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
164526777d37SBoris Brezillon int ret = 0;
164626777d37SBoris Brezillon int thislen, column;
164726777d37SBoris Brezillon
164826777d37SBoris Brezillon column = addr & (this->writesize - 1);
164926777d37SBoris Brezillon
165026777d37SBoris Brezillon while (len != 0) {
165126777d37SBoris Brezillon thislen = min_t(int, this->writesize - column, len);
165226777d37SBoris Brezillon
165326777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
165426777d37SBoris Brezillon
165526777d37SBoris Brezillon onenand_update_bufferram(mtd, addr, 0);
165626777d37SBoris Brezillon
165726777d37SBoris Brezillon ret = this->wait(mtd, FL_READING);
165826777d37SBoris Brezillon if (ret)
165926777d37SBoris Brezillon return ret;
166026777d37SBoris Brezillon
166126777d37SBoris Brezillon onenand_update_bufferram(mtd, addr, 1);
166226777d37SBoris Brezillon
166326777d37SBoris Brezillon this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
166426777d37SBoris Brezillon
166526777d37SBoris Brezillon if (memcmp(buf, this->verify_buf + column, thislen))
166626777d37SBoris Brezillon return -EBADMSG;
166726777d37SBoris Brezillon
166826777d37SBoris Brezillon len -= thislen;
166926777d37SBoris Brezillon buf += thislen;
167026777d37SBoris Brezillon addr += thislen;
167126777d37SBoris Brezillon column = 0;
167226777d37SBoris Brezillon }
167326777d37SBoris Brezillon
167426777d37SBoris Brezillon return 0;
167526777d37SBoris Brezillon }
167626777d37SBoris Brezillon #else
167726777d37SBoris Brezillon #define onenand_verify(...) (0)
167826777d37SBoris Brezillon #define onenand_verify_oob(...) (0)
167926777d37SBoris Brezillon #endif
168026777d37SBoris Brezillon
168126777d37SBoris Brezillon #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
168226777d37SBoris Brezillon
onenand_panic_wait(struct mtd_info * mtd)168326777d37SBoris Brezillon static void onenand_panic_wait(struct mtd_info *mtd)
168426777d37SBoris Brezillon {
168526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
168626777d37SBoris Brezillon unsigned int interrupt;
168726777d37SBoris Brezillon int i;
168826777d37SBoris Brezillon
168926777d37SBoris Brezillon for (i = 0; i < 2000; i++) {
169026777d37SBoris Brezillon interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
169126777d37SBoris Brezillon if (interrupt & ONENAND_INT_MASTER)
169226777d37SBoris Brezillon break;
169326777d37SBoris Brezillon udelay(10);
169426777d37SBoris Brezillon }
169526777d37SBoris Brezillon }
169626777d37SBoris Brezillon
169726777d37SBoris Brezillon /**
169826777d37SBoris Brezillon * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1699720ae74dSLee Jones * @mtd: MTD device structure
1700720ae74dSLee Jones * @to: offset to write to
1701720ae74dSLee Jones * @len: number of bytes to write
1702720ae74dSLee Jones * @retlen: pointer to variable to store the number of written bytes
1703720ae74dSLee Jones * @buf: the data to write
170426777d37SBoris Brezillon *
170526777d37SBoris Brezillon * Write with ECC
170626777d37SBoris Brezillon */
onenand_panic_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)170726777d37SBoris Brezillon static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
170826777d37SBoris Brezillon size_t *retlen, const u_char *buf)
170926777d37SBoris Brezillon {
171026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
171126777d37SBoris Brezillon int column, subpage;
171226777d37SBoris Brezillon int written = 0;
171326777d37SBoris Brezillon
171426777d37SBoris Brezillon if (this->state == FL_PM_SUSPENDED)
171526777d37SBoris Brezillon return -EBUSY;
171626777d37SBoris Brezillon
171726777d37SBoris Brezillon /* Wait for any existing operation to clear */
171826777d37SBoris Brezillon onenand_panic_wait(mtd);
171926777d37SBoris Brezillon
172026777d37SBoris Brezillon pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
172126777d37SBoris Brezillon (int)len);
172226777d37SBoris Brezillon
172326777d37SBoris Brezillon /* Reject writes, which are not page aligned */
172426777d37SBoris Brezillon if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
172526777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
172626777d37SBoris Brezillon __func__);
172726777d37SBoris Brezillon return -EINVAL;
172826777d37SBoris Brezillon }
172926777d37SBoris Brezillon
173026777d37SBoris Brezillon column = to & (mtd->writesize - 1);
173126777d37SBoris Brezillon
173226777d37SBoris Brezillon /* Loop until all data write */
173326777d37SBoris Brezillon while (written < len) {
173426777d37SBoris Brezillon int thislen = min_t(int, mtd->writesize - column, len - written);
173526777d37SBoris Brezillon u_char *wbuf = (u_char *) buf;
173626777d37SBoris Brezillon
173726777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
173826777d37SBoris Brezillon
173926777d37SBoris Brezillon /* Partial page write */
174026777d37SBoris Brezillon subpage = thislen < mtd->writesize;
174126777d37SBoris Brezillon if (subpage) {
174226777d37SBoris Brezillon memset(this->page_buf, 0xff, mtd->writesize);
174326777d37SBoris Brezillon memcpy(this->page_buf + column, buf, thislen);
174426777d37SBoris Brezillon wbuf = this->page_buf;
174526777d37SBoris Brezillon }
174626777d37SBoris Brezillon
174726777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
174826777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
174926777d37SBoris Brezillon
175026777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
175126777d37SBoris Brezillon
175226777d37SBoris Brezillon onenand_panic_wait(mtd);
175326777d37SBoris Brezillon
175426777d37SBoris Brezillon /* In partial page write we don't update bufferram */
175526777d37SBoris Brezillon onenand_update_bufferram(mtd, to, !subpage);
175626777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
175726777d37SBoris Brezillon ONENAND_SET_BUFFERRAM1(this);
175826777d37SBoris Brezillon onenand_update_bufferram(mtd, to + this->writesize, !subpage);
175926777d37SBoris Brezillon }
176026777d37SBoris Brezillon
176126777d37SBoris Brezillon written += thislen;
176226777d37SBoris Brezillon
176326777d37SBoris Brezillon if (written == len)
176426777d37SBoris Brezillon break;
176526777d37SBoris Brezillon
176626777d37SBoris Brezillon column = 0;
176726777d37SBoris Brezillon to += thislen;
176826777d37SBoris Brezillon buf += thislen;
176926777d37SBoris Brezillon }
177026777d37SBoris Brezillon
177126777d37SBoris Brezillon *retlen = written;
177226777d37SBoris Brezillon return 0;
177326777d37SBoris Brezillon }
177426777d37SBoris Brezillon
177526777d37SBoris Brezillon /**
177626777d37SBoris Brezillon * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
1777720ae74dSLee Jones * @mtd: MTD device structure
1778720ae74dSLee Jones * @oob_buf: oob buffer
1779720ae74dSLee Jones * @buf: source address
1780720ae74dSLee Jones * @column: oob offset to write to
1781720ae74dSLee Jones * @thislen: oob length to write
178226777d37SBoris Brezillon */
onenand_fill_auto_oob(struct mtd_info * mtd,u_char * oob_buf,const u_char * buf,int column,int thislen)178326777d37SBoris Brezillon static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
178426777d37SBoris Brezillon const u_char *buf, int column, int thislen)
178526777d37SBoris Brezillon {
178626777d37SBoris Brezillon return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen);
178726777d37SBoris Brezillon }
178826777d37SBoris Brezillon
178926777d37SBoris Brezillon /**
179026777d37SBoris Brezillon * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1791720ae74dSLee Jones * @mtd: MTD device structure
1792720ae74dSLee Jones * @to: offset to write to
1793720ae74dSLee Jones * @ops: oob operation description structure
179426777d37SBoris Brezillon *
179526777d37SBoris Brezillon * Write main and/or oob with ECC
179626777d37SBoris Brezillon */
onenand_write_ops_nolock(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)179726777d37SBoris Brezillon static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
179826777d37SBoris Brezillon struct mtd_oob_ops *ops)
179926777d37SBoris Brezillon {
180026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
180126777d37SBoris Brezillon int written = 0, column, thislen = 0, subpage = 0;
180226777d37SBoris Brezillon int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
180326777d37SBoris Brezillon int oobwritten = 0, oobcolumn, thisooblen, oobsize;
180426777d37SBoris Brezillon size_t len = ops->len;
180526777d37SBoris Brezillon size_t ooblen = ops->ooblen;
180626777d37SBoris Brezillon const u_char *buf = ops->datbuf;
180726777d37SBoris Brezillon const u_char *oob = ops->oobbuf;
180826777d37SBoris Brezillon u_char *oobbuf;
180926777d37SBoris Brezillon int ret = 0, cmd;
181026777d37SBoris Brezillon
181126777d37SBoris Brezillon pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
181226777d37SBoris Brezillon (int)len);
181326777d37SBoris Brezillon
181426777d37SBoris Brezillon /* Initialize retlen, in case of early exit */
181526777d37SBoris Brezillon ops->retlen = 0;
181626777d37SBoris Brezillon ops->oobretlen = 0;
181726777d37SBoris Brezillon
181826777d37SBoris Brezillon /* Reject writes, which are not page aligned */
181926777d37SBoris Brezillon if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
182026777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
182126777d37SBoris Brezillon __func__);
182226777d37SBoris Brezillon return -EINVAL;
182326777d37SBoris Brezillon }
182426777d37SBoris Brezillon
182526777d37SBoris Brezillon /* Check zero length */
182626777d37SBoris Brezillon if (!len)
182726777d37SBoris Brezillon return 0;
182826777d37SBoris Brezillon oobsize = mtd_oobavail(mtd, ops);
182926777d37SBoris Brezillon oobcolumn = to & (mtd->oobsize - 1);
183026777d37SBoris Brezillon
183126777d37SBoris Brezillon column = to & (mtd->writesize - 1);
183226777d37SBoris Brezillon
183326777d37SBoris Brezillon /* Loop until all data write */
183426777d37SBoris Brezillon while (1) {
183526777d37SBoris Brezillon if (written < len) {
183626777d37SBoris Brezillon u_char *wbuf = (u_char *) buf;
183726777d37SBoris Brezillon
183826777d37SBoris Brezillon thislen = min_t(int, mtd->writesize - column, len - written);
183926777d37SBoris Brezillon thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
184026777d37SBoris Brezillon
184126777d37SBoris Brezillon cond_resched();
184226777d37SBoris Brezillon
184326777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
184426777d37SBoris Brezillon
184526777d37SBoris Brezillon /* Partial page write */
184626777d37SBoris Brezillon subpage = thislen < mtd->writesize;
184726777d37SBoris Brezillon if (subpage) {
184826777d37SBoris Brezillon memset(this->page_buf, 0xff, mtd->writesize);
184926777d37SBoris Brezillon memcpy(this->page_buf + column, buf, thislen);
185026777d37SBoris Brezillon wbuf = this->page_buf;
185126777d37SBoris Brezillon }
185226777d37SBoris Brezillon
185326777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
185426777d37SBoris Brezillon
185526777d37SBoris Brezillon if (oob) {
185626777d37SBoris Brezillon oobbuf = this->oob_buf;
185726777d37SBoris Brezillon
185826777d37SBoris Brezillon /* We send data to spare ram with oobsize
185926777d37SBoris Brezillon * to prevent byte access */
186026777d37SBoris Brezillon memset(oobbuf, 0xff, mtd->oobsize);
186126777d37SBoris Brezillon if (ops->mode == MTD_OPS_AUTO_OOB)
186226777d37SBoris Brezillon onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
186326777d37SBoris Brezillon else
186426777d37SBoris Brezillon memcpy(oobbuf + oobcolumn, oob, thisooblen);
186526777d37SBoris Brezillon
186626777d37SBoris Brezillon oobwritten += thisooblen;
186726777d37SBoris Brezillon oob += thisooblen;
186826777d37SBoris Brezillon oobcolumn = 0;
186926777d37SBoris Brezillon } else
187026777d37SBoris Brezillon oobbuf = (u_char *) ffchars;
187126777d37SBoris Brezillon
187226777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
187326777d37SBoris Brezillon } else
187426777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
187526777d37SBoris Brezillon
187626777d37SBoris Brezillon /*
187726777d37SBoris Brezillon * 2 PLANE, MLC, and Flex-OneNAND do not support
187826777d37SBoris Brezillon * write-while-program feature.
187926777d37SBoris Brezillon */
188026777d37SBoris Brezillon if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
188126777d37SBoris Brezillon ONENAND_SET_PREV_BUFFERRAM(this);
188226777d37SBoris Brezillon
188326777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
188426777d37SBoris Brezillon
188526777d37SBoris Brezillon /* In partial page write we don't update bufferram */
188626777d37SBoris Brezillon onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
188726777d37SBoris Brezillon if (ret) {
188826777d37SBoris Brezillon written -= prevlen;
188926777d37SBoris Brezillon printk(KERN_ERR "%s: write failed %d\n",
189026777d37SBoris Brezillon __func__, ret);
189126777d37SBoris Brezillon break;
189226777d37SBoris Brezillon }
189326777d37SBoris Brezillon
189426777d37SBoris Brezillon if (written == len) {
189526777d37SBoris Brezillon /* Only check verify write turn on */
189626777d37SBoris Brezillon ret = onenand_verify(mtd, buf - len, to - len, len);
189726777d37SBoris Brezillon if (ret)
189826777d37SBoris Brezillon printk(KERN_ERR "%s: verify failed %d\n",
189926777d37SBoris Brezillon __func__, ret);
190026777d37SBoris Brezillon break;
190126777d37SBoris Brezillon }
190226777d37SBoris Brezillon
190326777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
190426777d37SBoris Brezillon }
190526777d37SBoris Brezillon
190626777d37SBoris Brezillon this->ongoing = 0;
190726777d37SBoris Brezillon cmd = ONENAND_CMD_PROG;
190826777d37SBoris Brezillon
190926777d37SBoris Brezillon /* Exclude 1st OTP and OTP blocks for cache program feature */
191026777d37SBoris Brezillon if (ONENAND_IS_CACHE_PROGRAM(this) &&
191126777d37SBoris Brezillon likely(onenand_block(this, to) != 0) &&
191226777d37SBoris Brezillon ONENAND_IS_4KB_PAGE(this) &&
191326777d37SBoris Brezillon ((written + thislen) < len)) {
191426777d37SBoris Brezillon cmd = ONENAND_CMD_2X_CACHE_PROG;
191526777d37SBoris Brezillon this->ongoing = 1;
191626777d37SBoris Brezillon }
191726777d37SBoris Brezillon
191826777d37SBoris Brezillon this->command(mtd, cmd, to, mtd->writesize);
191926777d37SBoris Brezillon
192026777d37SBoris Brezillon /*
192126777d37SBoris Brezillon * 2 PLANE, MLC, and Flex-OneNAND wait here
192226777d37SBoris Brezillon */
192326777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
192426777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
192526777d37SBoris Brezillon
192626777d37SBoris Brezillon /* In partial page write we don't update bufferram */
192726777d37SBoris Brezillon onenand_update_bufferram(mtd, to, !ret && !subpage);
192826777d37SBoris Brezillon if (ret) {
192926777d37SBoris Brezillon printk(KERN_ERR "%s: write failed %d\n",
193026777d37SBoris Brezillon __func__, ret);
193126777d37SBoris Brezillon break;
193226777d37SBoris Brezillon }
193326777d37SBoris Brezillon
193426777d37SBoris Brezillon /* Only check verify write turn on */
193526777d37SBoris Brezillon ret = onenand_verify(mtd, buf, to, thislen);
193626777d37SBoris Brezillon if (ret) {
193726777d37SBoris Brezillon printk(KERN_ERR "%s: verify failed %d\n",
193826777d37SBoris Brezillon __func__, ret);
193926777d37SBoris Brezillon break;
194026777d37SBoris Brezillon }
194126777d37SBoris Brezillon
194226777d37SBoris Brezillon written += thislen;
194326777d37SBoris Brezillon
194426777d37SBoris Brezillon if (written == len)
194526777d37SBoris Brezillon break;
194626777d37SBoris Brezillon
194726777d37SBoris Brezillon } else
194826777d37SBoris Brezillon written += thislen;
194926777d37SBoris Brezillon
195026777d37SBoris Brezillon column = 0;
195126777d37SBoris Brezillon prev_subpage = subpage;
195226777d37SBoris Brezillon prev = to;
195326777d37SBoris Brezillon prevlen = thislen;
195426777d37SBoris Brezillon to += thislen;
195526777d37SBoris Brezillon buf += thislen;
195626777d37SBoris Brezillon first = 0;
195726777d37SBoris Brezillon }
195826777d37SBoris Brezillon
195926777d37SBoris Brezillon /* In error case, clear all bufferrams */
196026777d37SBoris Brezillon if (written != len)
196126777d37SBoris Brezillon onenand_invalidate_bufferram(mtd, 0, -1);
196226777d37SBoris Brezillon
196326777d37SBoris Brezillon ops->retlen = written;
196426777d37SBoris Brezillon ops->oobretlen = oobwritten;
196526777d37SBoris Brezillon
196626777d37SBoris Brezillon return ret;
196726777d37SBoris Brezillon }
196826777d37SBoris Brezillon
196926777d37SBoris Brezillon
197026777d37SBoris Brezillon /**
197126777d37SBoris Brezillon * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
1972720ae74dSLee Jones * @mtd: MTD device structure
1973720ae74dSLee Jones * @to: offset to write to
19742dc3d1e0SLee Jones * @ops: oob operation description structure
197526777d37SBoris Brezillon *
197626777d37SBoris Brezillon * OneNAND write out-of-band
197726777d37SBoris Brezillon */
onenand_write_oob_nolock(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)197826777d37SBoris Brezillon static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
197926777d37SBoris Brezillon struct mtd_oob_ops *ops)
198026777d37SBoris Brezillon {
198126777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
198226777d37SBoris Brezillon int column, ret = 0, oobsize;
198326777d37SBoris Brezillon int written = 0, oobcmd;
198426777d37SBoris Brezillon u_char *oobbuf;
198526777d37SBoris Brezillon size_t len = ops->ooblen;
198626777d37SBoris Brezillon const u_char *buf = ops->oobbuf;
198726777d37SBoris Brezillon unsigned int mode = ops->mode;
198826777d37SBoris Brezillon
198926777d37SBoris Brezillon to += ops->ooboffs;
199026777d37SBoris Brezillon
199126777d37SBoris Brezillon pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
199226777d37SBoris Brezillon (int)len);
199326777d37SBoris Brezillon
199426777d37SBoris Brezillon /* Initialize retlen, in case of early exit */
199526777d37SBoris Brezillon ops->oobretlen = 0;
199626777d37SBoris Brezillon
199726777d37SBoris Brezillon if (mode == MTD_OPS_AUTO_OOB)
199826777d37SBoris Brezillon oobsize = mtd->oobavail;
199926777d37SBoris Brezillon else
200026777d37SBoris Brezillon oobsize = mtd->oobsize;
200126777d37SBoris Brezillon
200226777d37SBoris Brezillon column = to & (mtd->oobsize - 1);
200326777d37SBoris Brezillon
200426777d37SBoris Brezillon if (unlikely(column >= oobsize)) {
200526777d37SBoris Brezillon printk(KERN_ERR "%s: Attempted to start write outside oob\n",
200626777d37SBoris Brezillon __func__);
200726777d37SBoris Brezillon return -EINVAL;
200826777d37SBoris Brezillon }
200926777d37SBoris Brezillon
201026777d37SBoris Brezillon /* For compatibility with NAND: Do not allow write past end of page */
201126777d37SBoris Brezillon if (unlikely(column + len > oobsize)) {
201226777d37SBoris Brezillon printk(KERN_ERR "%s: Attempt to write past end of page\n",
201326777d37SBoris Brezillon __func__);
201426777d37SBoris Brezillon return -EINVAL;
201526777d37SBoris Brezillon }
201626777d37SBoris Brezillon
201726777d37SBoris Brezillon oobbuf = this->oob_buf;
201826777d37SBoris Brezillon
201926777d37SBoris Brezillon oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
202026777d37SBoris Brezillon
202126777d37SBoris Brezillon /* Loop until all data write */
202226777d37SBoris Brezillon while (written < len) {
202326777d37SBoris Brezillon int thislen = min_t(int, oobsize, len - written);
202426777d37SBoris Brezillon
202526777d37SBoris Brezillon cond_resched();
202626777d37SBoris Brezillon
202726777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
202826777d37SBoris Brezillon
202926777d37SBoris Brezillon /* We send data to spare ram with oobsize
203026777d37SBoris Brezillon * to prevent byte access */
203126777d37SBoris Brezillon memset(oobbuf, 0xff, mtd->oobsize);
203226777d37SBoris Brezillon if (mode == MTD_OPS_AUTO_OOB)
203326777d37SBoris Brezillon onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
203426777d37SBoris Brezillon else
203526777d37SBoris Brezillon memcpy(oobbuf + column, buf, thislen);
203626777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
203726777d37SBoris Brezillon
203826777d37SBoris Brezillon if (ONENAND_IS_4KB_PAGE(this)) {
203926777d37SBoris Brezillon /* Set main area of DataRAM to 0xff*/
204026777d37SBoris Brezillon memset(this->page_buf, 0xff, mtd->writesize);
204126777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_DATARAM,
204226777d37SBoris Brezillon this->page_buf, 0, mtd->writesize);
204326777d37SBoris Brezillon }
204426777d37SBoris Brezillon
204526777d37SBoris Brezillon this->command(mtd, oobcmd, to, mtd->oobsize);
204626777d37SBoris Brezillon
204726777d37SBoris Brezillon onenand_update_bufferram(mtd, to, 0);
204826777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
204926777d37SBoris Brezillon ONENAND_SET_BUFFERRAM1(this);
205026777d37SBoris Brezillon onenand_update_bufferram(mtd, to + this->writesize, 0);
205126777d37SBoris Brezillon }
205226777d37SBoris Brezillon
205326777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
205426777d37SBoris Brezillon if (ret) {
205526777d37SBoris Brezillon printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
205626777d37SBoris Brezillon break;
205726777d37SBoris Brezillon }
205826777d37SBoris Brezillon
205926777d37SBoris Brezillon ret = onenand_verify_oob(mtd, oobbuf, to);
206026777d37SBoris Brezillon if (ret) {
206126777d37SBoris Brezillon printk(KERN_ERR "%s: verify failed %d\n",
206226777d37SBoris Brezillon __func__, ret);
206326777d37SBoris Brezillon break;
206426777d37SBoris Brezillon }
206526777d37SBoris Brezillon
206626777d37SBoris Brezillon written += thislen;
206726777d37SBoris Brezillon if (written == len)
206826777d37SBoris Brezillon break;
206926777d37SBoris Brezillon
207026777d37SBoris Brezillon to += mtd->writesize;
207126777d37SBoris Brezillon buf += thislen;
207226777d37SBoris Brezillon column = 0;
207326777d37SBoris Brezillon }
207426777d37SBoris Brezillon
207526777d37SBoris Brezillon ops->oobretlen = written;
207626777d37SBoris Brezillon
207726777d37SBoris Brezillon return ret;
207826777d37SBoris Brezillon }
207926777d37SBoris Brezillon
208026777d37SBoris Brezillon /**
208126777d37SBoris Brezillon * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2082720ae74dSLee Jones * @mtd: MTD device structure
2083720ae74dSLee Jones * @to: offset to write
2084720ae74dSLee Jones * @ops: oob operation description structure
208526777d37SBoris Brezillon */
onenand_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)208626777d37SBoris Brezillon static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
208726777d37SBoris Brezillon struct mtd_oob_ops *ops)
208826777d37SBoris Brezillon {
208926777d37SBoris Brezillon int ret;
209026777d37SBoris Brezillon
209126777d37SBoris Brezillon switch (ops->mode) {
209226777d37SBoris Brezillon case MTD_OPS_PLACE_OOB:
209326777d37SBoris Brezillon case MTD_OPS_AUTO_OOB:
209426777d37SBoris Brezillon break;
209526777d37SBoris Brezillon case MTD_OPS_RAW:
209626777d37SBoris Brezillon /* Not implemented yet */
209726777d37SBoris Brezillon default:
209826777d37SBoris Brezillon return -EINVAL;
209926777d37SBoris Brezillon }
210026777d37SBoris Brezillon
210126777d37SBoris Brezillon onenand_get_device(mtd, FL_WRITING);
210226777d37SBoris Brezillon if (ops->datbuf)
210326777d37SBoris Brezillon ret = onenand_write_ops_nolock(mtd, to, ops);
210426777d37SBoris Brezillon else
210526777d37SBoris Brezillon ret = onenand_write_oob_nolock(mtd, to, ops);
210626777d37SBoris Brezillon onenand_release_device(mtd);
210726777d37SBoris Brezillon
210826777d37SBoris Brezillon return ret;
210926777d37SBoris Brezillon }
211026777d37SBoris Brezillon
211126777d37SBoris Brezillon /**
211226777d37SBoris Brezillon * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2113720ae74dSLee Jones * @mtd: MTD device structure
2114720ae74dSLee Jones * @ofs: offset from device start
2115720ae74dSLee Jones * @allowbbt: 1, if its allowed to access the bbt area
211626777d37SBoris Brezillon *
211726777d37SBoris Brezillon * Check, if the block is bad. Either by reading the bad block table or
211826777d37SBoris Brezillon * calling of the scan function.
211926777d37SBoris Brezillon */
onenand_block_isbad_nolock(struct mtd_info * mtd,loff_t ofs,int allowbbt)212026777d37SBoris Brezillon static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
212126777d37SBoris Brezillon {
212226777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
212326777d37SBoris Brezillon struct bbm_info *bbm = this->bbm;
212426777d37SBoris Brezillon
212526777d37SBoris Brezillon /* Return info from the table */
212626777d37SBoris Brezillon return bbm->isbad_bbt(mtd, ofs, allowbbt);
212726777d37SBoris Brezillon }
212826777d37SBoris Brezillon
212926777d37SBoris Brezillon
onenand_multiblock_erase_verify(struct mtd_info * mtd,struct erase_info * instr)213026777d37SBoris Brezillon static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
213126777d37SBoris Brezillon struct erase_info *instr)
213226777d37SBoris Brezillon {
213326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
213426777d37SBoris Brezillon loff_t addr = instr->addr;
213526777d37SBoris Brezillon int len = instr->len;
213626777d37SBoris Brezillon unsigned int block_size = (1 << this->erase_shift);
213726777d37SBoris Brezillon int ret = 0;
213826777d37SBoris Brezillon
213926777d37SBoris Brezillon while (len) {
214026777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
214126777d37SBoris Brezillon ret = this->wait(mtd, FL_VERIFYING_ERASE);
214226777d37SBoris Brezillon if (ret) {
214326777d37SBoris Brezillon printk(KERN_ERR "%s: Failed verify, block %d\n",
214426777d37SBoris Brezillon __func__, onenand_block(this, addr));
214526777d37SBoris Brezillon instr->fail_addr = addr;
214626777d37SBoris Brezillon return -1;
214726777d37SBoris Brezillon }
214826777d37SBoris Brezillon len -= block_size;
214926777d37SBoris Brezillon addr += block_size;
215026777d37SBoris Brezillon }
215126777d37SBoris Brezillon return 0;
215226777d37SBoris Brezillon }
215326777d37SBoris Brezillon
215426777d37SBoris Brezillon /**
215526777d37SBoris Brezillon * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
2156720ae74dSLee Jones * @mtd: MTD device structure
2157720ae74dSLee Jones * @instr: erase instruction
21582dc3d1e0SLee Jones * @block_size: block size
215926777d37SBoris Brezillon *
216026777d37SBoris Brezillon * Erase one or more blocks up to 64 block at a time
216126777d37SBoris Brezillon */
onenand_multiblock_erase(struct mtd_info * mtd,struct erase_info * instr,unsigned int block_size)216226777d37SBoris Brezillon static int onenand_multiblock_erase(struct mtd_info *mtd,
216326777d37SBoris Brezillon struct erase_info *instr,
216426777d37SBoris Brezillon unsigned int block_size)
216526777d37SBoris Brezillon {
216626777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
216726777d37SBoris Brezillon loff_t addr = instr->addr;
216826777d37SBoris Brezillon int len = instr->len;
216926777d37SBoris Brezillon int eb_count = 0;
217026777d37SBoris Brezillon int ret = 0;
217126777d37SBoris Brezillon int bdry_block = 0;
217226777d37SBoris Brezillon
217326777d37SBoris Brezillon if (ONENAND_IS_DDP(this)) {
217426777d37SBoris Brezillon loff_t bdry_addr = this->chipsize >> 1;
217526777d37SBoris Brezillon if (addr < bdry_addr && (addr + len) > bdry_addr)
217626777d37SBoris Brezillon bdry_block = bdry_addr >> this->erase_shift;
217726777d37SBoris Brezillon }
217826777d37SBoris Brezillon
217926777d37SBoris Brezillon /* Pre-check bbs */
218026777d37SBoris Brezillon while (len) {
218126777d37SBoris Brezillon /* Check if we have a bad block, we do not erase bad blocks */
218226777d37SBoris Brezillon if (onenand_block_isbad_nolock(mtd, addr, 0)) {
218326777d37SBoris Brezillon printk(KERN_WARNING "%s: attempt to erase a bad block "
218426777d37SBoris Brezillon "at addr 0x%012llx\n",
218526777d37SBoris Brezillon __func__, (unsigned long long) addr);
218626777d37SBoris Brezillon return -EIO;
218726777d37SBoris Brezillon }
218826777d37SBoris Brezillon len -= block_size;
218926777d37SBoris Brezillon addr += block_size;
219026777d37SBoris Brezillon }
219126777d37SBoris Brezillon
219226777d37SBoris Brezillon len = instr->len;
219326777d37SBoris Brezillon addr = instr->addr;
219426777d37SBoris Brezillon
219526777d37SBoris Brezillon /* loop over 64 eb batches */
219626777d37SBoris Brezillon while (len) {
219726777d37SBoris Brezillon struct erase_info verify_instr = *instr;
219826777d37SBoris Brezillon int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
219926777d37SBoris Brezillon
220026777d37SBoris Brezillon verify_instr.addr = addr;
220126777d37SBoris Brezillon verify_instr.len = 0;
220226777d37SBoris Brezillon
220326777d37SBoris Brezillon /* do not cross chip boundary */
220426777d37SBoris Brezillon if (bdry_block) {
220526777d37SBoris Brezillon int this_block = (addr >> this->erase_shift);
220626777d37SBoris Brezillon
220726777d37SBoris Brezillon if (this_block < bdry_block) {
220826777d37SBoris Brezillon max_eb_count = min(max_eb_count,
220926777d37SBoris Brezillon (bdry_block - this_block));
221026777d37SBoris Brezillon }
221126777d37SBoris Brezillon }
221226777d37SBoris Brezillon
221326777d37SBoris Brezillon eb_count = 0;
221426777d37SBoris Brezillon
221526777d37SBoris Brezillon while (len > block_size && eb_count < (max_eb_count - 1)) {
221626777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
221726777d37SBoris Brezillon addr, block_size);
221826777d37SBoris Brezillon onenand_invalidate_bufferram(mtd, addr, block_size);
221926777d37SBoris Brezillon
222026777d37SBoris Brezillon ret = this->wait(mtd, FL_PREPARING_ERASE);
222126777d37SBoris Brezillon if (ret) {
222226777d37SBoris Brezillon printk(KERN_ERR "%s: Failed multiblock erase, "
222326777d37SBoris Brezillon "block %d\n", __func__,
222426777d37SBoris Brezillon onenand_block(this, addr));
222526777d37SBoris Brezillon instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
222626777d37SBoris Brezillon return -EIO;
222726777d37SBoris Brezillon }
222826777d37SBoris Brezillon
222926777d37SBoris Brezillon len -= block_size;
223026777d37SBoris Brezillon addr += block_size;
223126777d37SBoris Brezillon eb_count++;
223226777d37SBoris Brezillon }
223326777d37SBoris Brezillon
223426777d37SBoris Brezillon /* last block of 64-eb series */
223526777d37SBoris Brezillon cond_resched();
223626777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
223726777d37SBoris Brezillon onenand_invalidate_bufferram(mtd, addr, block_size);
223826777d37SBoris Brezillon
223926777d37SBoris Brezillon ret = this->wait(mtd, FL_ERASING);
224026777d37SBoris Brezillon /* Check if it is write protected */
224126777d37SBoris Brezillon if (ret) {
224226777d37SBoris Brezillon printk(KERN_ERR "%s: Failed erase, block %d\n",
224326777d37SBoris Brezillon __func__, onenand_block(this, addr));
224426777d37SBoris Brezillon instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
224526777d37SBoris Brezillon return -EIO;
224626777d37SBoris Brezillon }
224726777d37SBoris Brezillon
224826777d37SBoris Brezillon len -= block_size;
224926777d37SBoris Brezillon addr += block_size;
225026777d37SBoris Brezillon eb_count++;
225126777d37SBoris Brezillon
225226777d37SBoris Brezillon /* verify */
225326777d37SBoris Brezillon verify_instr.len = eb_count * block_size;
225426777d37SBoris Brezillon if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
225526777d37SBoris Brezillon instr->fail_addr = verify_instr.fail_addr;
225626777d37SBoris Brezillon return -EIO;
225726777d37SBoris Brezillon }
225826777d37SBoris Brezillon
225926777d37SBoris Brezillon }
226026777d37SBoris Brezillon return 0;
226126777d37SBoris Brezillon }
226226777d37SBoris Brezillon
226326777d37SBoris Brezillon
226426777d37SBoris Brezillon /**
226526777d37SBoris Brezillon * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
2266720ae74dSLee Jones * @mtd: MTD device structure
2267720ae74dSLee Jones * @instr: erase instruction
2268720ae74dSLee Jones * @region: erase region
2269720ae74dSLee Jones * @block_size: erase block size
227026777d37SBoris Brezillon *
227126777d37SBoris Brezillon * Erase one or more blocks one block at a time
227226777d37SBoris Brezillon */
onenand_block_by_block_erase(struct mtd_info * mtd,struct erase_info * instr,struct mtd_erase_region_info * region,unsigned int block_size)227326777d37SBoris Brezillon static int onenand_block_by_block_erase(struct mtd_info *mtd,
227426777d37SBoris Brezillon struct erase_info *instr,
227526777d37SBoris Brezillon struct mtd_erase_region_info *region,
227626777d37SBoris Brezillon unsigned int block_size)
227726777d37SBoris Brezillon {
227826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
227926777d37SBoris Brezillon loff_t addr = instr->addr;
228026777d37SBoris Brezillon int len = instr->len;
228126777d37SBoris Brezillon loff_t region_end = 0;
228226777d37SBoris Brezillon int ret = 0;
228326777d37SBoris Brezillon
228426777d37SBoris Brezillon if (region) {
228526777d37SBoris Brezillon /* region is set for Flex-OneNAND */
228626777d37SBoris Brezillon region_end = region->offset + region->erasesize * region->numblocks;
228726777d37SBoris Brezillon }
228826777d37SBoris Brezillon
228926777d37SBoris Brezillon /* Loop through the blocks */
229026777d37SBoris Brezillon while (len) {
229126777d37SBoris Brezillon cond_resched();
229226777d37SBoris Brezillon
229326777d37SBoris Brezillon /* Check if we have a bad block, we do not erase bad blocks */
229426777d37SBoris Brezillon if (onenand_block_isbad_nolock(mtd, addr, 0)) {
229526777d37SBoris Brezillon printk(KERN_WARNING "%s: attempt to erase a bad block "
229626777d37SBoris Brezillon "at addr 0x%012llx\n",
229726777d37SBoris Brezillon __func__, (unsigned long long) addr);
229826777d37SBoris Brezillon return -EIO;
229926777d37SBoris Brezillon }
230026777d37SBoris Brezillon
230126777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
230226777d37SBoris Brezillon
230326777d37SBoris Brezillon onenand_invalidate_bufferram(mtd, addr, block_size);
230426777d37SBoris Brezillon
230526777d37SBoris Brezillon ret = this->wait(mtd, FL_ERASING);
230626777d37SBoris Brezillon /* Check, if it is write protected */
230726777d37SBoris Brezillon if (ret) {
230826777d37SBoris Brezillon printk(KERN_ERR "%s: Failed erase, block %d\n",
230926777d37SBoris Brezillon __func__, onenand_block(this, addr));
231026777d37SBoris Brezillon instr->fail_addr = addr;
231126777d37SBoris Brezillon return -EIO;
231226777d37SBoris Brezillon }
231326777d37SBoris Brezillon
231426777d37SBoris Brezillon len -= block_size;
231526777d37SBoris Brezillon addr += block_size;
231626777d37SBoris Brezillon
231726777d37SBoris Brezillon if (region && addr == region_end) {
231826777d37SBoris Brezillon if (!len)
231926777d37SBoris Brezillon break;
232026777d37SBoris Brezillon region++;
232126777d37SBoris Brezillon
232226777d37SBoris Brezillon block_size = region->erasesize;
232326777d37SBoris Brezillon region_end = region->offset + region->erasesize * region->numblocks;
232426777d37SBoris Brezillon
232526777d37SBoris Brezillon if (len & (block_size - 1)) {
232626777d37SBoris Brezillon /* FIXME: This should be handled at MTD partitioning level. */
232726777d37SBoris Brezillon printk(KERN_ERR "%s: Unaligned address\n",
232826777d37SBoris Brezillon __func__);
232926777d37SBoris Brezillon return -EIO;
233026777d37SBoris Brezillon }
233126777d37SBoris Brezillon }
233226777d37SBoris Brezillon }
233326777d37SBoris Brezillon return 0;
233426777d37SBoris Brezillon }
233526777d37SBoris Brezillon
233626777d37SBoris Brezillon /**
233726777d37SBoris Brezillon * onenand_erase - [MTD Interface] erase block(s)
2338720ae74dSLee Jones * @mtd: MTD device structure
2339720ae74dSLee Jones * @instr: erase instruction
234026777d37SBoris Brezillon *
234126777d37SBoris Brezillon * Erase one or more blocks
234226777d37SBoris Brezillon */
onenand_erase(struct mtd_info * mtd,struct erase_info * instr)234326777d37SBoris Brezillon static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
234426777d37SBoris Brezillon {
234526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
234626777d37SBoris Brezillon unsigned int block_size;
234726777d37SBoris Brezillon loff_t addr = instr->addr;
234826777d37SBoris Brezillon loff_t len = instr->len;
234926777d37SBoris Brezillon int ret = 0;
235026777d37SBoris Brezillon struct mtd_erase_region_info *region = NULL;
235126777d37SBoris Brezillon loff_t region_offset = 0;
235226777d37SBoris Brezillon
235326777d37SBoris Brezillon pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
235426777d37SBoris Brezillon (unsigned long long)instr->addr,
235526777d37SBoris Brezillon (unsigned long long)instr->len);
235626777d37SBoris Brezillon
235726777d37SBoris Brezillon if (FLEXONENAND(this)) {
235826777d37SBoris Brezillon /* Find the eraseregion of this address */
235926777d37SBoris Brezillon int i = flexonenand_region(mtd, addr);
236026777d37SBoris Brezillon
236126777d37SBoris Brezillon region = &mtd->eraseregions[i];
236226777d37SBoris Brezillon block_size = region->erasesize;
236326777d37SBoris Brezillon
236426777d37SBoris Brezillon /* Start address within region must align on block boundary.
236526777d37SBoris Brezillon * Erase region's start offset is always block start address.
236626777d37SBoris Brezillon */
236726777d37SBoris Brezillon region_offset = region->offset;
236826777d37SBoris Brezillon } else
236926777d37SBoris Brezillon block_size = 1 << this->erase_shift;
237026777d37SBoris Brezillon
237126777d37SBoris Brezillon /* Start address must align on block boundary */
237226777d37SBoris Brezillon if (unlikely((addr - region_offset) & (block_size - 1))) {
237326777d37SBoris Brezillon printk(KERN_ERR "%s: Unaligned address\n", __func__);
237426777d37SBoris Brezillon return -EINVAL;
237526777d37SBoris Brezillon }
237626777d37SBoris Brezillon
237726777d37SBoris Brezillon /* Length must align on block boundary */
237826777d37SBoris Brezillon if (unlikely(len & (block_size - 1))) {
237926777d37SBoris Brezillon printk(KERN_ERR "%s: Length not block aligned\n", __func__);
238026777d37SBoris Brezillon return -EINVAL;
238126777d37SBoris Brezillon }
238226777d37SBoris Brezillon
238326777d37SBoris Brezillon /* Grab the lock and see if the device is available */
238426777d37SBoris Brezillon onenand_get_device(mtd, FL_ERASING);
238526777d37SBoris Brezillon
238626777d37SBoris Brezillon if (ONENAND_IS_4KB_PAGE(this) || region ||
238726777d37SBoris Brezillon instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
238826777d37SBoris Brezillon /* region is set for Flex-OneNAND (no mb erase) */
238926777d37SBoris Brezillon ret = onenand_block_by_block_erase(mtd, instr,
239026777d37SBoris Brezillon region, block_size);
239126777d37SBoris Brezillon } else {
239226777d37SBoris Brezillon ret = onenand_multiblock_erase(mtd, instr, block_size);
239326777d37SBoris Brezillon }
239426777d37SBoris Brezillon
239526777d37SBoris Brezillon /* Deselect and wake up anyone waiting on the device */
239626777d37SBoris Brezillon onenand_release_device(mtd);
239726777d37SBoris Brezillon
239826777d37SBoris Brezillon return ret;
239926777d37SBoris Brezillon }
240026777d37SBoris Brezillon
240126777d37SBoris Brezillon /**
240226777d37SBoris Brezillon * onenand_sync - [MTD Interface] sync
2403720ae74dSLee Jones * @mtd: MTD device structure
240426777d37SBoris Brezillon *
240526777d37SBoris Brezillon * Sync is actually a wait for chip ready function
240626777d37SBoris Brezillon */
onenand_sync(struct mtd_info * mtd)240726777d37SBoris Brezillon static void onenand_sync(struct mtd_info *mtd)
240826777d37SBoris Brezillon {
240926777d37SBoris Brezillon pr_debug("%s: called\n", __func__);
241026777d37SBoris Brezillon
241126777d37SBoris Brezillon /* Grab the lock and see if the device is available */
241226777d37SBoris Brezillon onenand_get_device(mtd, FL_SYNCING);
241326777d37SBoris Brezillon
241426777d37SBoris Brezillon /* Release it and go back */
241526777d37SBoris Brezillon onenand_release_device(mtd);
241626777d37SBoris Brezillon }
241726777d37SBoris Brezillon
241826777d37SBoris Brezillon /**
241926777d37SBoris Brezillon * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2420720ae74dSLee Jones * @mtd: MTD device structure
2421720ae74dSLee Jones * @ofs: offset relative to mtd start
242226777d37SBoris Brezillon *
242326777d37SBoris Brezillon * Check whether the block is bad
242426777d37SBoris Brezillon */
onenand_block_isbad(struct mtd_info * mtd,loff_t ofs)242526777d37SBoris Brezillon static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
242626777d37SBoris Brezillon {
242726777d37SBoris Brezillon int ret;
242826777d37SBoris Brezillon
242926777d37SBoris Brezillon onenand_get_device(mtd, FL_READING);
243026777d37SBoris Brezillon ret = onenand_block_isbad_nolock(mtd, ofs, 0);
243126777d37SBoris Brezillon onenand_release_device(mtd);
243226777d37SBoris Brezillon return ret;
243326777d37SBoris Brezillon }
243426777d37SBoris Brezillon
243526777d37SBoris Brezillon /**
243626777d37SBoris Brezillon * onenand_default_block_markbad - [DEFAULT] mark a block bad
2437720ae74dSLee Jones * @mtd: MTD device structure
2438720ae74dSLee Jones * @ofs: offset from device start
243926777d37SBoris Brezillon *
244026777d37SBoris Brezillon * This is the default implementation, which can be overridden by
244126777d37SBoris Brezillon * a hardware specific driver.
244226777d37SBoris Brezillon */
onenand_default_block_markbad(struct mtd_info * mtd,loff_t ofs)244326777d37SBoris Brezillon static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
244426777d37SBoris Brezillon {
244526777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
244626777d37SBoris Brezillon struct bbm_info *bbm = this->bbm;
244726777d37SBoris Brezillon u_char buf[2] = {0, 0};
244826777d37SBoris Brezillon struct mtd_oob_ops ops = {
244926777d37SBoris Brezillon .mode = MTD_OPS_PLACE_OOB,
245026777d37SBoris Brezillon .ooblen = 2,
245126777d37SBoris Brezillon .oobbuf = buf,
245226777d37SBoris Brezillon .ooboffs = 0,
245326777d37SBoris Brezillon };
245426777d37SBoris Brezillon int block;
245526777d37SBoris Brezillon
245626777d37SBoris Brezillon /* Get block number */
245726777d37SBoris Brezillon block = onenand_block(this, ofs);
245826777d37SBoris Brezillon if (bbm->bbt)
245926777d37SBoris Brezillon bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
246026777d37SBoris Brezillon
246126777d37SBoris Brezillon /* We write two bytes, so we don't have to mess with 16-bit access */
2462bfd15c90SFrieder Schrempf ofs += mtd->oobsize + (this->badblockpos & ~0x01);
246326777d37SBoris Brezillon /* FIXME : What to do when marking SLC block in partition
246426777d37SBoris Brezillon * with MLC erasesize? For now, it is not advisable to
246526777d37SBoris Brezillon * create partitions containing both SLC and MLC regions.
246626777d37SBoris Brezillon */
246726777d37SBoris Brezillon return onenand_write_oob_nolock(mtd, ofs, &ops);
246826777d37SBoris Brezillon }
246926777d37SBoris Brezillon
247026777d37SBoris Brezillon /**
247126777d37SBoris Brezillon * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2472720ae74dSLee Jones * @mtd: MTD device structure
2473720ae74dSLee Jones * @ofs: offset relative to mtd start
247426777d37SBoris Brezillon *
247526777d37SBoris Brezillon * Mark the block as bad
247626777d37SBoris Brezillon */
onenand_block_markbad(struct mtd_info * mtd,loff_t ofs)247726777d37SBoris Brezillon static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
247826777d37SBoris Brezillon {
247926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
248026777d37SBoris Brezillon int ret;
248126777d37SBoris Brezillon
248226777d37SBoris Brezillon ret = onenand_block_isbad(mtd, ofs);
248326777d37SBoris Brezillon if (ret) {
248426777d37SBoris Brezillon /* If it was bad already, return success and do nothing */
248526777d37SBoris Brezillon if (ret > 0)
248626777d37SBoris Brezillon return 0;
248726777d37SBoris Brezillon return ret;
248826777d37SBoris Brezillon }
248926777d37SBoris Brezillon
249026777d37SBoris Brezillon onenand_get_device(mtd, FL_WRITING);
249126777d37SBoris Brezillon ret = this->block_markbad(mtd, ofs);
249226777d37SBoris Brezillon onenand_release_device(mtd);
249326777d37SBoris Brezillon return ret;
249426777d37SBoris Brezillon }
249526777d37SBoris Brezillon
249626777d37SBoris Brezillon /**
249726777d37SBoris Brezillon * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2498720ae74dSLee Jones * @mtd: MTD device structure
2499720ae74dSLee Jones * @ofs: offset relative to mtd start
2500720ae74dSLee Jones * @len: number of bytes to lock or unlock
2501720ae74dSLee Jones * @cmd: lock or unlock command
250226777d37SBoris Brezillon *
250326777d37SBoris Brezillon * Lock or unlock one or more blocks
250426777d37SBoris Brezillon */
onenand_do_lock_cmd(struct mtd_info * mtd,loff_t ofs,size_t len,int cmd)250526777d37SBoris Brezillon static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
250626777d37SBoris Brezillon {
250726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
250826777d37SBoris Brezillon int start, end, block, value, status;
250926777d37SBoris Brezillon int wp_status_mask;
251026777d37SBoris Brezillon
251126777d37SBoris Brezillon start = onenand_block(this, ofs);
251226777d37SBoris Brezillon end = onenand_block(this, ofs + len) - 1;
251326777d37SBoris Brezillon
251426777d37SBoris Brezillon if (cmd == ONENAND_CMD_LOCK)
251526777d37SBoris Brezillon wp_status_mask = ONENAND_WP_LS;
251626777d37SBoris Brezillon else
251726777d37SBoris Brezillon wp_status_mask = ONENAND_WP_US;
251826777d37SBoris Brezillon
251926777d37SBoris Brezillon /* Continuous lock scheme */
252026777d37SBoris Brezillon if (this->options & ONENAND_HAS_CONT_LOCK) {
252126777d37SBoris Brezillon /* Set start block address */
252226777d37SBoris Brezillon this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
252326777d37SBoris Brezillon /* Set end block address */
252426777d37SBoris Brezillon this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
252526777d37SBoris Brezillon /* Write lock command */
252626777d37SBoris Brezillon this->command(mtd, cmd, 0, 0);
252726777d37SBoris Brezillon
252826777d37SBoris Brezillon /* There's no return value */
252926777d37SBoris Brezillon this->wait(mtd, FL_LOCKING);
253026777d37SBoris Brezillon
253126777d37SBoris Brezillon /* Sanity check */
253226777d37SBoris Brezillon while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
253326777d37SBoris Brezillon & ONENAND_CTRL_ONGO)
253426777d37SBoris Brezillon continue;
253526777d37SBoris Brezillon
253626777d37SBoris Brezillon /* Check lock status */
253726777d37SBoris Brezillon status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
253826777d37SBoris Brezillon if (!(status & wp_status_mask))
253926777d37SBoris Brezillon printk(KERN_ERR "%s: wp status = 0x%x\n",
254026777d37SBoris Brezillon __func__, status);
254126777d37SBoris Brezillon
254226777d37SBoris Brezillon return 0;
254326777d37SBoris Brezillon }
254426777d37SBoris Brezillon
254526777d37SBoris Brezillon /* Block lock scheme */
254626777d37SBoris Brezillon for (block = start; block < end + 1; block++) {
254726777d37SBoris Brezillon /* Set block address */
254826777d37SBoris Brezillon value = onenand_block_address(this, block);
254926777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
255026777d37SBoris Brezillon /* Select DataRAM for DDP */
255126777d37SBoris Brezillon value = onenand_bufferram_address(this, block);
255226777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
255326777d37SBoris Brezillon /* Set start block address */
255426777d37SBoris Brezillon this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
255526777d37SBoris Brezillon /* Write lock command */
255626777d37SBoris Brezillon this->command(mtd, cmd, 0, 0);
255726777d37SBoris Brezillon
255826777d37SBoris Brezillon /* There's no return value */
255926777d37SBoris Brezillon this->wait(mtd, FL_LOCKING);
256026777d37SBoris Brezillon
256126777d37SBoris Brezillon /* Sanity check */
256226777d37SBoris Brezillon while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
256326777d37SBoris Brezillon & ONENAND_CTRL_ONGO)
256426777d37SBoris Brezillon continue;
256526777d37SBoris Brezillon
256626777d37SBoris Brezillon /* Check lock status */
256726777d37SBoris Brezillon status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
256826777d37SBoris Brezillon if (!(status & wp_status_mask))
256926777d37SBoris Brezillon printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
257026777d37SBoris Brezillon __func__, block, status);
257126777d37SBoris Brezillon }
257226777d37SBoris Brezillon
257326777d37SBoris Brezillon return 0;
257426777d37SBoris Brezillon }
257526777d37SBoris Brezillon
257626777d37SBoris Brezillon /**
257726777d37SBoris Brezillon * onenand_lock - [MTD Interface] Lock block(s)
2578720ae74dSLee Jones * @mtd: MTD device structure
2579720ae74dSLee Jones * @ofs: offset relative to mtd start
2580720ae74dSLee Jones * @len: number of bytes to unlock
258126777d37SBoris Brezillon *
258226777d37SBoris Brezillon * Lock one or more blocks
258326777d37SBoris Brezillon */
onenand_lock(struct mtd_info * mtd,loff_t ofs,uint64_t len)258426777d37SBoris Brezillon static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
258526777d37SBoris Brezillon {
258626777d37SBoris Brezillon int ret;
258726777d37SBoris Brezillon
258826777d37SBoris Brezillon onenand_get_device(mtd, FL_LOCKING);
258926777d37SBoris Brezillon ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
259026777d37SBoris Brezillon onenand_release_device(mtd);
259126777d37SBoris Brezillon return ret;
259226777d37SBoris Brezillon }
259326777d37SBoris Brezillon
259426777d37SBoris Brezillon /**
259526777d37SBoris Brezillon * onenand_unlock - [MTD Interface] Unlock block(s)
2596720ae74dSLee Jones * @mtd: MTD device structure
2597720ae74dSLee Jones * @ofs: offset relative to mtd start
2598720ae74dSLee Jones * @len: number of bytes to unlock
259926777d37SBoris Brezillon *
260026777d37SBoris Brezillon * Unlock one or more blocks
260126777d37SBoris Brezillon */
onenand_unlock(struct mtd_info * mtd,loff_t ofs,uint64_t len)260226777d37SBoris Brezillon static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
260326777d37SBoris Brezillon {
260426777d37SBoris Brezillon int ret;
260526777d37SBoris Brezillon
260626777d37SBoris Brezillon onenand_get_device(mtd, FL_LOCKING);
260726777d37SBoris Brezillon ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
260826777d37SBoris Brezillon onenand_release_device(mtd);
260926777d37SBoris Brezillon return ret;
261026777d37SBoris Brezillon }
261126777d37SBoris Brezillon
261226777d37SBoris Brezillon /**
261326777d37SBoris Brezillon * onenand_check_lock_status - [OneNAND Interface] Check lock status
2614720ae74dSLee Jones * @this: onenand chip data structure
261526777d37SBoris Brezillon *
261626777d37SBoris Brezillon * Check lock status
261726777d37SBoris Brezillon */
onenand_check_lock_status(struct onenand_chip * this)261826777d37SBoris Brezillon static int onenand_check_lock_status(struct onenand_chip *this)
261926777d37SBoris Brezillon {
262026777d37SBoris Brezillon unsigned int value, block, status;
262126777d37SBoris Brezillon unsigned int end;
262226777d37SBoris Brezillon
262326777d37SBoris Brezillon end = this->chipsize >> this->erase_shift;
262426777d37SBoris Brezillon for (block = 0; block < end; block++) {
262526777d37SBoris Brezillon /* Set block address */
262626777d37SBoris Brezillon value = onenand_block_address(this, block);
262726777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
262826777d37SBoris Brezillon /* Select DataRAM for DDP */
262926777d37SBoris Brezillon value = onenand_bufferram_address(this, block);
263026777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
263126777d37SBoris Brezillon /* Set start block address */
263226777d37SBoris Brezillon this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
263326777d37SBoris Brezillon
263426777d37SBoris Brezillon /* Check lock status */
263526777d37SBoris Brezillon status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
263626777d37SBoris Brezillon if (!(status & ONENAND_WP_US)) {
263726777d37SBoris Brezillon printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
263826777d37SBoris Brezillon __func__, block, status);
263926777d37SBoris Brezillon return 0;
264026777d37SBoris Brezillon }
264126777d37SBoris Brezillon }
264226777d37SBoris Brezillon
264326777d37SBoris Brezillon return 1;
264426777d37SBoris Brezillon }
264526777d37SBoris Brezillon
264626777d37SBoris Brezillon /**
264726777d37SBoris Brezillon * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2648720ae74dSLee Jones * @mtd: MTD device structure
264926777d37SBoris Brezillon *
265026777d37SBoris Brezillon * Unlock all blocks
265126777d37SBoris Brezillon */
onenand_unlock_all(struct mtd_info * mtd)265226777d37SBoris Brezillon static void onenand_unlock_all(struct mtd_info *mtd)
265326777d37SBoris Brezillon {
265426777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
265526777d37SBoris Brezillon loff_t ofs = 0;
265626777d37SBoris Brezillon loff_t len = mtd->size;
265726777d37SBoris Brezillon
265826777d37SBoris Brezillon if (this->options & ONENAND_HAS_UNLOCK_ALL) {
265926777d37SBoris Brezillon /* Set start block address */
266026777d37SBoris Brezillon this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
266126777d37SBoris Brezillon /* Write unlock command */
266226777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
266326777d37SBoris Brezillon
266426777d37SBoris Brezillon /* There's no return value */
266526777d37SBoris Brezillon this->wait(mtd, FL_LOCKING);
266626777d37SBoris Brezillon
266726777d37SBoris Brezillon /* Sanity check */
266826777d37SBoris Brezillon while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
266926777d37SBoris Brezillon & ONENAND_CTRL_ONGO)
267026777d37SBoris Brezillon continue;
267126777d37SBoris Brezillon
267226777d37SBoris Brezillon /* Don't check lock status */
267326777d37SBoris Brezillon if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
267426777d37SBoris Brezillon return;
267526777d37SBoris Brezillon
267626777d37SBoris Brezillon /* Check lock status */
267726777d37SBoris Brezillon if (onenand_check_lock_status(this))
267826777d37SBoris Brezillon return;
267926777d37SBoris Brezillon
268026777d37SBoris Brezillon /* Workaround for all block unlock in DDP */
268126777d37SBoris Brezillon if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
268226777d37SBoris Brezillon /* All blocks on another chip */
268326777d37SBoris Brezillon ofs = this->chipsize >> 1;
268426777d37SBoris Brezillon len = this->chipsize >> 1;
268526777d37SBoris Brezillon }
268626777d37SBoris Brezillon }
268726777d37SBoris Brezillon
268826777d37SBoris Brezillon onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
268926777d37SBoris Brezillon }
269026777d37SBoris Brezillon
269126777d37SBoris Brezillon #ifdef CONFIG_MTD_ONENAND_OTP
269226777d37SBoris Brezillon
269326777d37SBoris Brezillon /**
269426777d37SBoris Brezillon * onenand_otp_command - Send OTP specific command to OneNAND device
2695720ae74dSLee Jones * @mtd: MTD device structure
2696720ae74dSLee Jones * @cmd: the command to be sent
2697720ae74dSLee Jones * @addr: offset to read from or write to
2698720ae74dSLee Jones * @len: number of bytes to read or write
269926777d37SBoris Brezillon */
onenand_otp_command(struct mtd_info * mtd,int cmd,loff_t addr,size_t len)270026777d37SBoris Brezillon static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
270126777d37SBoris Brezillon size_t len)
270226777d37SBoris Brezillon {
270326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
270426777d37SBoris Brezillon int value, block, page;
270526777d37SBoris Brezillon
270626777d37SBoris Brezillon /* Address translation */
270726777d37SBoris Brezillon switch (cmd) {
270826777d37SBoris Brezillon case ONENAND_CMD_OTP_ACCESS:
270926777d37SBoris Brezillon block = (int) (addr >> this->erase_shift);
271026777d37SBoris Brezillon page = -1;
271126777d37SBoris Brezillon break;
271226777d37SBoris Brezillon
271326777d37SBoris Brezillon default:
271426777d37SBoris Brezillon block = (int) (addr >> this->erase_shift);
271526777d37SBoris Brezillon page = (int) (addr >> this->page_shift);
271626777d37SBoris Brezillon
271726777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
271826777d37SBoris Brezillon /* Make the even block number */
271926777d37SBoris Brezillon block &= ~1;
272026777d37SBoris Brezillon /* Is it the odd plane? */
272126777d37SBoris Brezillon if (addr & this->writesize)
272226777d37SBoris Brezillon block++;
272326777d37SBoris Brezillon page >>= 1;
272426777d37SBoris Brezillon }
272526777d37SBoris Brezillon page &= this->page_mask;
272626777d37SBoris Brezillon break;
272726777d37SBoris Brezillon }
272826777d37SBoris Brezillon
272926777d37SBoris Brezillon if (block != -1) {
273026777d37SBoris Brezillon /* Write 'DFS, FBA' of Flash */
273126777d37SBoris Brezillon value = onenand_block_address(this, block);
273226777d37SBoris Brezillon this->write_word(value, this->base +
273326777d37SBoris Brezillon ONENAND_REG_START_ADDRESS1);
273426777d37SBoris Brezillon }
273526777d37SBoris Brezillon
273626777d37SBoris Brezillon if (page != -1) {
273726777d37SBoris Brezillon /* Now we use page size operation */
273826777d37SBoris Brezillon int sectors = 4, count = 4;
273926777d37SBoris Brezillon int dataram;
274026777d37SBoris Brezillon
274126777d37SBoris Brezillon switch (cmd) {
274226777d37SBoris Brezillon default:
274326777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
274426777d37SBoris Brezillon cmd = ONENAND_CMD_2X_PROG;
274526777d37SBoris Brezillon dataram = ONENAND_CURRENT_BUFFERRAM(this);
274626777d37SBoris Brezillon break;
274726777d37SBoris Brezillon }
274826777d37SBoris Brezillon
274926777d37SBoris Brezillon /* Write 'FPA, FSA' of Flash */
275026777d37SBoris Brezillon value = onenand_page_address(page, sectors);
275126777d37SBoris Brezillon this->write_word(value, this->base +
275226777d37SBoris Brezillon ONENAND_REG_START_ADDRESS8);
275326777d37SBoris Brezillon
275426777d37SBoris Brezillon /* Write 'BSA, BSC' of DataRAM */
275526777d37SBoris Brezillon value = onenand_buffer_address(dataram, sectors, count);
275626777d37SBoris Brezillon this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
275726777d37SBoris Brezillon }
275826777d37SBoris Brezillon
275926777d37SBoris Brezillon /* Interrupt clear */
276026777d37SBoris Brezillon this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
276126777d37SBoris Brezillon
276226777d37SBoris Brezillon /* Write command */
276326777d37SBoris Brezillon this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
276426777d37SBoris Brezillon
276526777d37SBoris Brezillon return 0;
276626777d37SBoris Brezillon }
276726777d37SBoris Brezillon
276826777d37SBoris Brezillon /**
276926777d37SBoris Brezillon * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
2770720ae74dSLee Jones * @mtd: MTD device structure
2771720ae74dSLee Jones * @to: offset to write to
27722dc3d1e0SLee Jones * @ops: oob operation description structure
277326777d37SBoris Brezillon *
277426777d37SBoris Brezillon * OneNAND write out-of-band only for OTP
277526777d37SBoris Brezillon */
onenand_otp_write_oob_nolock(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)277626777d37SBoris Brezillon static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
277726777d37SBoris Brezillon struct mtd_oob_ops *ops)
277826777d37SBoris Brezillon {
277926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
278026777d37SBoris Brezillon int column, ret = 0, oobsize;
278126777d37SBoris Brezillon int written = 0;
278226777d37SBoris Brezillon u_char *oobbuf;
278326777d37SBoris Brezillon size_t len = ops->ooblen;
278426777d37SBoris Brezillon const u_char *buf = ops->oobbuf;
278526777d37SBoris Brezillon int block, value, status;
278626777d37SBoris Brezillon
278726777d37SBoris Brezillon to += ops->ooboffs;
278826777d37SBoris Brezillon
278926777d37SBoris Brezillon /* Initialize retlen, in case of early exit */
279026777d37SBoris Brezillon ops->oobretlen = 0;
279126777d37SBoris Brezillon
279226777d37SBoris Brezillon oobsize = mtd->oobsize;
279326777d37SBoris Brezillon
279426777d37SBoris Brezillon column = to & (mtd->oobsize - 1);
279526777d37SBoris Brezillon
279626777d37SBoris Brezillon oobbuf = this->oob_buf;
279726777d37SBoris Brezillon
279826777d37SBoris Brezillon /* Loop until all data write */
279926777d37SBoris Brezillon while (written < len) {
280026777d37SBoris Brezillon int thislen = min_t(int, oobsize, len - written);
280126777d37SBoris Brezillon
280226777d37SBoris Brezillon cond_resched();
280326777d37SBoris Brezillon
280426777d37SBoris Brezillon block = (int) (to >> this->erase_shift);
280526777d37SBoris Brezillon /*
280626777d37SBoris Brezillon * Write 'DFS, FBA' of Flash
280726777d37SBoris Brezillon * Add: F100h DQ=DFS, FBA
280826777d37SBoris Brezillon */
280926777d37SBoris Brezillon
281026777d37SBoris Brezillon value = onenand_block_address(this, block);
281126777d37SBoris Brezillon this->write_word(value, this->base +
281226777d37SBoris Brezillon ONENAND_REG_START_ADDRESS1);
281326777d37SBoris Brezillon
281426777d37SBoris Brezillon /*
281526777d37SBoris Brezillon * Select DataRAM for DDP
281626777d37SBoris Brezillon * Add: F101h DQ=DBS
281726777d37SBoris Brezillon */
281826777d37SBoris Brezillon
281926777d37SBoris Brezillon value = onenand_bufferram_address(this, block);
282026777d37SBoris Brezillon this->write_word(value, this->base +
282126777d37SBoris Brezillon ONENAND_REG_START_ADDRESS2);
282226777d37SBoris Brezillon ONENAND_SET_NEXT_BUFFERRAM(this);
282326777d37SBoris Brezillon
282426777d37SBoris Brezillon /*
282526777d37SBoris Brezillon * Enter OTP access mode
282626777d37SBoris Brezillon */
282726777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
282826777d37SBoris Brezillon this->wait(mtd, FL_OTPING);
282926777d37SBoris Brezillon
283026777d37SBoris Brezillon /* We send data to spare ram with oobsize
283126777d37SBoris Brezillon * to prevent byte access */
283226777d37SBoris Brezillon memcpy(oobbuf + column, buf, thislen);
283326777d37SBoris Brezillon
283426777d37SBoris Brezillon /*
283526777d37SBoris Brezillon * Write Data into DataRAM
283626777d37SBoris Brezillon * Add: 8th Word
283726777d37SBoris Brezillon * in sector0/spare/page0
283826777d37SBoris Brezillon * DQ=XXFCh
283926777d37SBoris Brezillon */
284026777d37SBoris Brezillon this->write_bufferram(mtd, ONENAND_SPARERAM,
284126777d37SBoris Brezillon oobbuf, 0, mtd->oobsize);
284226777d37SBoris Brezillon
284326777d37SBoris Brezillon onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
284426777d37SBoris Brezillon onenand_update_bufferram(mtd, to, 0);
284526777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
284626777d37SBoris Brezillon ONENAND_SET_BUFFERRAM1(this);
284726777d37SBoris Brezillon onenand_update_bufferram(mtd, to + this->writesize, 0);
284826777d37SBoris Brezillon }
284926777d37SBoris Brezillon
285026777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
285126777d37SBoris Brezillon if (ret) {
285226777d37SBoris Brezillon printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
285326777d37SBoris Brezillon break;
285426777d37SBoris Brezillon }
285526777d37SBoris Brezillon
285626777d37SBoris Brezillon /* Exit OTP access mode */
285726777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_RESET, 0, 0);
285844f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
285926777d37SBoris Brezillon
286026777d37SBoris Brezillon status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
286126777d37SBoris Brezillon status &= 0x60;
286226777d37SBoris Brezillon
286326777d37SBoris Brezillon if (status == 0x60) {
286426777d37SBoris Brezillon printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
286526777d37SBoris Brezillon printk(KERN_DEBUG "1st Block\tLOCKED\n");
286626777d37SBoris Brezillon printk(KERN_DEBUG "OTP Block\tLOCKED\n");
286726777d37SBoris Brezillon } else if (status == 0x20) {
286826777d37SBoris Brezillon printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
286926777d37SBoris Brezillon printk(KERN_DEBUG "1st Block\tLOCKED\n");
287026777d37SBoris Brezillon printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
287126777d37SBoris Brezillon } else if (status == 0x40) {
287226777d37SBoris Brezillon printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
287326777d37SBoris Brezillon printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
287426777d37SBoris Brezillon printk(KERN_DEBUG "OTP Block\tLOCKED\n");
287526777d37SBoris Brezillon } else {
287626777d37SBoris Brezillon printk(KERN_DEBUG "Reboot to check\n");
287726777d37SBoris Brezillon }
287826777d37SBoris Brezillon
287926777d37SBoris Brezillon written += thislen;
288026777d37SBoris Brezillon if (written == len)
288126777d37SBoris Brezillon break;
288226777d37SBoris Brezillon
288326777d37SBoris Brezillon to += mtd->writesize;
288426777d37SBoris Brezillon buf += thislen;
288526777d37SBoris Brezillon column = 0;
288626777d37SBoris Brezillon }
288726777d37SBoris Brezillon
288826777d37SBoris Brezillon ops->oobretlen = written;
288926777d37SBoris Brezillon
289026777d37SBoris Brezillon return ret;
289126777d37SBoris Brezillon }
289226777d37SBoris Brezillon
289326777d37SBoris Brezillon /* Internal OTP operation */
289426777d37SBoris Brezillon typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
289526777d37SBoris Brezillon size_t *retlen, u_char *buf);
289626777d37SBoris Brezillon
289726777d37SBoris Brezillon /**
289826777d37SBoris Brezillon * do_otp_read - [DEFAULT] Read OTP block area
2899720ae74dSLee Jones * @mtd: MTD device structure
2900720ae74dSLee Jones * @from: The offset to read
2901720ae74dSLee Jones * @len: number of bytes to read
2902720ae74dSLee Jones * @retlen: pointer to variable to store the number of readbytes
2903720ae74dSLee Jones * @buf: the databuffer to put/get data
290426777d37SBoris Brezillon *
290526777d37SBoris Brezillon * Read OTP block area.
290626777d37SBoris Brezillon */
do_otp_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)290726777d37SBoris Brezillon static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
290826777d37SBoris Brezillon size_t *retlen, u_char *buf)
290926777d37SBoris Brezillon {
291026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
291126777d37SBoris Brezillon struct mtd_oob_ops ops = {
291226777d37SBoris Brezillon .len = len,
291326777d37SBoris Brezillon .ooblen = 0,
291426777d37SBoris Brezillon .datbuf = buf,
291526777d37SBoris Brezillon .oobbuf = NULL,
291626777d37SBoris Brezillon };
291726777d37SBoris Brezillon int ret;
291826777d37SBoris Brezillon
291926777d37SBoris Brezillon /* Enter OTP access mode */
292026777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
292126777d37SBoris Brezillon this->wait(mtd, FL_OTPING);
292226777d37SBoris Brezillon
292326777d37SBoris Brezillon ret = ONENAND_IS_4KB_PAGE(this) ?
292426777d37SBoris Brezillon onenand_mlc_read_ops_nolock(mtd, from, &ops) :
292526777d37SBoris Brezillon onenand_read_ops_nolock(mtd, from, &ops);
2926*5d19ca5dSIvan Stepchenko *retlen = ops.retlen;
292726777d37SBoris Brezillon
292826777d37SBoris Brezillon /* Exit OTP access mode */
292926777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_RESET, 0, 0);
293044f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
293126777d37SBoris Brezillon
293226777d37SBoris Brezillon return ret;
293326777d37SBoris Brezillon }
293426777d37SBoris Brezillon
293526777d37SBoris Brezillon /**
293626777d37SBoris Brezillon * do_otp_write - [DEFAULT] Write OTP block area
2937720ae74dSLee Jones * @mtd: MTD device structure
2938720ae74dSLee Jones * @to: The offset to write
2939720ae74dSLee Jones * @len: number of bytes to write
2940720ae74dSLee Jones * @retlen: pointer to variable to store the number of write bytes
2941720ae74dSLee Jones * @buf: the databuffer to put/get data
294226777d37SBoris Brezillon *
294326777d37SBoris Brezillon * Write OTP block area.
294426777d37SBoris Brezillon */
do_otp_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,u_char * buf)294526777d37SBoris Brezillon static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
294626777d37SBoris Brezillon size_t *retlen, u_char *buf)
294726777d37SBoris Brezillon {
294826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
294926777d37SBoris Brezillon unsigned char *pbuf = buf;
295026777d37SBoris Brezillon int ret;
2951745df179SMichał Kępień struct mtd_oob_ops ops = { };
295226777d37SBoris Brezillon
295326777d37SBoris Brezillon /* Force buffer page aligned */
295426777d37SBoris Brezillon if (len < mtd->writesize) {
295526777d37SBoris Brezillon memcpy(this->page_buf, buf, len);
295626777d37SBoris Brezillon memset(this->page_buf + len, 0xff, mtd->writesize - len);
295726777d37SBoris Brezillon pbuf = this->page_buf;
295826777d37SBoris Brezillon len = mtd->writesize;
295926777d37SBoris Brezillon }
296026777d37SBoris Brezillon
296126777d37SBoris Brezillon /* Enter OTP access mode */
296226777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
296326777d37SBoris Brezillon this->wait(mtd, FL_OTPING);
296426777d37SBoris Brezillon
296526777d37SBoris Brezillon ops.len = len;
296626777d37SBoris Brezillon ops.ooblen = 0;
296726777d37SBoris Brezillon ops.datbuf = pbuf;
296826777d37SBoris Brezillon ops.oobbuf = NULL;
296926777d37SBoris Brezillon ret = onenand_write_ops_nolock(mtd, to, &ops);
297026777d37SBoris Brezillon *retlen = ops.retlen;
297126777d37SBoris Brezillon
297226777d37SBoris Brezillon /* Exit OTP access mode */
297326777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_RESET, 0, 0);
297444f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
297526777d37SBoris Brezillon
297626777d37SBoris Brezillon return ret;
297726777d37SBoris Brezillon }
297826777d37SBoris Brezillon
297926777d37SBoris Brezillon /**
298026777d37SBoris Brezillon * do_otp_lock - [DEFAULT] Lock OTP block area
2981720ae74dSLee Jones * @mtd: MTD device structure
2982720ae74dSLee Jones * @from: The offset to lock
2983720ae74dSLee Jones * @len: number of bytes to lock
2984720ae74dSLee Jones * @retlen: pointer to variable to store the number of lock bytes
2985720ae74dSLee Jones * @buf: the databuffer to put/get data
298626777d37SBoris Brezillon *
298726777d37SBoris Brezillon * Lock OTP block area.
298826777d37SBoris Brezillon */
do_otp_lock(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)298926777d37SBoris Brezillon static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
299026777d37SBoris Brezillon size_t *retlen, u_char *buf)
299126777d37SBoris Brezillon {
299226777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
2993745df179SMichał Kępień struct mtd_oob_ops ops = { };
299426777d37SBoris Brezillon int ret;
299526777d37SBoris Brezillon
299626777d37SBoris Brezillon if (FLEXONENAND(this)) {
299726777d37SBoris Brezillon
299826777d37SBoris Brezillon /* Enter OTP access mode */
299926777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
300026777d37SBoris Brezillon this->wait(mtd, FL_OTPING);
300126777d37SBoris Brezillon /*
300226777d37SBoris Brezillon * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
300326777d37SBoris Brezillon * main area of page 49.
300426777d37SBoris Brezillon */
300526777d37SBoris Brezillon ops.len = mtd->writesize;
300626777d37SBoris Brezillon ops.ooblen = 0;
300726777d37SBoris Brezillon ops.datbuf = buf;
300826777d37SBoris Brezillon ops.oobbuf = NULL;
300926777d37SBoris Brezillon ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
301026777d37SBoris Brezillon *retlen = ops.retlen;
301126777d37SBoris Brezillon
301226777d37SBoris Brezillon /* Exit OTP access mode */
301326777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_RESET, 0, 0);
301444f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
301526777d37SBoris Brezillon } else {
301626777d37SBoris Brezillon ops.mode = MTD_OPS_PLACE_OOB;
301726777d37SBoris Brezillon ops.ooblen = len;
301826777d37SBoris Brezillon ops.oobbuf = buf;
301926777d37SBoris Brezillon ops.ooboffs = 0;
302026777d37SBoris Brezillon ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
302126777d37SBoris Brezillon *retlen = ops.oobretlen;
302226777d37SBoris Brezillon }
302326777d37SBoris Brezillon
302426777d37SBoris Brezillon return ret;
302526777d37SBoris Brezillon }
302626777d37SBoris Brezillon
302726777d37SBoris Brezillon /**
302826777d37SBoris Brezillon * onenand_otp_walk - [DEFAULT] Handle OTP operation
3029720ae74dSLee Jones * @mtd: MTD device structure
3030720ae74dSLee Jones * @from: The offset to read/write
3031720ae74dSLee Jones * @len: number of bytes to read/write
3032720ae74dSLee Jones * @retlen: pointer to variable to store the number of read bytes
3033720ae74dSLee Jones * @buf: the databuffer to put/get data
3034720ae74dSLee Jones * @action: do given action
3035720ae74dSLee Jones * @mode: specify user and factory
303626777d37SBoris Brezillon *
303726777d37SBoris Brezillon * Handle OTP operation.
303826777d37SBoris Brezillon */
onenand_otp_walk(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf,otp_op_t action,int mode)303926777d37SBoris Brezillon static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
304026777d37SBoris Brezillon size_t *retlen, u_char *buf,
304126777d37SBoris Brezillon otp_op_t action, int mode)
304226777d37SBoris Brezillon {
304326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
304426777d37SBoris Brezillon int otp_pages;
304526777d37SBoris Brezillon int density;
304626777d37SBoris Brezillon int ret = 0;
304726777d37SBoris Brezillon
304826777d37SBoris Brezillon *retlen = 0;
304926777d37SBoris Brezillon
305026777d37SBoris Brezillon density = onenand_get_density(this->device_id);
305126777d37SBoris Brezillon if (density < ONENAND_DEVICE_DENSITY_512Mb)
305226777d37SBoris Brezillon otp_pages = 20;
305326777d37SBoris Brezillon else
305426777d37SBoris Brezillon otp_pages = 50;
305526777d37SBoris Brezillon
305626777d37SBoris Brezillon if (mode == MTD_OTP_FACTORY) {
305726777d37SBoris Brezillon from += mtd->writesize * otp_pages;
305826777d37SBoris Brezillon otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
305926777d37SBoris Brezillon }
306026777d37SBoris Brezillon
306126777d37SBoris Brezillon /* Check User/Factory boundary */
306226777d37SBoris Brezillon if (mode == MTD_OTP_USER) {
306326777d37SBoris Brezillon if (mtd->writesize * otp_pages < from + len)
306426777d37SBoris Brezillon return 0;
306526777d37SBoris Brezillon } else {
306626777d37SBoris Brezillon if (mtd->writesize * otp_pages < len)
306726777d37SBoris Brezillon return 0;
306826777d37SBoris Brezillon }
306926777d37SBoris Brezillon
307026777d37SBoris Brezillon onenand_get_device(mtd, FL_OTPING);
307126777d37SBoris Brezillon while (len > 0 && otp_pages > 0) {
307226777d37SBoris Brezillon if (!action) { /* OTP Info functions */
307326777d37SBoris Brezillon struct otp_info *otpinfo;
307426777d37SBoris Brezillon
307526777d37SBoris Brezillon len -= sizeof(struct otp_info);
307626777d37SBoris Brezillon if (len <= 0) {
307726777d37SBoris Brezillon ret = -ENOSPC;
307826777d37SBoris Brezillon break;
307926777d37SBoris Brezillon }
308026777d37SBoris Brezillon
308126777d37SBoris Brezillon otpinfo = (struct otp_info *) buf;
308226777d37SBoris Brezillon otpinfo->start = from;
308326777d37SBoris Brezillon otpinfo->length = mtd->writesize;
308426777d37SBoris Brezillon otpinfo->locked = 0;
308526777d37SBoris Brezillon
308626777d37SBoris Brezillon from += mtd->writesize;
308726777d37SBoris Brezillon buf += sizeof(struct otp_info);
308826777d37SBoris Brezillon *retlen += sizeof(struct otp_info);
308926777d37SBoris Brezillon } else {
309026777d37SBoris Brezillon size_t tmp_retlen;
309126777d37SBoris Brezillon
309226777d37SBoris Brezillon ret = action(mtd, from, len, &tmp_retlen, buf);
309326777d37SBoris Brezillon if (ret)
309426777d37SBoris Brezillon break;
309526777d37SBoris Brezillon
309626777d37SBoris Brezillon buf += tmp_retlen;
309726777d37SBoris Brezillon len -= tmp_retlen;
309826777d37SBoris Brezillon *retlen += tmp_retlen;
309926777d37SBoris Brezillon
310026777d37SBoris Brezillon }
310126777d37SBoris Brezillon otp_pages--;
310226777d37SBoris Brezillon }
310326777d37SBoris Brezillon onenand_release_device(mtd);
310426777d37SBoris Brezillon
310526777d37SBoris Brezillon return ret;
310626777d37SBoris Brezillon }
310726777d37SBoris Brezillon
310826777d37SBoris Brezillon /**
310926777d37SBoris Brezillon * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3110720ae74dSLee Jones * @mtd: MTD device structure
3111720ae74dSLee Jones * @len: number of bytes to read
3112720ae74dSLee Jones * @retlen: pointer to variable to store the number of read bytes
3113720ae74dSLee Jones * @buf: the databuffer to put/get data
311426777d37SBoris Brezillon *
311526777d37SBoris Brezillon * Read factory OTP info.
311626777d37SBoris Brezillon */
onenand_get_fact_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)311726777d37SBoris Brezillon static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
311826777d37SBoris Brezillon size_t *retlen, struct otp_info *buf)
311926777d37SBoris Brezillon {
312026777d37SBoris Brezillon return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
312126777d37SBoris Brezillon MTD_OTP_FACTORY);
312226777d37SBoris Brezillon }
312326777d37SBoris Brezillon
312426777d37SBoris Brezillon /**
312526777d37SBoris Brezillon * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3126720ae74dSLee Jones * @mtd: MTD device structure
3127720ae74dSLee Jones * @from: The offset to read
3128720ae74dSLee Jones * @len: number of bytes to read
3129720ae74dSLee Jones * @retlen: pointer to variable to store the number of read bytes
3130720ae74dSLee Jones * @buf: the databuffer to put/get data
313126777d37SBoris Brezillon *
313226777d37SBoris Brezillon * Read factory OTP area.
313326777d37SBoris Brezillon */
onenand_read_fact_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)313426777d37SBoris Brezillon static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
313526777d37SBoris Brezillon size_t len, size_t *retlen, u_char *buf)
313626777d37SBoris Brezillon {
313726777d37SBoris Brezillon return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
313826777d37SBoris Brezillon }
313926777d37SBoris Brezillon
314026777d37SBoris Brezillon /**
314126777d37SBoris Brezillon * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3142720ae74dSLee Jones * @mtd: MTD device structure
3143720ae74dSLee Jones * @retlen: pointer to variable to store the number of read bytes
3144720ae74dSLee Jones * @len: number of bytes to read
3145720ae74dSLee Jones * @buf: the databuffer to put/get data
314626777d37SBoris Brezillon *
314726777d37SBoris Brezillon * Read user OTP info.
314826777d37SBoris Brezillon */
onenand_get_user_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)314926777d37SBoris Brezillon static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len,
315026777d37SBoris Brezillon size_t *retlen, struct otp_info *buf)
315126777d37SBoris Brezillon {
315226777d37SBoris Brezillon return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
315326777d37SBoris Brezillon MTD_OTP_USER);
315426777d37SBoris Brezillon }
315526777d37SBoris Brezillon
315626777d37SBoris Brezillon /**
315726777d37SBoris Brezillon * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3158720ae74dSLee Jones * @mtd: MTD device structure
3159720ae74dSLee Jones * @from: The offset to read
3160720ae74dSLee Jones * @len: number of bytes to read
3161720ae74dSLee Jones * @retlen: pointer to variable to store the number of read bytes
3162720ae74dSLee Jones * @buf: the databuffer to put/get data
316326777d37SBoris Brezillon *
316426777d37SBoris Brezillon * Read user OTP area.
316526777d37SBoris Brezillon */
onenand_read_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)316626777d37SBoris Brezillon static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
316726777d37SBoris Brezillon size_t len, size_t *retlen, u_char *buf)
316826777d37SBoris Brezillon {
316926777d37SBoris Brezillon return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
317026777d37SBoris Brezillon }
317126777d37SBoris Brezillon
317226777d37SBoris Brezillon /**
317326777d37SBoris Brezillon * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3174720ae74dSLee Jones * @mtd: MTD device structure
3175720ae74dSLee Jones * @from: The offset to write
3176720ae74dSLee Jones * @len: number of bytes to write
3177720ae74dSLee Jones * @retlen: pointer to variable to store the number of write bytes
3178720ae74dSLee Jones * @buf: the databuffer to put/get data
317926777d37SBoris Brezillon *
318026777d37SBoris Brezillon * Write user OTP area.
318126777d37SBoris Brezillon */
onenand_write_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,const u_char * buf)318226777d37SBoris Brezillon static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
31831df1fc8cSTudor Ambarus size_t len, size_t *retlen, const u_char *buf)
318426777d37SBoris Brezillon {
31851df1fc8cSTudor Ambarus return onenand_otp_walk(mtd, from, len, retlen, (u_char *)buf,
31861df1fc8cSTudor Ambarus do_otp_write, MTD_OTP_USER);
318726777d37SBoris Brezillon }
318826777d37SBoris Brezillon
318926777d37SBoris Brezillon /**
319026777d37SBoris Brezillon * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3191720ae74dSLee Jones * @mtd: MTD device structure
3192720ae74dSLee Jones * @from: The offset to lock
3193720ae74dSLee Jones * @len: number of bytes to unlock
319426777d37SBoris Brezillon *
319526777d37SBoris Brezillon * Write lock mark on spare area in page 0 in OTP block
319626777d37SBoris Brezillon */
onenand_lock_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len)319726777d37SBoris Brezillon static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
319826777d37SBoris Brezillon size_t len)
319926777d37SBoris Brezillon {
320026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
320126777d37SBoris Brezillon u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
320226777d37SBoris Brezillon size_t retlen;
320326777d37SBoris Brezillon int ret;
320426777d37SBoris Brezillon unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
320526777d37SBoris Brezillon
320626777d37SBoris Brezillon memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
320726777d37SBoris Brezillon : mtd->oobsize);
320826777d37SBoris Brezillon /*
320926777d37SBoris Brezillon * Write lock mark to 8th word of sector0 of page0 of the spare0.
321026777d37SBoris Brezillon * We write 16 bytes spare area instead of 2 bytes.
321126777d37SBoris Brezillon * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
321226777d37SBoris Brezillon * main area of page 49.
321326777d37SBoris Brezillon */
321426777d37SBoris Brezillon
321526777d37SBoris Brezillon from = 0;
321626777d37SBoris Brezillon len = FLEXONENAND(this) ? mtd->writesize : 16;
321726777d37SBoris Brezillon
321826777d37SBoris Brezillon /*
321926777d37SBoris Brezillon * Note: OTP lock operation
322026777d37SBoris Brezillon * OTP block : 0xXXFC XX 1111 1100
322126777d37SBoris Brezillon * 1st block : 0xXXF3 (If chip support) XX 1111 0011
322226777d37SBoris Brezillon * Both : 0xXXF0 (If chip support) XX 1111 0000
322326777d37SBoris Brezillon */
322426777d37SBoris Brezillon if (FLEXONENAND(this))
322526777d37SBoris Brezillon otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
322626777d37SBoris Brezillon
322726777d37SBoris Brezillon /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
322826777d37SBoris Brezillon if (otp == 1)
322926777d37SBoris Brezillon buf[otp_lock_offset] = 0xFC;
323026777d37SBoris Brezillon else if (otp == 2)
323126777d37SBoris Brezillon buf[otp_lock_offset] = 0xF3;
323226777d37SBoris Brezillon else if (otp == 3)
323326777d37SBoris Brezillon buf[otp_lock_offset] = 0xF0;
323426777d37SBoris Brezillon else if (otp != 0)
323526777d37SBoris Brezillon printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
323626777d37SBoris Brezillon
323726777d37SBoris Brezillon ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
323826777d37SBoris Brezillon
323926777d37SBoris Brezillon return ret ? : retlen;
324026777d37SBoris Brezillon }
324126777d37SBoris Brezillon
324226777d37SBoris Brezillon #endif /* CONFIG_MTD_ONENAND_OTP */
324326777d37SBoris Brezillon
324426777d37SBoris Brezillon /**
324526777d37SBoris Brezillon * onenand_check_features - Check and set OneNAND features
3246720ae74dSLee Jones * @mtd: MTD data structure
324726777d37SBoris Brezillon *
324826777d37SBoris Brezillon * Check and set OneNAND features
324926777d37SBoris Brezillon * - lock scheme
325026777d37SBoris Brezillon * - two plane
325126777d37SBoris Brezillon */
onenand_check_features(struct mtd_info * mtd)325226777d37SBoris Brezillon static void onenand_check_features(struct mtd_info *mtd)
325326777d37SBoris Brezillon {
325426777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
325526777d37SBoris Brezillon unsigned int density, process, numbufs;
325626777d37SBoris Brezillon
325726777d37SBoris Brezillon /* Lock scheme depends on density and process */
325826777d37SBoris Brezillon density = onenand_get_density(this->device_id);
325926777d37SBoris Brezillon process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
326026777d37SBoris Brezillon numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
326126777d37SBoris Brezillon
326226777d37SBoris Brezillon /* Lock scheme */
326326777d37SBoris Brezillon switch (density) {
3264855eff21SJonathan Bakker case ONENAND_DEVICE_DENSITY_8Gb:
3265855eff21SJonathan Bakker this->options |= ONENAND_HAS_NOP_1;
3266025a06c1SMiquel Raynal fallthrough;
326726777d37SBoris Brezillon case ONENAND_DEVICE_DENSITY_4Gb:
326826777d37SBoris Brezillon if (ONENAND_IS_DDP(this))
326926777d37SBoris Brezillon this->options |= ONENAND_HAS_2PLANE;
327026777d37SBoris Brezillon else if (numbufs == 1) {
327126777d37SBoris Brezillon this->options |= ONENAND_HAS_4KB_PAGE;
327226777d37SBoris Brezillon this->options |= ONENAND_HAS_CACHE_PROGRAM;
327326777d37SBoris Brezillon /*
327426777d37SBoris Brezillon * There are two different 4KiB pagesize chips
327526777d37SBoris Brezillon * and no way to detect it by H/W config values.
327626777d37SBoris Brezillon *
327726777d37SBoris Brezillon * To detect the correct NOP for each chips,
327826777d37SBoris Brezillon * It should check the version ID as workaround.
327926777d37SBoris Brezillon *
328026777d37SBoris Brezillon * Now it has as following
328126777d37SBoris Brezillon * KFM4G16Q4M has NOP 4 with version ID 0x0131
328226777d37SBoris Brezillon * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
328326777d37SBoris Brezillon */
328426777d37SBoris Brezillon if ((this->version_id & 0xf) == 0xe)
328526777d37SBoris Brezillon this->options |= ONENAND_HAS_NOP_1;
328626777d37SBoris Brezillon }
3287db01077cSGustavo A. R. Silva this->options |= ONENAND_HAS_UNLOCK_ALL;
3288db01077cSGustavo A. R. Silva break;
328926777d37SBoris Brezillon
329026777d37SBoris Brezillon case ONENAND_DEVICE_DENSITY_2Gb:
329126777d37SBoris Brezillon /* 2Gb DDP does not have 2 plane */
329226777d37SBoris Brezillon if (!ONENAND_IS_DDP(this))
329326777d37SBoris Brezillon this->options |= ONENAND_HAS_2PLANE;
329426777d37SBoris Brezillon this->options |= ONENAND_HAS_UNLOCK_ALL;
3295db01077cSGustavo A. R. Silva break;
329626777d37SBoris Brezillon
329726777d37SBoris Brezillon case ONENAND_DEVICE_DENSITY_1Gb:
329826777d37SBoris Brezillon /* A-Die has all block unlock */
329926777d37SBoris Brezillon if (process)
330026777d37SBoris Brezillon this->options |= ONENAND_HAS_UNLOCK_ALL;
330126777d37SBoris Brezillon break;
330226777d37SBoris Brezillon
330326777d37SBoris Brezillon default:
330426777d37SBoris Brezillon /* Some OneNAND has continuous lock scheme */
330526777d37SBoris Brezillon if (!process)
330626777d37SBoris Brezillon this->options |= ONENAND_HAS_CONT_LOCK;
330726777d37SBoris Brezillon break;
330826777d37SBoris Brezillon }
330926777d37SBoris Brezillon
331026777d37SBoris Brezillon /* The MLC has 4KiB pagesize. */
331126777d37SBoris Brezillon if (ONENAND_IS_MLC(this))
331226777d37SBoris Brezillon this->options |= ONENAND_HAS_4KB_PAGE;
331326777d37SBoris Brezillon
331426777d37SBoris Brezillon if (ONENAND_IS_4KB_PAGE(this))
331526777d37SBoris Brezillon this->options &= ~ONENAND_HAS_2PLANE;
331626777d37SBoris Brezillon
331726777d37SBoris Brezillon if (FLEXONENAND(this)) {
331826777d37SBoris Brezillon this->options &= ~ONENAND_HAS_CONT_LOCK;
331926777d37SBoris Brezillon this->options |= ONENAND_HAS_UNLOCK_ALL;
332026777d37SBoris Brezillon }
332126777d37SBoris Brezillon
332226777d37SBoris Brezillon if (this->options & ONENAND_HAS_CONT_LOCK)
332326777d37SBoris Brezillon printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
332426777d37SBoris Brezillon if (this->options & ONENAND_HAS_UNLOCK_ALL)
332526777d37SBoris Brezillon printk(KERN_DEBUG "Chip support all block unlock\n");
332626777d37SBoris Brezillon if (this->options & ONENAND_HAS_2PLANE)
332726777d37SBoris Brezillon printk(KERN_DEBUG "Chip has 2 plane\n");
332826777d37SBoris Brezillon if (this->options & ONENAND_HAS_4KB_PAGE)
332926777d37SBoris Brezillon printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
333026777d37SBoris Brezillon if (this->options & ONENAND_HAS_CACHE_PROGRAM)
333126777d37SBoris Brezillon printk(KERN_DEBUG "Chip has cache program feature\n");
333226777d37SBoris Brezillon }
333326777d37SBoris Brezillon
333426777d37SBoris Brezillon /**
333526777d37SBoris Brezillon * onenand_print_device_info - Print device & version ID
3336720ae74dSLee Jones * @device: device ID
3337720ae74dSLee Jones * @version: version ID
333826777d37SBoris Brezillon *
333926777d37SBoris Brezillon * Print device & version ID
334026777d37SBoris Brezillon */
onenand_print_device_info(int device,int version)334126777d37SBoris Brezillon static void onenand_print_device_info(int device, int version)
334226777d37SBoris Brezillon {
334326777d37SBoris Brezillon int vcc, demuxed, ddp, density, flexonenand;
334426777d37SBoris Brezillon
334526777d37SBoris Brezillon vcc = device & ONENAND_DEVICE_VCC_MASK;
334626777d37SBoris Brezillon demuxed = device & ONENAND_DEVICE_IS_DEMUX;
334726777d37SBoris Brezillon ddp = device & ONENAND_DEVICE_IS_DDP;
334826777d37SBoris Brezillon density = onenand_get_density(device);
334926777d37SBoris Brezillon flexonenand = device & DEVICE_IS_FLEXONENAND;
335026777d37SBoris Brezillon printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
335126777d37SBoris Brezillon demuxed ? "" : "Muxed ",
335226777d37SBoris Brezillon flexonenand ? "Flex-" : "",
335326777d37SBoris Brezillon ddp ? "(DDP)" : "",
335426777d37SBoris Brezillon (16 << density),
335526777d37SBoris Brezillon vcc ? "2.65/3.3" : "1.8",
335626777d37SBoris Brezillon device);
335726777d37SBoris Brezillon printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
335826777d37SBoris Brezillon }
335926777d37SBoris Brezillon
336026777d37SBoris Brezillon static const struct onenand_manufacturers onenand_manuf_ids[] = {
336126777d37SBoris Brezillon {ONENAND_MFR_SAMSUNG, "Samsung"},
336226777d37SBoris Brezillon {ONENAND_MFR_NUMONYX, "Numonyx"},
336326777d37SBoris Brezillon };
336426777d37SBoris Brezillon
336526777d37SBoris Brezillon /**
336626777d37SBoris Brezillon * onenand_check_maf - Check manufacturer ID
3367720ae74dSLee Jones * @manuf: manufacturer ID
336826777d37SBoris Brezillon *
336926777d37SBoris Brezillon * Check manufacturer ID
337026777d37SBoris Brezillon */
onenand_check_maf(int manuf)337126777d37SBoris Brezillon static int onenand_check_maf(int manuf)
337226777d37SBoris Brezillon {
337326777d37SBoris Brezillon int size = ARRAY_SIZE(onenand_manuf_ids);
337426777d37SBoris Brezillon char *name;
337526777d37SBoris Brezillon int i;
337626777d37SBoris Brezillon
337726777d37SBoris Brezillon for (i = 0; i < size; i++)
337826777d37SBoris Brezillon if (manuf == onenand_manuf_ids[i].id)
337926777d37SBoris Brezillon break;
338026777d37SBoris Brezillon
338126777d37SBoris Brezillon if (i < size)
338226777d37SBoris Brezillon name = onenand_manuf_ids[i].name;
338326777d37SBoris Brezillon else
338426777d37SBoris Brezillon name = "Unknown";
338526777d37SBoris Brezillon
338626777d37SBoris Brezillon printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
338726777d37SBoris Brezillon
338826777d37SBoris Brezillon return (i == size);
338926777d37SBoris Brezillon }
339026777d37SBoris Brezillon
339126777d37SBoris Brezillon /**
339226777d37SBoris Brezillon * flexonenand_get_boundary - Reads the SLC boundary
33932dc3d1e0SLee Jones * @mtd: MTD data structure
33942dc3d1e0SLee Jones */
flexonenand_get_boundary(struct mtd_info * mtd)339526777d37SBoris Brezillon static int flexonenand_get_boundary(struct mtd_info *mtd)
339626777d37SBoris Brezillon {
339726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
339826777d37SBoris Brezillon unsigned die, bdry;
339926777d37SBoris Brezillon int syscfg, locked;
340026777d37SBoris Brezillon
340126777d37SBoris Brezillon /* Disable ECC */
340226777d37SBoris Brezillon syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
340326777d37SBoris Brezillon this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
340426777d37SBoris Brezillon
340526777d37SBoris Brezillon for (die = 0; die < this->dies; die++) {
340626777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
340726777d37SBoris Brezillon this->wait(mtd, FL_SYNCING);
340826777d37SBoris Brezillon
340926777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
341026777d37SBoris Brezillon this->wait(mtd, FL_READING);
341126777d37SBoris Brezillon
341226777d37SBoris Brezillon bdry = this->read_word(this->base + ONENAND_DATARAM);
341326777d37SBoris Brezillon if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
341426777d37SBoris Brezillon locked = 0;
341526777d37SBoris Brezillon else
341626777d37SBoris Brezillon locked = 1;
341726777d37SBoris Brezillon this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
341826777d37SBoris Brezillon
341926777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_RESET, 0, 0);
342044f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
342126777d37SBoris Brezillon
342226777d37SBoris Brezillon printk(KERN_INFO "Die %d boundary: %d%s\n", die,
342326777d37SBoris Brezillon this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
342426777d37SBoris Brezillon }
342526777d37SBoris Brezillon
342626777d37SBoris Brezillon /* Enable ECC */
342726777d37SBoris Brezillon this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
342826777d37SBoris Brezillon return 0;
342926777d37SBoris Brezillon }
343026777d37SBoris Brezillon
343126777d37SBoris Brezillon /**
343226777d37SBoris Brezillon * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
343326777d37SBoris Brezillon * boundary[], diesize[], mtd->size, mtd->erasesize
3434720ae74dSLee Jones * @mtd: - MTD device structure
343526777d37SBoris Brezillon */
flexonenand_get_size(struct mtd_info * mtd)343626777d37SBoris Brezillon static void flexonenand_get_size(struct mtd_info *mtd)
343726777d37SBoris Brezillon {
343826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
343926777d37SBoris Brezillon int die, i, eraseshift, density;
344026777d37SBoris Brezillon int blksperdie, maxbdry;
344126777d37SBoris Brezillon loff_t ofs;
344226777d37SBoris Brezillon
344326777d37SBoris Brezillon density = onenand_get_density(this->device_id);
344426777d37SBoris Brezillon blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
344526777d37SBoris Brezillon blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
344626777d37SBoris Brezillon maxbdry = blksperdie - 1;
344726777d37SBoris Brezillon eraseshift = this->erase_shift - 1;
344826777d37SBoris Brezillon
344926777d37SBoris Brezillon mtd->numeraseregions = this->dies << 1;
345026777d37SBoris Brezillon
345126777d37SBoris Brezillon /* This fills up the device boundary */
345226777d37SBoris Brezillon flexonenand_get_boundary(mtd);
345326777d37SBoris Brezillon die = ofs = 0;
345426777d37SBoris Brezillon i = -1;
345526777d37SBoris Brezillon for (; die < this->dies; die++) {
345626777d37SBoris Brezillon if (!die || this->boundary[die-1] != maxbdry) {
345726777d37SBoris Brezillon i++;
345826777d37SBoris Brezillon mtd->eraseregions[i].offset = ofs;
345926777d37SBoris Brezillon mtd->eraseregions[i].erasesize = 1 << eraseshift;
346026777d37SBoris Brezillon mtd->eraseregions[i].numblocks =
346126777d37SBoris Brezillon this->boundary[die] + 1;
346226777d37SBoris Brezillon ofs += mtd->eraseregions[i].numblocks << eraseshift;
346326777d37SBoris Brezillon eraseshift++;
346426777d37SBoris Brezillon } else {
346526777d37SBoris Brezillon mtd->numeraseregions -= 1;
346626777d37SBoris Brezillon mtd->eraseregions[i].numblocks +=
346726777d37SBoris Brezillon this->boundary[die] + 1;
346826777d37SBoris Brezillon ofs += (this->boundary[die] + 1) << (eraseshift - 1);
346926777d37SBoris Brezillon }
347026777d37SBoris Brezillon if (this->boundary[die] != maxbdry) {
347126777d37SBoris Brezillon i++;
347226777d37SBoris Brezillon mtd->eraseregions[i].offset = ofs;
347326777d37SBoris Brezillon mtd->eraseregions[i].erasesize = 1 << eraseshift;
347426777d37SBoris Brezillon mtd->eraseregions[i].numblocks = maxbdry ^
347526777d37SBoris Brezillon this->boundary[die];
347626777d37SBoris Brezillon ofs += mtd->eraseregions[i].numblocks << eraseshift;
347726777d37SBoris Brezillon eraseshift--;
347826777d37SBoris Brezillon } else
347926777d37SBoris Brezillon mtd->numeraseregions -= 1;
348026777d37SBoris Brezillon }
348126777d37SBoris Brezillon
348226777d37SBoris Brezillon /* Expose MLC erase size except when all blocks are SLC */
348326777d37SBoris Brezillon mtd->erasesize = 1 << this->erase_shift;
348426777d37SBoris Brezillon if (mtd->numeraseregions == 1)
348526777d37SBoris Brezillon mtd->erasesize >>= 1;
348626777d37SBoris Brezillon
348726777d37SBoris Brezillon printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
348826777d37SBoris Brezillon for (i = 0; i < mtd->numeraseregions; i++)
348926777d37SBoris Brezillon printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
349026777d37SBoris Brezillon " numblocks: %04u]\n",
349126777d37SBoris Brezillon (unsigned int) mtd->eraseregions[i].offset,
349226777d37SBoris Brezillon mtd->eraseregions[i].erasesize,
349326777d37SBoris Brezillon mtd->eraseregions[i].numblocks);
349426777d37SBoris Brezillon
349526777d37SBoris Brezillon for (die = 0, mtd->size = 0; die < this->dies; die++) {
349626777d37SBoris Brezillon this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
349726777d37SBoris Brezillon this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
349826777d37SBoris Brezillon << (this->erase_shift - 1);
349926777d37SBoris Brezillon mtd->size += this->diesize[die];
350026777d37SBoris Brezillon }
350126777d37SBoris Brezillon }
350226777d37SBoris Brezillon
350326777d37SBoris Brezillon /**
350426777d37SBoris Brezillon * flexonenand_check_blocks_erased - Check if blocks are erased
35052dc3d1e0SLee Jones * @mtd: mtd info structure
35062dc3d1e0SLee Jones * @start: first erase block to check
35072dc3d1e0SLee Jones * @end: last erase block to check
350826777d37SBoris Brezillon *
350926777d37SBoris Brezillon * Converting an unerased block from MLC to SLC
351026777d37SBoris Brezillon * causes byte values to change. Since both data and its ECC
351126777d37SBoris Brezillon * have changed, reads on the block give uncorrectable error.
351226777d37SBoris Brezillon * This might lead to the block being detected as bad.
351326777d37SBoris Brezillon *
351426777d37SBoris Brezillon * Avoid this by ensuring that the block to be converted is
351526777d37SBoris Brezillon * erased.
351626777d37SBoris Brezillon */
flexonenand_check_blocks_erased(struct mtd_info * mtd,int start,int end)351726777d37SBoris Brezillon static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
351826777d37SBoris Brezillon {
351926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
352026777d37SBoris Brezillon int i, ret;
352126777d37SBoris Brezillon int block;
352226777d37SBoris Brezillon struct mtd_oob_ops ops = {
352326777d37SBoris Brezillon .mode = MTD_OPS_PLACE_OOB,
352426777d37SBoris Brezillon .ooboffs = 0,
352526777d37SBoris Brezillon .ooblen = mtd->oobsize,
352626777d37SBoris Brezillon .datbuf = NULL,
352726777d37SBoris Brezillon .oobbuf = this->oob_buf,
352826777d37SBoris Brezillon };
352926777d37SBoris Brezillon loff_t addr;
353026777d37SBoris Brezillon
353126777d37SBoris Brezillon printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
353226777d37SBoris Brezillon
353326777d37SBoris Brezillon for (block = start; block <= end; block++) {
353426777d37SBoris Brezillon addr = flexonenand_addr(this, block);
353526777d37SBoris Brezillon if (onenand_block_isbad_nolock(mtd, addr, 0))
353626777d37SBoris Brezillon continue;
353726777d37SBoris Brezillon
353826777d37SBoris Brezillon /*
353926777d37SBoris Brezillon * Since main area write results in ECC write to spare,
354026777d37SBoris Brezillon * it is sufficient to check only ECC bytes for change.
354126777d37SBoris Brezillon */
354226777d37SBoris Brezillon ret = onenand_read_oob_nolock(mtd, addr, &ops);
354326777d37SBoris Brezillon if (ret)
354426777d37SBoris Brezillon return ret;
354526777d37SBoris Brezillon
354626777d37SBoris Brezillon for (i = 0; i < mtd->oobsize; i++)
354726777d37SBoris Brezillon if (this->oob_buf[i] != 0xff)
354826777d37SBoris Brezillon break;
354926777d37SBoris Brezillon
355026777d37SBoris Brezillon if (i != mtd->oobsize) {
355126777d37SBoris Brezillon printk(KERN_WARNING "%s: Block %d not erased.\n",
355226777d37SBoris Brezillon __func__, block);
355326777d37SBoris Brezillon return 1;
355426777d37SBoris Brezillon }
355526777d37SBoris Brezillon }
355626777d37SBoris Brezillon
355726777d37SBoris Brezillon return 0;
355826777d37SBoris Brezillon }
355926777d37SBoris Brezillon
35602dc3d1e0SLee Jones /*
356126777d37SBoris Brezillon * flexonenand_set_boundary - Writes the SLC boundary
356226777d37SBoris Brezillon */
flexonenand_set_boundary(struct mtd_info * mtd,int die,int boundary,int lock)356326777d37SBoris Brezillon static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
356426777d37SBoris Brezillon int boundary, int lock)
356526777d37SBoris Brezillon {
356626777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
356726777d37SBoris Brezillon int ret, density, blksperdie, old, new, thisboundary;
356826777d37SBoris Brezillon loff_t addr;
356926777d37SBoris Brezillon
357026777d37SBoris Brezillon /* Change only once for SDP Flex-OneNAND */
357126777d37SBoris Brezillon if (die && (!ONENAND_IS_DDP(this)))
357226777d37SBoris Brezillon return 0;
357326777d37SBoris Brezillon
357426777d37SBoris Brezillon /* boundary value of -1 indicates no required change */
357526777d37SBoris Brezillon if (boundary < 0 || boundary == this->boundary[die])
357626777d37SBoris Brezillon return 0;
357726777d37SBoris Brezillon
357826777d37SBoris Brezillon density = onenand_get_density(this->device_id);
357926777d37SBoris Brezillon blksperdie = ((16 << density) << 20) >> this->erase_shift;
358026777d37SBoris Brezillon blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
358126777d37SBoris Brezillon
358226777d37SBoris Brezillon if (boundary >= blksperdie) {
358326777d37SBoris Brezillon printk(KERN_ERR "%s: Invalid boundary value. "
358426777d37SBoris Brezillon "Boundary not changed.\n", __func__);
358526777d37SBoris Brezillon return -EINVAL;
358626777d37SBoris Brezillon }
358726777d37SBoris Brezillon
358826777d37SBoris Brezillon /* Check if converting blocks are erased */
358926777d37SBoris Brezillon old = this->boundary[die] + (die * this->density_mask);
359026777d37SBoris Brezillon new = boundary + (die * this->density_mask);
359126777d37SBoris Brezillon ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
359226777d37SBoris Brezillon if (ret) {
359326777d37SBoris Brezillon printk(KERN_ERR "%s: Please erase blocks "
359426777d37SBoris Brezillon "before boundary change\n", __func__);
359526777d37SBoris Brezillon return ret;
359626777d37SBoris Brezillon }
359726777d37SBoris Brezillon
359826777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
359926777d37SBoris Brezillon this->wait(mtd, FL_SYNCING);
360026777d37SBoris Brezillon
360126777d37SBoris Brezillon /* Check is boundary is locked */
360226777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
360326777d37SBoris Brezillon this->wait(mtd, FL_READING);
360426777d37SBoris Brezillon
360526777d37SBoris Brezillon thisboundary = this->read_word(this->base + ONENAND_DATARAM);
360626777d37SBoris Brezillon if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
360726777d37SBoris Brezillon printk(KERN_ERR "%s: boundary locked\n", __func__);
360826777d37SBoris Brezillon ret = 1;
360926777d37SBoris Brezillon goto out;
361026777d37SBoris Brezillon }
361126777d37SBoris Brezillon
361226777d37SBoris Brezillon printk(KERN_INFO "Changing die %d boundary: %d%s\n",
361326777d37SBoris Brezillon die, boundary, lock ? "(Locked)" : "(Unlocked)");
361426777d37SBoris Brezillon
361526777d37SBoris Brezillon addr = die ? this->diesize[0] : 0;
361626777d37SBoris Brezillon
361726777d37SBoris Brezillon boundary &= FLEXONENAND_PI_MASK;
361826777d37SBoris Brezillon boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
361926777d37SBoris Brezillon
362026777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
362126777d37SBoris Brezillon ret = this->wait(mtd, FL_ERASING);
362226777d37SBoris Brezillon if (ret) {
362326777d37SBoris Brezillon printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
362426777d37SBoris Brezillon __func__, die);
362526777d37SBoris Brezillon goto out;
362626777d37SBoris Brezillon }
362726777d37SBoris Brezillon
362826777d37SBoris Brezillon this->write_word(boundary, this->base + ONENAND_DATARAM);
362926777d37SBoris Brezillon this->command(mtd, ONENAND_CMD_PROG, addr, 0);
363026777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
363126777d37SBoris Brezillon if (ret) {
363226777d37SBoris Brezillon printk(KERN_ERR "%s: Failed PI write for Die %d\n",
363326777d37SBoris Brezillon __func__, die);
363426777d37SBoris Brezillon goto out;
363526777d37SBoris Brezillon }
363626777d37SBoris Brezillon
363726777d37SBoris Brezillon this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
363826777d37SBoris Brezillon ret = this->wait(mtd, FL_WRITING);
363926777d37SBoris Brezillon out:
364026777d37SBoris Brezillon this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
364144f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
364226777d37SBoris Brezillon if (!ret)
364326777d37SBoris Brezillon /* Recalculate device size on boundary change*/
364426777d37SBoris Brezillon flexonenand_get_size(mtd);
364526777d37SBoris Brezillon
364626777d37SBoris Brezillon return ret;
364726777d37SBoris Brezillon }
364826777d37SBoris Brezillon
364926777d37SBoris Brezillon /**
365026777d37SBoris Brezillon * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3651720ae74dSLee Jones * @mtd: MTD device structure
365226777d37SBoris Brezillon *
365326777d37SBoris Brezillon * OneNAND detection method:
365426777d37SBoris Brezillon * Compare the values from command with ones from register
365526777d37SBoris Brezillon */
onenand_chip_probe(struct mtd_info * mtd)365626777d37SBoris Brezillon static int onenand_chip_probe(struct mtd_info *mtd)
365726777d37SBoris Brezillon {
365826777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
365926777d37SBoris Brezillon int bram_maf_id, bram_dev_id, maf_id, dev_id;
366026777d37SBoris Brezillon int syscfg;
366126777d37SBoris Brezillon
366226777d37SBoris Brezillon /* Save system configuration 1 */
366326777d37SBoris Brezillon syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
366426777d37SBoris Brezillon /* Clear Sync. Burst Read mode to read BootRAM */
366526777d37SBoris Brezillon this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
366626777d37SBoris Brezillon
366726777d37SBoris Brezillon /* Send the command for reading device ID from BootRAM */
366826777d37SBoris Brezillon this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
366926777d37SBoris Brezillon
367026777d37SBoris Brezillon /* Read manufacturer and device IDs from BootRAM */
367126777d37SBoris Brezillon bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
367226777d37SBoris Brezillon bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
367326777d37SBoris Brezillon
367426777d37SBoris Brezillon /* Reset OneNAND to read default register values */
367526777d37SBoris Brezillon this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
367626777d37SBoris Brezillon /* Wait reset */
367744f45994SAmir Mahdi Ghorbanian this->wait(mtd, FL_RESETTING);
367826777d37SBoris Brezillon
367926777d37SBoris Brezillon /* Restore system configuration 1 */
368026777d37SBoris Brezillon this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
368126777d37SBoris Brezillon
368226777d37SBoris Brezillon /* Check manufacturer ID */
368326777d37SBoris Brezillon if (onenand_check_maf(bram_maf_id))
368426777d37SBoris Brezillon return -ENXIO;
368526777d37SBoris Brezillon
368626777d37SBoris Brezillon /* Read manufacturer and device IDs from Register */
368726777d37SBoris Brezillon maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
368826777d37SBoris Brezillon dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
368926777d37SBoris Brezillon
369026777d37SBoris Brezillon /* Check OneNAND device */
369126777d37SBoris Brezillon if (maf_id != bram_maf_id || dev_id != bram_dev_id)
369226777d37SBoris Brezillon return -ENXIO;
369326777d37SBoris Brezillon
369426777d37SBoris Brezillon return 0;
369526777d37SBoris Brezillon }
369626777d37SBoris Brezillon
369726777d37SBoris Brezillon /**
369826777d37SBoris Brezillon * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3699720ae74dSLee Jones * @mtd: MTD device structure
370026777d37SBoris Brezillon */
onenand_probe(struct mtd_info * mtd)370126777d37SBoris Brezillon static int onenand_probe(struct mtd_info *mtd)
370226777d37SBoris Brezillon {
370326777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
370426777d37SBoris Brezillon int dev_id, ver_id;
370526777d37SBoris Brezillon int density;
370626777d37SBoris Brezillon int ret;
370726777d37SBoris Brezillon
370826777d37SBoris Brezillon ret = this->chip_probe(mtd);
370926777d37SBoris Brezillon if (ret)
371026777d37SBoris Brezillon return ret;
371126777d37SBoris Brezillon
371226777d37SBoris Brezillon /* Device and version IDs from Register */
371326777d37SBoris Brezillon dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
371426777d37SBoris Brezillon ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
371526777d37SBoris Brezillon this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
371626777d37SBoris Brezillon
371726777d37SBoris Brezillon /* Flash device information */
371826777d37SBoris Brezillon onenand_print_device_info(dev_id, ver_id);
371926777d37SBoris Brezillon this->device_id = dev_id;
372026777d37SBoris Brezillon this->version_id = ver_id;
372126777d37SBoris Brezillon
372226777d37SBoris Brezillon /* Check OneNAND features */
372326777d37SBoris Brezillon onenand_check_features(mtd);
372426777d37SBoris Brezillon
372526777d37SBoris Brezillon density = onenand_get_density(dev_id);
372626777d37SBoris Brezillon if (FLEXONENAND(this)) {
372726777d37SBoris Brezillon this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
372826777d37SBoris Brezillon /* Maximum possible erase regions */
372926777d37SBoris Brezillon mtd->numeraseregions = this->dies << 1;
37306396bb22SKees Cook mtd->eraseregions =
37316396bb22SKees Cook kcalloc(this->dies << 1,
37326396bb22SKees Cook sizeof(struct mtd_erase_region_info),
37336396bb22SKees Cook GFP_KERNEL);
373426777d37SBoris Brezillon if (!mtd->eraseregions)
373526777d37SBoris Brezillon return -ENOMEM;
373626777d37SBoris Brezillon }
373726777d37SBoris Brezillon
373826777d37SBoris Brezillon /*
373926777d37SBoris Brezillon * For Flex-OneNAND, chipsize represents maximum possible device size.
374026777d37SBoris Brezillon * mtd->size represents the actual device size.
374126777d37SBoris Brezillon */
374226777d37SBoris Brezillon this->chipsize = (16 << density) << 20;
374326777d37SBoris Brezillon
374426777d37SBoris Brezillon /* OneNAND page size & block size */
374526777d37SBoris Brezillon /* The data buffer size is equal to page size */
374626777d37SBoris Brezillon mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
374726777d37SBoris Brezillon /* We use the full BufferRAM */
374826777d37SBoris Brezillon if (ONENAND_IS_4KB_PAGE(this))
374926777d37SBoris Brezillon mtd->writesize <<= 1;
375026777d37SBoris Brezillon
375126777d37SBoris Brezillon mtd->oobsize = mtd->writesize >> 5;
375226777d37SBoris Brezillon /* Pages per a block are always 64 in OneNAND */
375326777d37SBoris Brezillon mtd->erasesize = mtd->writesize << 6;
375426777d37SBoris Brezillon /*
375526777d37SBoris Brezillon * Flex-OneNAND SLC area has 64 pages per block.
375626777d37SBoris Brezillon * Flex-OneNAND MLC area has 128 pages per block.
375726777d37SBoris Brezillon * Expose MLC erase size to find erase_shift and page_mask.
375826777d37SBoris Brezillon */
375926777d37SBoris Brezillon if (FLEXONENAND(this))
376026777d37SBoris Brezillon mtd->erasesize <<= 1;
376126777d37SBoris Brezillon
376226777d37SBoris Brezillon this->erase_shift = ffs(mtd->erasesize) - 1;
376326777d37SBoris Brezillon this->page_shift = ffs(mtd->writesize) - 1;
376426777d37SBoris Brezillon this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
376526777d37SBoris Brezillon /* Set density mask. it is used for DDP */
376626777d37SBoris Brezillon if (ONENAND_IS_DDP(this))
376726777d37SBoris Brezillon this->density_mask = this->chipsize >> (this->erase_shift + 1);
376826777d37SBoris Brezillon /* It's real page size */
376926777d37SBoris Brezillon this->writesize = mtd->writesize;
377026777d37SBoris Brezillon
377126777d37SBoris Brezillon /* REVISIT: Multichip handling */
377226777d37SBoris Brezillon
377326777d37SBoris Brezillon if (FLEXONENAND(this))
377426777d37SBoris Brezillon flexonenand_get_size(mtd);
377526777d37SBoris Brezillon else
377626777d37SBoris Brezillon mtd->size = this->chipsize;
377726777d37SBoris Brezillon
377826777d37SBoris Brezillon /*
377926777d37SBoris Brezillon * We emulate the 4KiB page and 256KiB erase block size
378026777d37SBoris Brezillon * But oobsize is still 64 bytes.
378126777d37SBoris Brezillon * It is only valid if you turn on 2X program support,
378226777d37SBoris Brezillon * Otherwise it will be ignored by compiler.
378326777d37SBoris Brezillon */
378426777d37SBoris Brezillon if (ONENAND_IS_2PLANE(this)) {
378526777d37SBoris Brezillon mtd->writesize <<= 1;
378626777d37SBoris Brezillon mtd->erasesize <<= 1;
378726777d37SBoris Brezillon }
378826777d37SBoris Brezillon
378926777d37SBoris Brezillon return 0;
379026777d37SBoris Brezillon }
379126777d37SBoris Brezillon
379226777d37SBoris Brezillon /**
379326777d37SBoris Brezillon * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3794720ae74dSLee Jones * @mtd: MTD device structure
379526777d37SBoris Brezillon */
onenand_suspend(struct mtd_info * mtd)379626777d37SBoris Brezillon static int onenand_suspend(struct mtd_info *mtd)
379726777d37SBoris Brezillon {
379826777d37SBoris Brezillon return onenand_get_device(mtd, FL_PM_SUSPENDED);
379926777d37SBoris Brezillon }
380026777d37SBoris Brezillon
380126777d37SBoris Brezillon /**
380226777d37SBoris Brezillon * onenand_resume - [MTD Interface] Resume the OneNAND flash
3803720ae74dSLee Jones * @mtd: MTD device structure
380426777d37SBoris Brezillon */
onenand_resume(struct mtd_info * mtd)380526777d37SBoris Brezillon static void onenand_resume(struct mtd_info *mtd)
380626777d37SBoris Brezillon {
380726777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
380826777d37SBoris Brezillon
380926777d37SBoris Brezillon if (this->state == FL_PM_SUSPENDED)
381026777d37SBoris Brezillon onenand_release_device(mtd);
381126777d37SBoris Brezillon else
381226777d37SBoris Brezillon printk(KERN_ERR "%s: resume() called for the chip which is not "
381326777d37SBoris Brezillon "in suspended state\n", __func__);
381426777d37SBoris Brezillon }
381526777d37SBoris Brezillon
381626777d37SBoris Brezillon /**
381726777d37SBoris Brezillon * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3818720ae74dSLee Jones * @mtd: MTD device structure
3819720ae74dSLee Jones * @maxchips: Number of chips to scan for
382026777d37SBoris Brezillon *
382126777d37SBoris Brezillon * This fills out all the not initialized function pointers
382226777d37SBoris Brezillon * with the defaults.
382326777d37SBoris Brezillon * The flash ID is read and the mtd/chip structures are
382426777d37SBoris Brezillon * filled with the appropriate values.
382526777d37SBoris Brezillon */
onenand_scan(struct mtd_info * mtd,int maxchips)382626777d37SBoris Brezillon int onenand_scan(struct mtd_info *mtd, int maxchips)
382726777d37SBoris Brezillon {
382826777d37SBoris Brezillon int i, ret;
382926777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
383026777d37SBoris Brezillon
383126777d37SBoris Brezillon if (!this->read_word)
383226777d37SBoris Brezillon this->read_word = onenand_readw;
383326777d37SBoris Brezillon if (!this->write_word)
383426777d37SBoris Brezillon this->write_word = onenand_writew;
383526777d37SBoris Brezillon
383626777d37SBoris Brezillon if (!this->command)
383726777d37SBoris Brezillon this->command = onenand_command;
383826777d37SBoris Brezillon if (!this->wait)
383926777d37SBoris Brezillon onenand_setup_wait(mtd);
384026777d37SBoris Brezillon if (!this->bbt_wait)
384126777d37SBoris Brezillon this->bbt_wait = onenand_bbt_wait;
384226777d37SBoris Brezillon if (!this->unlock_all)
384326777d37SBoris Brezillon this->unlock_all = onenand_unlock_all;
384426777d37SBoris Brezillon
384526777d37SBoris Brezillon if (!this->chip_probe)
384626777d37SBoris Brezillon this->chip_probe = onenand_chip_probe;
384726777d37SBoris Brezillon
384826777d37SBoris Brezillon if (!this->read_bufferram)
384926777d37SBoris Brezillon this->read_bufferram = onenand_read_bufferram;
385026777d37SBoris Brezillon if (!this->write_bufferram)
385126777d37SBoris Brezillon this->write_bufferram = onenand_write_bufferram;
385226777d37SBoris Brezillon
385326777d37SBoris Brezillon if (!this->block_markbad)
385426777d37SBoris Brezillon this->block_markbad = onenand_default_block_markbad;
385526777d37SBoris Brezillon if (!this->scan_bbt)
385626777d37SBoris Brezillon this->scan_bbt = onenand_default_bbt;
385726777d37SBoris Brezillon
385826777d37SBoris Brezillon if (onenand_probe(mtd))
385926777d37SBoris Brezillon return -ENXIO;
386026777d37SBoris Brezillon
386126777d37SBoris Brezillon /* Set Sync. Burst Read after probing */
386226777d37SBoris Brezillon if (this->mmcontrol) {
386326777d37SBoris Brezillon printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
386426777d37SBoris Brezillon this->read_bufferram = onenand_sync_read_bufferram;
386526777d37SBoris Brezillon }
386626777d37SBoris Brezillon
386726777d37SBoris Brezillon /* Allocate buffers, if necessary */
386826777d37SBoris Brezillon if (!this->page_buf) {
386926777d37SBoris Brezillon this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
387026777d37SBoris Brezillon if (!this->page_buf)
387126777d37SBoris Brezillon return -ENOMEM;
387226777d37SBoris Brezillon #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
387326777d37SBoris Brezillon this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
387426777d37SBoris Brezillon if (!this->verify_buf) {
387526777d37SBoris Brezillon kfree(this->page_buf);
387626777d37SBoris Brezillon return -ENOMEM;
387726777d37SBoris Brezillon }
387826777d37SBoris Brezillon #endif
387926777d37SBoris Brezillon this->options |= ONENAND_PAGEBUF_ALLOC;
388026777d37SBoris Brezillon }
388126777d37SBoris Brezillon if (!this->oob_buf) {
388226777d37SBoris Brezillon this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
388326777d37SBoris Brezillon if (!this->oob_buf) {
388426777d37SBoris Brezillon if (this->options & ONENAND_PAGEBUF_ALLOC) {
388526777d37SBoris Brezillon this->options &= ~ONENAND_PAGEBUF_ALLOC;
3886d83aef09SWenwen Wang #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3887d83aef09SWenwen Wang kfree(this->verify_buf);
3888d83aef09SWenwen Wang #endif
388926777d37SBoris Brezillon kfree(this->page_buf);
389026777d37SBoris Brezillon }
389126777d37SBoris Brezillon return -ENOMEM;
389226777d37SBoris Brezillon }
389326777d37SBoris Brezillon this->options |= ONENAND_OOBBUF_ALLOC;
389426777d37SBoris Brezillon }
389526777d37SBoris Brezillon
389626777d37SBoris Brezillon this->state = FL_READY;
389726777d37SBoris Brezillon init_waitqueue_head(&this->wq);
389826777d37SBoris Brezillon spin_lock_init(&this->chip_lock);
389926777d37SBoris Brezillon
390026777d37SBoris Brezillon /*
390126777d37SBoris Brezillon * Allow subpage writes up to oobsize.
390226777d37SBoris Brezillon */
390326777d37SBoris Brezillon switch (mtd->oobsize) {
390426777d37SBoris Brezillon case 128:
390526777d37SBoris Brezillon if (FLEXONENAND(this)) {
390626777d37SBoris Brezillon mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops);
390726777d37SBoris Brezillon mtd->subpage_sft = 0;
390826777d37SBoris Brezillon } else {
390926777d37SBoris Brezillon mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops);
391026777d37SBoris Brezillon mtd->subpage_sft = 2;
391126777d37SBoris Brezillon }
391226777d37SBoris Brezillon if (ONENAND_IS_NOP_1(this))
391326777d37SBoris Brezillon mtd->subpage_sft = 0;
391426777d37SBoris Brezillon break;
391526777d37SBoris Brezillon case 64:
391626777d37SBoris Brezillon mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
391726777d37SBoris Brezillon mtd->subpage_sft = 2;
391826777d37SBoris Brezillon break;
391926777d37SBoris Brezillon
392026777d37SBoris Brezillon case 32:
392126777d37SBoris Brezillon mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
392226777d37SBoris Brezillon mtd->subpage_sft = 1;
392326777d37SBoris Brezillon break;
392426777d37SBoris Brezillon
392526777d37SBoris Brezillon default:
392626777d37SBoris Brezillon printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
392726777d37SBoris Brezillon __func__, mtd->oobsize);
392826777d37SBoris Brezillon mtd->subpage_sft = 0;
392926777d37SBoris Brezillon /* To prevent kernel oops */
393026777d37SBoris Brezillon mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
393126777d37SBoris Brezillon break;
393226777d37SBoris Brezillon }
393326777d37SBoris Brezillon
393426777d37SBoris Brezillon this->subpagesize = mtd->writesize >> mtd->subpage_sft;
393526777d37SBoris Brezillon
393626777d37SBoris Brezillon /*
393726777d37SBoris Brezillon * The number of bytes available for a client to place data into
393826777d37SBoris Brezillon * the out of band area
393926777d37SBoris Brezillon */
394026777d37SBoris Brezillon ret = mtd_ooblayout_count_freebytes(mtd);
394126777d37SBoris Brezillon if (ret < 0)
394226777d37SBoris Brezillon ret = 0;
394326777d37SBoris Brezillon
394426777d37SBoris Brezillon mtd->oobavail = ret;
394526777d37SBoris Brezillon
394626777d37SBoris Brezillon mtd->ecc_strength = 1;
394726777d37SBoris Brezillon
394826777d37SBoris Brezillon /* Fill in remaining MTD driver data */
394926777d37SBoris Brezillon mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
395026777d37SBoris Brezillon mtd->flags = MTD_CAP_NANDFLASH;
395126777d37SBoris Brezillon mtd->_erase = onenand_erase;
395226777d37SBoris Brezillon mtd->_point = NULL;
395326777d37SBoris Brezillon mtd->_unpoint = NULL;
395426777d37SBoris Brezillon mtd->_read_oob = onenand_read_oob;
395526777d37SBoris Brezillon mtd->_write_oob = onenand_write_oob;
395626777d37SBoris Brezillon mtd->_panic_write = onenand_panic_write;
395726777d37SBoris Brezillon #ifdef CONFIG_MTD_ONENAND_OTP
395826777d37SBoris Brezillon mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
395926777d37SBoris Brezillon mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
396026777d37SBoris Brezillon mtd->_get_user_prot_info = onenand_get_user_prot_info;
396126777d37SBoris Brezillon mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
396226777d37SBoris Brezillon mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
396326777d37SBoris Brezillon mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
396426777d37SBoris Brezillon #endif
396526777d37SBoris Brezillon mtd->_sync = onenand_sync;
396626777d37SBoris Brezillon mtd->_lock = onenand_lock;
396726777d37SBoris Brezillon mtd->_unlock = onenand_unlock;
396826777d37SBoris Brezillon mtd->_suspend = onenand_suspend;
396926777d37SBoris Brezillon mtd->_resume = onenand_resume;
397026777d37SBoris Brezillon mtd->_block_isbad = onenand_block_isbad;
397126777d37SBoris Brezillon mtd->_block_markbad = onenand_block_markbad;
397226777d37SBoris Brezillon mtd->owner = THIS_MODULE;
397326777d37SBoris Brezillon mtd->writebufsize = mtd->writesize;
397426777d37SBoris Brezillon
397526777d37SBoris Brezillon /* Unlock whole block */
397626777d37SBoris Brezillon if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
397726777d37SBoris Brezillon this->unlock_all(mtd);
397826777d37SBoris Brezillon
3979bfd15c90SFrieder Schrempf /* Set the bad block marker position */
3980bfd15c90SFrieder Schrempf this->badblockpos = ONENAND_BADBLOCK_POS;
3981bfd15c90SFrieder Schrempf
398226777d37SBoris Brezillon ret = this->scan_bbt(mtd);
398326777d37SBoris Brezillon if ((!FLEXONENAND(this)) || ret)
398426777d37SBoris Brezillon return ret;
398526777d37SBoris Brezillon
398626777d37SBoris Brezillon /* Change Flex-OneNAND boundaries if required */
398726777d37SBoris Brezillon for (i = 0; i < MAX_DIES; i++)
398826777d37SBoris Brezillon flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
398926777d37SBoris Brezillon flex_bdry[(2 * i) + 1]);
399026777d37SBoris Brezillon
399126777d37SBoris Brezillon return 0;
399226777d37SBoris Brezillon }
399326777d37SBoris Brezillon
399426777d37SBoris Brezillon /**
399526777d37SBoris Brezillon * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3996720ae74dSLee Jones * @mtd: MTD device structure
399726777d37SBoris Brezillon */
onenand_release(struct mtd_info * mtd)399826777d37SBoris Brezillon void onenand_release(struct mtd_info *mtd)
399926777d37SBoris Brezillon {
400026777d37SBoris Brezillon struct onenand_chip *this = mtd->priv;
400126777d37SBoris Brezillon
400226777d37SBoris Brezillon /* Deregister partitions */
400326777d37SBoris Brezillon mtd_device_unregister(mtd);
400426777d37SBoris Brezillon
400526777d37SBoris Brezillon /* Free bad block table memory, if allocated */
400626777d37SBoris Brezillon if (this->bbm) {
400726777d37SBoris Brezillon struct bbm_info *bbm = this->bbm;
400826777d37SBoris Brezillon kfree(bbm->bbt);
400926777d37SBoris Brezillon kfree(this->bbm);
401026777d37SBoris Brezillon }
401126777d37SBoris Brezillon /* Buffers allocated by onenand_scan */
401226777d37SBoris Brezillon if (this->options & ONENAND_PAGEBUF_ALLOC) {
401326777d37SBoris Brezillon kfree(this->page_buf);
401426777d37SBoris Brezillon #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
401526777d37SBoris Brezillon kfree(this->verify_buf);
401626777d37SBoris Brezillon #endif
401726777d37SBoris Brezillon }
401826777d37SBoris Brezillon if (this->options & ONENAND_OOBBUF_ALLOC)
401926777d37SBoris Brezillon kfree(this->oob_buf);
402026777d37SBoris Brezillon kfree(mtd->eraseregions);
402126777d37SBoris Brezillon }
402226777d37SBoris Brezillon
402326777d37SBoris Brezillon EXPORT_SYMBOL_GPL(onenand_scan);
402426777d37SBoris Brezillon EXPORT_SYMBOL_GPL(onenand_release);
402526777d37SBoris Brezillon
402626777d37SBoris Brezillon MODULE_LICENSE("GPL");
402726777d37SBoris Brezillon MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
402826777d37SBoris Brezillon MODULE_DESCRIPTION("Generic OneNAND flash driver code");
4029