Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mtd/onenand/onenand_base.c |
| 3 | * |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 4 | * Copyright © 2005-2009 Samsung Electronics |
| 5 | * Copyright © 2007 Nokia Corporation |
| 6 | * |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 7 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 8 | * |
Adrian Hunter | 81280d5 | 2007-02-15 09:47:29 +0900 | [diff] [blame] | 9 | * Credits: |
| 10 | * Adrian Hunter <ext-adrian.hunter@nokia.com>: |
| 11 | * auto-placement support, read-while load support, various fixes |
Adrian Hunter | 81280d5 | 2007-02-15 09:47:29 +0900 | [diff] [blame] | 12 | * |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 13 | * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com> |
| 14 | * Flex-OneNAND support |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 15 | * Amul Kumar Saha <amul.saha at samsung.com> |
| 16 | * OTP support |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 17 | * |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 18 | * This program is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License version 2 as |
| 20 | * published by the Free Software Foundation. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
Amul Saha | c90173f | 2009-06-16 11:24:01 +0530 | [diff] [blame] | 25 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Andrew Morton | 015953d | 2005-11-08 21:34:28 -0800 | [diff] [blame] | 27 | #include <linux/sched.h> |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 28 | #include <linux/delay.h> |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
Andrew Morton | 015953d | 2005-11-08 21:34:28 -0800 | [diff] [blame] | 30 | #include <linux/jiffies.h> |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 31 | #include <linux/mtd/mtd.h> |
| 32 | #include <linux/mtd/onenand.h> |
| 33 | #include <linux/mtd/partitions.h> |
| 34 | |
| 35 | #include <asm/io.h> |
| 36 | |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Multiblock erase if number of blocks to erase is 2 or more. |
| 39 | * Maximum number of blocks for simultaneous erase is 64. |
| 40 | */ |
| 41 | #define MB_ERASE_MIN_BLK_COUNT 2 |
| 42 | #define MB_ERASE_MAX_BLK_COUNT 64 |
| 43 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 44 | /* Default Flex-OneNAND boundary and lock respectively */ |
| 45 | static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 }; |
| 46 | |
Amul Saha | c90173f | 2009-06-16 11:24:01 +0530 | [diff] [blame] | 47 | module_param_array(flex_bdry, int, NULL, 0400); |
| 48 | MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND" |
| 49 | "Syntax:flex_bdry=DIE_BDRY,LOCK,..." |
| 50 | "DIE_BDRY: SLC boundary of the die" |
| 51 | "LOCK: Locking information for SLC boundary" |
| 52 | " : 0->Set boundary in unlocked status" |
| 53 | " : 1->Set boundary in locked status"); |
| 54 | |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 55 | /* Default OneNAND/Flex-OneNAND OTP options*/ |
| 56 | static int otp; |
| 57 | |
| 58 | module_param(otp, int, 0400); |
| 59 | MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP" |
| 60 | "Syntax : otp=LOCK_TYPE" |
| 61 | "LOCK_TYPE : Keys issued, for specific OTP Lock type" |
| 62 | " : 0 -> Default (No Blocks Locked)" |
| 63 | " : 1 -> OTP Block lock" |
| 64 | " : 2 -> 1st Block lock" |
| 65 | " : 3 -> BOTH OTP Block and 1st Block lock"); |
| 66 | |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 67 | /* |
| 68 | * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page |
| 69 | * For now, we expose only 64 out of 80 ecc bytes |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 70 | */ |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 71 | static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 72 | struct mtd_oob_region *oobregion) |
| 73 | { |
| 74 | if (section > 7) |
| 75 | return -ERANGE; |
| 76 | |
| 77 | oobregion->offset = (section * 16) + 6; |
| 78 | oobregion->length = 10; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section, |
| 84 | struct mtd_oob_region *oobregion) |
| 85 | { |
| 86 | if (section > 7) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | oobregion->offset = (section * 16) + 2; |
| 90 | oobregion->length = 4; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = { |
| 96 | .ecc = flexonenand_ooblayout_ecc, |
| 97 | .free = flexonenand_ooblayout_free, |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 100 | /* |
| 101 | * onenand_oob_128 - oob info for OneNAND with 4KB page |
| 102 | * |
| 103 | * Based on specification: |
| 104 | * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010 |
| 105 | * |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 106 | */ |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 107 | static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section, |
| 108 | struct mtd_oob_region *oobregion) |
| 109 | { |
| 110 | if (section > 7) |
| 111 | return -ERANGE; |
| 112 | |
| 113 | oobregion->offset = (section * 16) + 7; |
| 114 | oobregion->length = 9; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section, |
| 120 | struct mtd_oob_region *oobregion) |
| 121 | { |
| 122 | if (section >= 8) |
| 123 | return -ERANGE; |
| 124 | |
| 125 | /* |
| 126 | * free bytes are using the spare area fields marked as |
| 127 | * "Managed by internal ECC logic for Logical Sector Number area" |
| 128 | */ |
| 129 | oobregion->offset = (section * 16) + 2; |
| 130 | oobregion->length = 3; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = { |
| 136 | .ecc = onenand_ooblayout_128_ecc, |
| 137 | .free = onenand_ooblayout_128_free, |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 138 | }; |
| 139 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 140 | /** |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 141 | * onenand_oob_32_64 - oob info for large (2KB) page |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 142 | */ |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 143 | static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section, |
| 144 | struct mtd_oob_region *oobregion) |
| 145 | { |
| 146 | if (section > 3) |
| 147 | return -ERANGE; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 148 | |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 149 | oobregion->offset = (section * 16) + 8; |
| 150 | oobregion->length = 5; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section, |
| 156 | struct mtd_oob_region *oobregion) |
| 157 | { |
| 158 | int sections = (mtd->oobsize / 32) * 2; |
| 159 | |
| 160 | if (section >= sections) |
| 161 | return -ERANGE; |
| 162 | |
| 163 | if (section & 1) { |
| 164 | oobregion->offset = ((section - 1) * 16) + 14; |
| 165 | oobregion->length = 2; |
| 166 | } else { |
| 167 | oobregion->offset = (section * 16) + 2; |
| 168 | oobregion->length = 3; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = { |
| 175 | .ecc = onenand_ooblayout_32_64_ecc, |
| 176 | .free = onenand_ooblayout_32_64_free, |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static const unsigned char ffchars[] = { |
| 180 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 181 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */ |
| 182 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 183 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */ |
| 184 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 185 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */ |
| 186 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 187 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 188 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 189 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */ |
| 190 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 191 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */ |
| 192 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 193 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */ |
| 194 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 195 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /** |
| 199 | * onenand_readw - [OneNAND Interface] Read OneNAND register |
| 200 | * @param addr address to read |
| 201 | * |
| 202 | * Read OneNAND register |
| 203 | */ |
| 204 | static unsigned short onenand_readw(void __iomem *addr) |
| 205 | { |
| 206 | return readw(addr); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * onenand_writew - [OneNAND Interface] Write OneNAND register with value |
| 211 | * @param value value to write |
| 212 | * @param addr address to write |
| 213 | * |
| 214 | * Write OneNAND register with value |
| 215 | */ |
| 216 | static void onenand_writew(unsigned short value, void __iomem *addr) |
| 217 | { |
| 218 | writew(value, addr); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * onenand_block_address - [DEFAULT] Get block address |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 223 | * @param this onenand chip data structure |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 224 | * @param block the block |
| 225 | * @return translated block address if DDP, otherwise same |
| 226 | * |
| 227 | * Setup Start Address 1 Register (F100h) |
| 228 | */ |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 229 | static int onenand_block_address(struct onenand_chip *this, int block) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 230 | { |
Kyungmin Park | 738d61f | 2007-01-15 17:09:14 +0900 | [diff] [blame] | 231 | /* Device Flash Core select, NAND Flash Block Address */ |
| 232 | if (block & this->density_mask) |
| 233 | return ONENAND_DDP_CHIP1 | (block ^ this->density_mask); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 234 | |
| 235 | return block; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * onenand_bufferram_address - [DEFAULT] Get bufferram address |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 240 | * @param this onenand chip data structure |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 241 | * @param block the block |
| 242 | * @return set DBS value if DDP, otherwise 0 |
| 243 | * |
| 244 | * Setup Start Address 2 Register (F101h) for DDP |
| 245 | */ |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 246 | static int onenand_bufferram_address(struct onenand_chip *this, int block) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 247 | { |
Kyungmin Park | 738d61f | 2007-01-15 17:09:14 +0900 | [diff] [blame] | 248 | /* Device BufferRAM Select */ |
| 249 | if (block & this->density_mask) |
| 250 | return ONENAND_DDP_CHIP1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 251 | |
Kyungmin Park | 738d61f | 2007-01-15 17:09:14 +0900 | [diff] [blame] | 252 | return ONENAND_DDP_CHIP0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * onenand_page_address - [DEFAULT] Get page address |
| 257 | * @param page the page address |
| 258 | * @param sector the sector address |
| 259 | * @return combined page and sector address |
| 260 | * |
| 261 | * Setup Start Address 8 Register (F107h) |
| 262 | */ |
| 263 | static int onenand_page_address(int page, int sector) |
| 264 | { |
| 265 | /* Flash Page Address, Flash Sector Address */ |
| 266 | int fpa, fsa; |
| 267 | |
| 268 | fpa = page & ONENAND_FPA_MASK; |
| 269 | fsa = sector & ONENAND_FSA_MASK; |
| 270 | |
| 271 | return ((fpa << ONENAND_FPA_SHIFT) | fsa); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * onenand_buffer_address - [DEFAULT] Get buffer address |
| 276 | * @param dataram1 DataRAM index |
| 277 | * @param sectors the sector address |
| 278 | * @param count the number of sectors |
| 279 | * @return the start buffer value |
| 280 | * |
| 281 | * Setup Start Buffer Register (F200h) |
| 282 | */ |
| 283 | static int onenand_buffer_address(int dataram1, int sectors, int count) |
| 284 | { |
| 285 | int bsa, bsc; |
| 286 | |
| 287 | /* BufferRAM Sector Address */ |
| 288 | bsa = sectors & ONENAND_BSA_MASK; |
| 289 | |
| 290 | if (dataram1) |
| 291 | bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */ |
| 292 | else |
| 293 | bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */ |
| 294 | |
| 295 | /* BufferRAM Sector Count */ |
| 296 | bsc = count & ONENAND_BSC_MASK; |
| 297 | |
| 298 | return ((bsa << ONENAND_BSA_SHIFT) | bsc); |
| 299 | } |
| 300 | |
| 301 | /** |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 302 | * flexonenand_block- For given address return block number |
| 303 | * @param this - OneNAND device structure |
| 304 | * @param addr - Address for which block number is needed |
| 305 | */ |
| 306 | static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr) |
| 307 | { |
| 308 | unsigned boundary, blk, die = 0; |
| 309 | |
| 310 | if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) { |
| 311 | die = 1; |
| 312 | addr -= this->diesize[0]; |
| 313 | } |
| 314 | |
| 315 | boundary = this->boundary[die]; |
| 316 | |
| 317 | blk = addr >> (this->erase_shift - 1); |
| 318 | if (blk > boundary) |
| 319 | blk = (blk + boundary + 1) >> 1; |
| 320 | |
| 321 | blk += die ? this->density_mask : 0; |
| 322 | return blk; |
| 323 | } |
| 324 | |
| 325 | inline unsigned onenand_block(struct onenand_chip *this, loff_t addr) |
| 326 | { |
| 327 | if (!FLEXONENAND(this)) |
| 328 | return addr >> this->erase_shift; |
| 329 | return flexonenand_block(this, addr); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * flexonenand_addr - Return address of the block |
| 334 | * @this: OneNAND device structure |
| 335 | * @block: Block number on Flex-OneNAND |
| 336 | * |
| 337 | * Return address of the block |
| 338 | */ |
| 339 | static loff_t flexonenand_addr(struct onenand_chip *this, int block) |
| 340 | { |
| 341 | loff_t ofs = 0; |
| 342 | int die = 0, boundary; |
| 343 | |
| 344 | if (ONENAND_IS_DDP(this) && block >= this->density_mask) { |
| 345 | block -= this->density_mask; |
| 346 | die = 1; |
| 347 | ofs = this->diesize[0]; |
| 348 | } |
| 349 | |
| 350 | boundary = this->boundary[die]; |
| 351 | ofs += (loff_t)block << (this->erase_shift - 1); |
| 352 | if (block > (boundary + 1)) |
| 353 | ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1); |
| 354 | return ofs; |
| 355 | } |
| 356 | |
| 357 | loff_t onenand_addr(struct onenand_chip *this, int block) |
| 358 | { |
| 359 | if (!FLEXONENAND(this)) |
| 360 | return (loff_t)block << this->erase_shift; |
| 361 | return flexonenand_addr(this, block); |
| 362 | } |
| 363 | EXPORT_SYMBOL(onenand_addr); |
| 364 | |
| 365 | /** |
Kyungmin Park | e71f04f | 2007-12-11 11:23:45 +0900 | [diff] [blame] | 366 | * onenand_get_density - [DEFAULT] Get OneNAND density |
| 367 | * @param dev_id OneNAND device ID |
| 368 | * |
| 369 | * Get OneNAND density from device ID |
| 370 | */ |
| 371 | static inline int onenand_get_density(int dev_id) |
| 372 | { |
| 373 | int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT; |
| 374 | return (density & ONENAND_DEVICE_DENSITY_MASK); |
| 375 | } |
| 376 | |
| 377 | /** |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 378 | * flexonenand_region - [Flex-OneNAND] Return erase region of addr |
| 379 | * @param mtd MTD device structure |
| 380 | * @param addr address whose erase region needs to be identified |
| 381 | */ |
| 382 | int flexonenand_region(struct mtd_info *mtd, loff_t addr) |
| 383 | { |
| 384 | int i; |
| 385 | |
| 386 | for (i = 0; i < mtd->numeraseregions; i++) |
| 387 | if (addr < mtd->eraseregions[i].offset) |
| 388 | break; |
| 389 | return i - 1; |
| 390 | } |
| 391 | EXPORT_SYMBOL(flexonenand_region); |
| 392 | |
| 393 | /** |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 394 | * onenand_command - [DEFAULT] Send command to OneNAND device |
| 395 | * @param mtd MTD device structure |
| 396 | * @param cmd the command to be sent |
| 397 | * @param addr offset to read from or write to |
| 398 | * @param len number of bytes to read or write |
| 399 | * |
| 400 | * Send command to OneNAND device. This function is used for middle/large page |
| 401 | * devices (1KB/2KB Bytes per page) |
| 402 | */ |
| 403 | static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len) |
| 404 | { |
| 405 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | b21b72c | 2007-12-11 11:13:18 +0900 | [diff] [blame] | 406 | int value, block, page; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 407 | |
| 408 | /* Address translation */ |
| 409 | switch (cmd) { |
| 410 | case ONENAND_CMD_UNLOCK: |
| 411 | case ONENAND_CMD_LOCK: |
| 412 | case ONENAND_CMD_LOCK_TIGHT: |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 413 | case ONENAND_CMD_UNLOCK_ALL: |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 414 | block = -1; |
| 415 | page = -1; |
| 416 | break; |
| 417 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 418 | case FLEXONENAND_CMD_PI_ACCESS: |
| 419 | /* addr contains die index */ |
| 420 | block = addr * this->density_mask; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 421 | page = -1; |
| 422 | break; |
| 423 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 424 | case ONENAND_CMD_ERASE: |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 425 | case ONENAND_CMD_MULTIBLOCK_ERASE: |
| 426 | case ONENAND_CMD_ERASE_VERIFY: |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 427 | case ONENAND_CMD_BUFFERRAM: |
| 428 | case ONENAND_CMD_OTP_ACCESS: |
| 429 | block = onenand_block(this, addr); |
| 430 | page = -1; |
| 431 | break; |
| 432 | |
| 433 | case FLEXONENAND_CMD_READ_PI: |
| 434 | cmd = ONENAND_CMD_READ; |
| 435 | block = addr * this->density_mask; |
| 436 | page = 0; |
| 437 | break; |
| 438 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 439 | default: |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 440 | block = onenand_block(this, addr); |
Rohit Hassan Sathyanarayan | 42b0aab | 2010-07-23 12:29:25 +0530 | [diff] [blame] | 441 | if (FLEXONENAND(this)) |
| 442 | page = (int) (addr - onenand_addr(this, block))>>\ |
| 443 | this->page_shift; |
| 444 | else |
| 445 | page = (int) (addr >> this->page_shift); |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 446 | if (ONENAND_IS_2PLANE(this)) { |
| 447 | /* Make the even block number */ |
| 448 | block &= ~1; |
| 449 | /* Is it the odd plane? */ |
| 450 | if (addr & this->writesize) |
| 451 | block++; |
| 452 | page >>= 1; |
| 453 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 454 | page &= this->page_mask; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | /* NOTE: The setting order of the registers is very important! */ |
| 459 | if (cmd == ONENAND_CMD_BUFFERRAM) { |
| 460 | /* Select DataRAM for DDP */ |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 461 | value = onenand_bufferram_address(this, block); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 462 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 463 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 464 | if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 465 | /* It is always BufferRAM0 */ |
| 466 | ONENAND_SET_BUFFERRAM0(this); |
| 467 | else |
| 468 | /* Switch to the next data buffer */ |
| 469 | ONENAND_SET_NEXT_BUFFERRAM(this); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | if (block != -1) { |
| 475 | /* Write 'DFS, FBA' of Flash */ |
Kyungmin Park | 83a3683 | 2005-09-29 04:53:16 +0100 | [diff] [blame] | 476 | value = onenand_block_address(this, block); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 477 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1); |
Kyungmin Park | 3cecf69 | 2006-05-12 17:02:51 +0300 | [diff] [blame] | 478 | |
Kyungmin Park | b21b72c | 2007-12-11 11:13:18 +0900 | [diff] [blame] | 479 | /* Select DataRAM for DDP */ |
| 480 | value = onenand_bufferram_address(this, block); |
| 481 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (page != -1) { |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 485 | /* Now we use page size operation */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 486 | int sectors = 0, count = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 487 | int dataram; |
| 488 | |
| 489 | switch (cmd) { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 490 | case FLEXONENAND_CMD_RECOVER_LSB: |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 491 | case ONENAND_CMD_READ: |
| 492 | case ONENAND_CMD_READOOB: |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 493 | if (ONENAND_IS_4KB_PAGE(this)) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 494 | /* It is always BufferRAM0 */ |
| 495 | dataram = ONENAND_SET_BUFFERRAM0(this); |
| 496 | else |
| 497 | dataram = ONENAND_SET_NEXT_BUFFERRAM(this); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | default: |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 501 | if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG) |
| 502 | cmd = ONENAND_CMD_2X_PROG; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 503 | dataram = ONENAND_CURRENT_BUFFERRAM(this); |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | /* Write 'FPA, FSA' of Flash */ |
| 508 | value = onenand_page_address(page, sectors); |
| 509 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8); |
| 510 | |
| 511 | /* Write 'BSA, BSC' of DataRAM */ |
| 512 | value = onenand_buffer_address(dataram, sectors, count); |
| 513 | this->write_word(value, this->base + ONENAND_REG_START_BUFFER); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* Interrupt clear */ |
| 517 | this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT); |
| 518 | |
| 519 | /* Write command */ |
| 520 | this->write_word(cmd, this->base + ONENAND_REG_COMMAND); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | /** |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 526 | * onenand_read_ecc - return ecc status |
| 527 | * @param this onenand chip structure |
| 528 | */ |
| 529 | static inline int onenand_read_ecc(struct onenand_chip *this) |
| 530 | { |
| 531 | int ecc, i, result = 0; |
| 532 | |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 533 | if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this)) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 534 | return this->read_word(this->base + ONENAND_REG_ECC_STATUS); |
| 535 | |
| 536 | for (i = 0; i < 4; i++) { |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 537 | ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 538 | if (likely(!ecc)) |
| 539 | continue; |
| 540 | if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR) |
| 541 | return ONENAND_ECC_2BIT_ALL; |
| 542 | else |
| 543 | result = ONENAND_ECC_1BIT_ALL; |
| 544 | } |
| 545 | |
| 546 | return result; |
| 547 | } |
| 548 | |
| 549 | /** |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 550 | * onenand_wait - [DEFAULT] wait until the command is done |
| 551 | * @param mtd MTD device structure |
| 552 | * @param state state to select the max. timeout value |
| 553 | * |
| 554 | * Wait for command done. This applies to all OneNAND command |
| 555 | * Read can take up to 30us, erase up to 2ms and program up to 350us |
| 556 | * according to general OneNAND specs |
| 557 | */ |
| 558 | static int onenand_wait(struct mtd_info *mtd, int state) |
| 559 | { |
| 560 | struct onenand_chip * this = mtd->priv; |
| 561 | unsigned long timeout; |
| 562 | unsigned int flags = ONENAND_INT_MASTER; |
| 563 | unsigned int interrupt = 0; |
Kyungmin Park | 2fd32d4 | 2006-12-29 11:51:40 +0900 | [diff] [blame] | 564 | unsigned int ctrl; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 565 | |
| 566 | /* The 20 msec is enough */ |
| 567 | timeout = jiffies + msecs_to_jiffies(20); |
| 568 | while (time_before(jiffies, timeout)) { |
| 569 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 570 | |
| 571 | if (interrupt & flags) |
| 572 | break; |
| 573 | |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 574 | if (state != FL_READING && state != FL_PREPARING_ERASE) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 575 | cond_resched(); |
| 576 | } |
| 577 | /* To get correct interrupt status in timeout case */ |
| 578 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 579 | |
| 580 | ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS); |
| 581 | |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 582 | /* |
| 583 | * In the Spec. it checks the controller status first |
| 584 | * However if you get the correct information in case of |
| 585 | * power off recovery (POR) test, it should read ECC status first |
| 586 | */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 587 | if (interrupt & ONENAND_INT_READ) { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 588 | int ecc = onenand_read_ecc(this); |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 589 | if (ecc) { |
Kyungmin Park | b3c9f8b | 2007-01-05 19:16:04 +0900 | [diff] [blame] | 590 | if (ecc & ONENAND_ECC_2BIT_ALL) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 591 | printk(KERN_ERR "%s: ECC error = 0x%04x\n", |
| 592 | __func__, ecc); |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 593 | mtd->ecc_stats.failed++; |
Adrian Hunter | 30a7eb2 | 2007-10-12 10:19:38 +0300 | [diff] [blame] | 594 | return -EBADMSG; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 595 | } else if (ecc & ONENAND_ECC_1BIT_ALL) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 596 | printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n", |
| 597 | __func__, ecc); |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 598 | mtd->ecc_stats.corrected++; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 599 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 600 | } |
Adrian Hunter | 9d03280 | 2007-01-10 07:51:26 +0200 | [diff] [blame] | 601 | } else if (state == FL_READING) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 602 | printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n", |
| 603 | __func__, ctrl, interrupt); |
Adrian Hunter | 9d03280 | 2007-01-10 07:51:26 +0200 | [diff] [blame] | 604 | return -EIO; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 607 | if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) { |
| 608 | printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n", |
| 609 | __func__, ctrl, interrupt); |
| 610 | return -EIO; |
| 611 | } |
| 612 | |
| 613 | if (!(interrupt & ONENAND_INT_MASTER)) { |
| 614 | printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n", |
| 615 | __func__, ctrl, interrupt); |
| 616 | return -EIO; |
| 617 | } |
| 618 | |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 619 | /* If there's controller error, it's a real error */ |
| 620 | if (ctrl & ONENAND_CTRL_ERROR) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 621 | printk(KERN_ERR "%s: controller error = 0x%04x\n", |
| 622 | __func__, ctrl); |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 623 | if (ctrl & ONENAND_CTRL_LOCK) |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 624 | printk(KERN_ERR "%s: it's locked error.\n", __func__); |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 625 | return -EIO; |
| 626 | } |
| 627 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 631 | /* |
| 632 | * onenand_interrupt - [DEFAULT] onenand interrupt handler |
| 633 | * @param irq onenand interrupt number |
| 634 | * @param dev_id interrupt data |
| 635 | * |
| 636 | * complete the work |
| 637 | */ |
| 638 | static irqreturn_t onenand_interrupt(int irq, void *data) |
| 639 | { |
Jeff Garzik | 06efcad | 2007-10-19 03:10:11 -0400 | [diff] [blame] | 640 | struct onenand_chip *this = data; |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 641 | |
| 642 | /* To handle shared interrupt */ |
| 643 | if (!this->complete.done) |
| 644 | complete(&this->complete); |
| 645 | |
| 646 | return IRQ_HANDLED; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * onenand_interrupt_wait - [DEFAULT] wait until the command is done |
| 651 | * @param mtd MTD device structure |
| 652 | * @param state state to select the max. timeout value |
| 653 | * |
| 654 | * Wait for command done. |
| 655 | */ |
| 656 | static int onenand_interrupt_wait(struct mtd_info *mtd, int state) |
| 657 | { |
| 658 | struct onenand_chip *this = mtd->priv; |
| 659 | |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 660 | wait_for_completion(&this->complete); |
| 661 | |
| 662 | return onenand_wait(mtd, state); |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait |
| 667 | * @param mtd MTD device structure |
| 668 | * @param state state to select the max. timeout value |
| 669 | * |
| 670 | * Try interrupt based wait (It is used one-time) |
| 671 | */ |
| 672 | static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state) |
| 673 | { |
| 674 | struct onenand_chip *this = mtd->priv; |
| 675 | unsigned long remain, timeout; |
| 676 | |
| 677 | /* We use interrupt wait first */ |
| 678 | this->wait = onenand_interrupt_wait; |
| 679 | |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 680 | timeout = msecs_to_jiffies(100); |
| 681 | remain = wait_for_completion_timeout(&this->complete, timeout); |
| 682 | if (!remain) { |
| 683 | printk(KERN_INFO "OneNAND: There's no interrupt. " |
| 684 | "We use the normal wait\n"); |
| 685 | |
| 686 | /* Release the irq */ |
| 687 | free_irq(this->irq, this); |
David Woodhouse | c9ac597 | 2006-11-30 08:17:38 +0000 | [diff] [blame] | 688 | |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 689 | this->wait = onenand_wait; |
| 690 | } |
| 691 | |
| 692 | return onenand_wait(mtd, state); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * onenand_setup_wait - [OneNAND Interface] setup onenand wait method |
| 697 | * @param mtd MTD device structure |
| 698 | * |
| 699 | * There's two method to wait onenand work |
| 700 | * 1. polling - read interrupt status register |
| 701 | * 2. interrupt - use the kernel interrupt method |
| 702 | */ |
| 703 | static void onenand_setup_wait(struct mtd_info *mtd) |
| 704 | { |
| 705 | struct onenand_chip *this = mtd->priv; |
| 706 | int syscfg; |
| 707 | |
| 708 | init_completion(&this->complete); |
| 709 | |
| 710 | if (this->irq <= 0) { |
| 711 | this->wait = onenand_wait; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | if (request_irq(this->irq, &onenand_interrupt, |
| 716 | IRQF_SHARED, "onenand", this)) { |
| 717 | /* If we can't get irq, use the normal wait */ |
| 718 | this->wait = onenand_wait; |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | /* Enable interrupt */ |
| 723 | syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1); |
| 724 | syscfg |= ONENAND_SYS_CFG1_IOBE; |
| 725 | this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1); |
| 726 | |
| 727 | this->wait = onenand_try_interrupt_wait; |
| 728 | } |
| 729 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 730 | /** |
| 731 | * onenand_bufferram_offset - [DEFAULT] BufferRAM offset |
| 732 | * @param mtd MTD data structure |
| 733 | * @param area BufferRAM area |
| 734 | * @return offset given area |
| 735 | * |
| 736 | * Return BufferRAM offset given area |
| 737 | */ |
| 738 | static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area) |
| 739 | { |
| 740 | struct onenand_chip *this = mtd->priv; |
| 741 | |
| 742 | if (ONENAND_CURRENT_BUFFERRAM(this)) { |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 743 | /* Note: the 'this->writesize' is a real page size */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 744 | if (area == ONENAND_DATARAM) |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 745 | return this->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 746 | if (area == ONENAND_SPARERAM) |
| 747 | return mtd->oobsize; |
| 748 | } |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area |
| 755 | * @param mtd MTD data structure |
| 756 | * @param area BufferRAM area |
| 757 | * @param buffer the databuffer to put/get data |
| 758 | * @param offset offset to read from or write to |
| 759 | * @param count number of bytes to read/write |
| 760 | * |
| 761 | * Read the BufferRAM area |
| 762 | */ |
| 763 | static int onenand_read_bufferram(struct mtd_info *mtd, int area, |
| 764 | unsigned char *buffer, int offset, size_t count) |
| 765 | { |
| 766 | struct onenand_chip *this = mtd->priv; |
| 767 | void __iomem *bufferram; |
| 768 | |
| 769 | bufferram = this->base + area; |
| 770 | |
| 771 | bufferram += onenand_bufferram_offset(mtd, area); |
| 772 | |
Kyungmin Park | 9c01f87d | 2006-05-12 17:02:31 +0300 | [diff] [blame] | 773 | if (ONENAND_CHECK_BYTE_ACCESS(count)) { |
| 774 | unsigned short word; |
| 775 | |
| 776 | /* Align with word(16-bit) size */ |
| 777 | count--; |
| 778 | |
| 779 | /* Read word and save byte */ |
| 780 | word = this->read_word(bufferram + offset + count); |
| 781 | buffer[count] = (word & 0xff); |
| 782 | } |
| 783 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 784 | memcpy(buffer, bufferram + offset, count); |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /** |
Kyungmin Park | 52b0eea | 2005-09-03 07:07:19 +0100 | [diff] [blame] | 790 | * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode |
| 791 | * @param mtd MTD data structure |
| 792 | * @param area BufferRAM area |
| 793 | * @param buffer the databuffer to put/get data |
| 794 | * @param offset offset to read from or write to |
| 795 | * @param count number of bytes to read/write |
| 796 | * |
| 797 | * Read the BufferRAM area with Sync. Burst Mode |
| 798 | */ |
| 799 | static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area, |
| 800 | unsigned char *buffer, int offset, size_t count) |
| 801 | { |
| 802 | struct onenand_chip *this = mtd->priv; |
| 803 | void __iomem *bufferram; |
| 804 | |
| 805 | bufferram = this->base + area; |
| 806 | |
| 807 | bufferram += onenand_bufferram_offset(mtd, area); |
| 808 | |
| 809 | this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ); |
| 810 | |
Kyungmin Park | 9c01f87d | 2006-05-12 17:02:31 +0300 | [diff] [blame] | 811 | if (ONENAND_CHECK_BYTE_ACCESS(count)) { |
| 812 | unsigned short word; |
| 813 | |
| 814 | /* Align with word(16-bit) size */ |
| 815 | count--; |
| 816 | |
| 817 | /* Read word and save byte */ |
| 818 | word = this->read_word(bufferram + offset + count); |
| 819 | buffer[count] = (word & 0xff); |
| 820 | } |
| 821 | |
Kyungmin Park | 52b0eea | 2005-09-03 07:07:19 +0100 | [diff] [blame] | 822 | memcpy(buffer, bufferram + offset, count); |
| 823 | |
| 824 | this->mmcontrol(mtd, 0); |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /** |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 830 | * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area |
| 831 | * @param mtd MTD data structure |
| 832 | * @param area BufferRAM area |
| 833 | * @param buffer the databuffer to put/get data |
| 834 | * @param offset offset to read from or write to |
| 835 | * @param count number of bytes to read/write |
| 836 | * |
| 837 | * Write the BufferRAM area |
| 838 | */ |
| 839 | static int onenand_write_bufferram(struct mtd_info *mtd, int area, |
| 840 | const unsigned char *buffer, int offset, size_t count) |
| 841 | { |
| 842 | struct onenand_chip *this = mtd->priv; |
| 843 | void __iomem *bufferram; |
| 844 | |
| 845 | bufferram = this->base + area; |
| 846 | |
| 847 | bufferram += onenand_bufferram_offset(mtd, area); |
| 848 | |
Kyungmin Park | 9c01f87d | 2006-05-12 17:02:31 +0300 | [diff] [blame] | 849 | if (ONENAND_CHECK_BYTE_ACCESS(count)) { |
| 850 | unsigned short word; |
| 851 | int byte_offset; |
| 852 | |
| 853 | /* Align with word(16-bit) size */ |
| 854 | count--; |
| 855 | |
| 856 | /* Calculate byte access offset */ |
| 857 | byte_offset = offset + count; |
| 858 | |
| 859 | /* Read word and save byte */ |
| 860 | word = this->read_word(bufferram + byte_offset); |
| 861 | word = (word & ~0xff) | buffer[count]; |
| 862 | this->write_word(word, bufferram + byte_offset); |
| 863 | } |
| 864 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 865 | memcpy(bufferram + offset, buffer, count); |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /** |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 871 | * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode |
| 872 | * @param mtd MTD data structure |
| 873 | * @param addr address to check |
| 874 | * @return blockpage address |
| 875 | * |
| 876 | * Get blockpage address at 2x program mode |
| 877 | */ |
| 878 | static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr) |
| 879 | { |
| 880 | struct onenand_chip *this = mtd->priv; |
| 881 | int blockpage, block, page; |
| 882 | |
| 883 | /* Calculate the even block number */ |
| 884 | block = (int) (addr >> this->erase_shift) & ~1; |
| 885 | /* Is it the odd plane? */ |
| 886 | if (addr & this->writesize) |
| 887 | block++; |
| 888 | page = (int) (addr >> (this->page_shift + 1)) & this->page_mask; |
| 889 | blockpage = (block << 7) | page; |
| 890 | |
| 891 | return blockpage; |
| 892 | } |
| 893 | |
| 894 | /** |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 895 | * onenand_check_bufferram - [GENERIC] Check BufferRAM information |
| 896 | * @param mtd MTD data structure |
| 897 | * @param addr address to check |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 898 | * @return 1 if there are valid data, otherwise 0 |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 899 | * |
| 900 | * Check bufferram if there is data we required |
| 901 | */ |
| 902 | static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr) |
| 903 | { |
| 904 | struct onenand_chip *this = mtd->priv; |
Adrian Hunter | cde36b3 | 2007-02-08 10:28:08 +0200 | [diff] [blame] | 905 | int blockpage, found = 0; |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 906 | unsigned int i; |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 907 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 908 | if (ONENAND_IS_2PLANE(this)) |
| 909 | blockpage = onenand_get_2x_blockpage(mtd, addr); |
| 910 | else |
| 911 | blockpage = (int) (addr >> this->page_shift); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 912 | |
| 913 | /* Is there valid data? */ |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 914 | i = ONENAND_CURRENT_BUFFERRAM(this); |
| 915 | if (this->bufferram[i].blockpage == blockpage) |
Adrian Hunter | cde36b3 | 2007-02-08 10:28:08 +0200 | [diff] [blame] | 916 | found = 1; |
| 917 | else { |
| 918 | /* Check another BufferRAM */ |
| 919 | i = ONENAND_NEXT_BUFFERRAM(this); |
| 920 | if (this->bufferram[i].blockpage == blockpage) { |
| 921 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 922 | found = 1; |
| 923 | } |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 924 | } |
| 925 | |
Adrian Hunter | cde36b3 | 2007-02-08 10:28:08 +0200 | [diff] [blame] | 926 | if (found && ONENAND_IS_DDP(this)) { |
| 927 | /* Select DataRAM for DDP */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 928 | int block = onenand_block(this, addr); |
Adrian Hunter | cde36b3 | 2007-02-08 10:28:08 +0200 | [diff] [blame] | 929 | int value = onenand_bufferram_address(this, block); |
| 930 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 931 | } |
| 932 | |
| 933 | return found; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /** |
| 937 | * onenand_update_bufferram - [GENERIC] Update BufferRAM information |
| 938 | * @param mtd MTD data structure |
| 939 | * @param addr address to update |
| 940 | * @param valid valid flag |
| 941 | * |
| 942 | * Update BufferRAM information |
| 943 | */ |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 944 | static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr, |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 945 | int valid) |
| 946 | { |
| 947 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 948 | int blockpage; |
| 949 | unsigned int i; |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 950 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 951 | if (ONENAND_IS_2PLANE(this)) |
| 952 | blockpage = onenand_get_2x_blockpage(mtd, addr); |
| 953 | else |
| 954 | blockpage = (int) (addr >> this->page_shift); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 955 | |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 956 | /* Invalidate another BufferRAM */ |
| 957 | i = ONENAND_NEXT_BUFFERRAM(this); |
Kyungmin Park | 5b4246f | 2007-02-02 09:39:21 +0900 | [diff] [blame] | 958 | if (this->bufferram[i].blockpage == blockpage) |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 959 | this->bufferram[i].blockpage = -1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 960 | |
| 961 | /* Update BufferRAM */ |
| 962 | i = ONENAND_CURRENT_BUFFERRAM(this); |
Kyungmin Park | abf3c0f | 2007-02-02 09:29:36 +0900 | [diff] [blame] | 963 | if (valid) |
| 964 | this->bufferram[i].blockpage = blockpage; |
| 965 | else |
| 966 | this->bufferram[i].blockpage = -1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /** |
Adrian Hunter | 480b9df | 2007-02-07 13:55:19 +0200 | [diff] [blame] | 970 | * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information |
| 971 | * @param mtd MTD data structure |
| 972 | * @param addr start address to invalidate |
| 973 | * @param len length to invalidate |
| 974 | * |
| 975 | * Invalidate BufferRAM information |
| 976 | */ |
| 977 | static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr, |
| 978 | unsigned int len) |
| 979 | { |
| 980 | struct onenand_chip *this = mtd->priv; |
| 981 | int i; |
| 982 | loff_t end_addr = addr + len; |
| 983 | |
| 984 | /* Invalidate BufferRAM */ |
| 985 | for (i = 0; i < MAX_BUFFERRAM; i++) { |
| 986 | loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift; |
| 987 | if (buf_addr >= addr && buf_addr < end_addr) |
| 988 | this->bufferram[i].blockpage = -1; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /** |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 993 | * onenand_get_device - [GENERIC] Get chip for selected access |
| 994 | * @param mtd MTD device structure |
| 995 | * @param new_state the state which is requested |
| 996 | * |
| 997 | * Get the device and lock it for exclusive access |
| 998 | */ |
Kyungmin Park | a41371e | 2005-09-29 03:55:31 +0100 | [diff] [blame] | 999 | static int onenand_get_device(struct mtd_info *mtd, int new_state) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1000 | { |
| 1001 | struct onenand_chip *this = mtd->priv; |
| 1002 | DECLARE_WAITQUEUE(wait, current); |
| 1003 | |
| 1004 | /* |
| 1005 | * Grab the lock and see if the device is available |
| 1006 | */ |
| 1007 | while (1) { |
| 1008 | spin_lock(&this->chip_lock); |
| 1009 | if (this->state == FL_READY) { |
| 1010 | this->state = new_state; |
| 1011 | spin_unlock(&this->chip_lock); |
Adrian Hunter | cf24dc8 | 2010-02-19 15:39:52 +0100 | [diff] [blame] | 1012 | if (new_state != FL_PM_SUSPENDED && this->enable) |
| 1013 | this->enable(mtd); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1014 | break; |
| 1015 | } |
Kyungmin Park | a41371e | 2005-09-29 03:55:31 +0100 | [diff] [blame] | 1016 | if (new_state == FL_PM_SUSPENDED) { |
| 1017 | spin_unlock(&this->chip_lock); |
| 1018 | return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN; |
| 1019 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1020 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1021 | add_wait_queue(&this->wq, &wait); |
| 1022 | spin_unlock(&this->chip_lock); |
| 1023 | schedule(); |
| 1024 | remove_wait_queue(&this->wq, &wait); |
| 1025 | } |
Kyungmin Park | a41371e | 2005-09-29 03:55:31 +0100 | [diff] [blame] | 1026 | |
| 1027 | return 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * onenand_release_device - [GENERIC] release chip |
| 1032 | * @param mtd MTD device structure |
| 1033 | * |
| 1034 | * Deselect, release chip lock and wake up anyone waiting on the device |
| 1035 | */ |
| 1036 | static void onenand_release_device(struct mtd_info *mtd) |
| 1037 | { |
| 1038 | struct onenand_chip *this = mtd->priv; |
| 1039 | |
Adrian Hunter | cf24dc8 | 2010-02-19 15:39:52 +0100 | [diff] [blame] | 1040 | if (this->state != FL_PM_SUSPENDED && this->disable) |
| 1041 | this->disable(mtd); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1042 | /* Release the chip */ |
| 1043 | spin_lock(&this->chip_lock); |
| 1044 | this->state = FL_READY; |
| 1045 | wake_up(&this->wq); |
| 1046 | spin_unlock(&this->chip_lock); |
| 1047 | } |
| 1048 | |
| 1049 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1050 | * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1051 | * @param mtd MTD device structure |
| 1052 | * @param buf destination address |
| 1053 | * @param column oob offset to read from |
| 1054 | * @param thislen oob length to read |
| 1055 | */ |
| 1056 | static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column, |
| 1057 | int thislen) |
| 1058 | { |
| 1059 | struct onenand_chip *this = mtd->priv; |
Boris Brezillon | d30aae6 | 2016-02-03 20:12:31 +0100 | [diff] [blame] | 1060 | int ret; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1061 | |
Boris Brezillon | d30aae6 | 2016-02-03 20:12:31 +0100 | [diff] [blame] | 1062 | this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0, |
| 1063 | mtd->oobsize); |
| 1064 | ret = mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf, |
| 1065 | column, thislen); |
| 1066 | if (ret) |
| 1067 | return ret; |
| 1068 | |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1073 | * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data |
| 1074 | * @param mtd MTD device structure |
| 1075 | * @param addr address to recover |
| 1076 | * @param status return value from onenand_wait / onenand_bbt_wait |
| 1077 | * |
| 1078 | * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has |
| 1079 | * lower page address and MSB page has higher page address in paired pages. |
| 1080 | * If power off occurs during MSB page program, the paired LSB page data can |
| 1081 | * become corrupt. LSB page recovery read is a way to read LSB page though page |
| 1082 | * data are corrupted. When uncorrectable error occurs as a result of LSB page |
| 1083 | * read after power up, issue LSB page recovery read. |
| 1084 | */ |
| 1085 | static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status) |
| 1086 | { |
| 1087 | struct onenand_chip *this = mtd->priv; |
| 1088 | int i; |
| 1089 | |
| 1090 | /* Recovery is only for Flex-OneNAND */ |
| 1091 | if (!FLEXONENAND(this)) |
| 1092 | return status; |
| 1093 | |
| 1094 | /* check if we failed due to uncorrectable error */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 1095 | if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1096 | return status; |
| 1097 | |
| 1098 | /* check if address lies in MLC region */ |
| 1099 | i = flexonenand_region(mtd, addr); |
| 1100 | if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift)) |
| 1101 | return status; |
| 1102 | |
| 1103 | /* We are attempting to reread, so decrement stats.failed |
| 1104 | * which was incremented by onenand_wait due to read failure |
| 1105 | */ |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1106 | printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n", |
| 1107 | __func__); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1108 | mtd->ecc_stats.failed--; |
| 1109 | |
| 1110 | /* Issue the LSB page recovery command */ |
| 1111 | this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize); |
| 1112 | return this->wait(mtd, FL_READING); |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band |
| 1117 | * @param mtd MTD device structure |
| 1118 | * @param from offset to read from |
| 1119 | * @param ops: oob operation description structure |
| 1120 | * |
| 1121 | * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram. |
| 1122 | * So, read-while-load is not present. |
| 1123 | */ |
| 1124 | static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from, |
| 1125 | struct mtd_oob_ops *ops) |
| 1126 | { |
| 1127 | struct onenand_chip *this = mtd->priv; |
| 1128 | struct mtd_ecc_stats stats; |
| 1129 | size_t len = ops->len; |
| 1130 | size_t ooblen = ops->ooblen; |
| 1131 | u_char *buf = ops->datbuf; |
| 1132 | u_char *oobbuf = ops->oobbuf; |
| 1133 | int read = 0, column, thislen; |
| 1134 | int oobread = 0, oobcolumn, thisooblen, oobsize; |
| 1135 | int ret = 0; |
| 1136 | int writesize = this->writesize; |
| 1137 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1138 | pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from, |
| 1139 | (int)len); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1140 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1141 | oobsize = mtd_oobavail(mtd, ops); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1142 | oobcolumn = from & (mtd->oobsize - 1); |
| 1143 | |
| 1144 | /* Do not allow reads past end of device */ |
| 1145 | if (from + len > mtd->size) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1146 | printk(KERN_ERR "%s: Attempt read beyond end of device\n", |
| 1147 | __func__); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1148 | ops->retlen = 0; |
| 1149 | ops->oobretlen = 0; |
| 1150 | return -EINVAL; |
| 1151 | } |
| 1152 | |
| 1153 | stats = mtd->ecc_stats; |
| 1154 | |
| 1155 | while (read < len) { |
| 1156 | cond_resched(); |
| 1157 | |
| 1158 | thislen = min_t(int, writesize, len - read); |
| 1159 | |
| 1160 | column = from & (writesize - 1); |
| 1161 | if (column + thislen > writesize) |
| 1162 | thislen = writesize - column; |
| 1163 | |
| 1164 | if (!onenand_check_bufferram(mtd, from)) { |
| 1165 | this->command(mtd, ONENAND_CMD_READ, from, writesize); |
| 1166 | |
| 1167 | ret = this->wait(mtd, FL_READING); |
| 1168 | if (unlikely(ret)) |
| 1169 | ret = onenand_recover_lsb(mtd, from, ret); |
| 1170 | onenand_update_bufferram(mtd, from, !ret); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 1171 | if (mtd_is_eccerr(ret)) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1172 | ret = 0; |
Adrian Hunter | b085058 | 2011-02-08 12:02:38 +0200 | [diff] [blame] | 1173 | if (ret) |
| 1174 | break; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen); |
| 1178 | if (oobbuf) { |
| 1179 | thisooblen = oobsize - oobcolumn; |
| 1180 | thisooblen = min_t(int, thisooblen, ooblen - oobread); |
| 1181 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1182 | if (ops->mode == MTD_OPS_AUTO_OOB) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1183 | onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen); |
| 1184 | else |
| 1185 | this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen); |
| 1186 | oobread += thisooblen; |
| 1187 | oobbuf += thisooblen; |
| 1188 | oobcolumn = 0; |
| 1189 | } |
| 1190 | |
| 1191 | read += thislen; |
| 1192 | if (read == len) |
| 1193 | break; |
| 1194 | |
| 1195 | from += thislen; |
| 1196 | buf += thislen; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Return success, if no ECC failures, else -EBADMSG |
| 1201 | * fs driver will take care of that, because |
| 1202 | * retlen == desired len and result == -EBADMSG |
| 1203 | */ |
| 1204 | ops->retlen = read; |
| 1205 | ops->oobretlen = oobread; |
| 1206 | |
| 1207 | if (ret) |
| 1208 | return ret; |
| 1209 | |
| 1210 | if (mtd->ecc_stats.failed - stats.failed) |
| 1211 | return -EBADMSG; |
| 1212 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1213 | /* return max bitflips per ecc step; ONENANDs correct 1 bit only */ |
| 1214 | return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /** |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1218 | * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1219 | * @param mtd MTD device structure |
| 1220 | * @param from offset to read from |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1221 | * @param ops: oob operation description structure |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1222 | * |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1223 | * OneNAND read main and/or out-of-band data |
| 1224 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1225 | static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from, |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1226 | struct mtd_oob_ops *ops) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1227 | { |
| 1228 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 1229 | struct mtd_ecc_stats stats; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1230 | size_t len = ops->len; |
| 1231 | size_t ooblen = ops->ooblen; |
| 1232 | u_char *buf = ops->datbuf; |
| 1233 | u_char *oobbuf = ops->oobbuf; |
| 1234 | int read = 0, column, thislen; |
| 1235 | int oobread = 0, oobcolumn, thisooblen, oobsize; |
Adrian Hunter | 0fc2cce | 2007-01-09 17:55:21 +0200 | [diff] [blame] | 1236 | int ret = 0, boundary = 0; |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1237 | int writesize = this->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1238 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1239 | pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from, |
| 1240 | (int)len); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1241 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1242 | oobsize = mtd_oobavail(mtd, ops); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1243 | oobcolumn = from & (mtd->oobsize - 1); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1244 | |
| 1245 | /* Do not allow reads past end of device */ |
| 1246 | if ((from + len) > mtd->size) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1247 | printk(KERN_ERR "%s: Attempt read beyond end of device\n", |
| 1248 | __func__); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1249 | ops->retlen = 0; |
| 1250 | ops->oobretlen = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 1254 | stats = mtd->ecc_stats; |
Artem Bityutskiy | 61a7e19 | 2006-12-26 16:41:24 +0900 | [diff] [blame] | 1255 | |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1256 | /* Read-while-load method */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1257 | |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1258 | /* Do first load to bufferRAM */ |
| 1259 | if (read < len) { |
| 1260 | if (!onenand_check_bufferram(mtd, from)) { |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1261 | this->command(mtd, ONENAND_CMD_READ, from, writesize); |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1262 | ret = this->wait(mtd, FL_READING); |
| 1263 | onenand_update_bufferram(mtd, from, !ret); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 1264 | if (mtd_is_eccerr(ret)) |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1265 | ret = 0; |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1266 | } |
| 1267 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1268 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1269 | thislen = min_t(int, writesize, len - read); |
| 1270 | column = from & (writesize - 1); |
| 1271 | if (column + thislen > writesize) |
| 1272 | thislen = writesize - column; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1273 | |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1274 | while (!ret) { |
| 1275 | /* If there is more to load then start next load */ |
| 1276 | from += thislen; |
| 1277 | if (read + thislen < len) { |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1278 | this->command(mtd, ONENAND_CMD_READ, from, writesize); |
Adrian Hunter | 0fc2cce | 2007-01-09 17:55:21 +0200 | [diff] [blame] | 1279 | /* |
| 1280 | * Chip boundary handling in DDP |
| 1281 | * Now we issued chip 1 read and pointed chip 1 |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 1282 | * bufferram so we have to point chip 0 bufferram. |
Adrian Hunter | 0fc2cce | 2007-01-09 17:55:21 +0200 | [diff] [blame] | 1283 | */ |
Kyungmin Park | 738d61f | 2007-01-15 17:09:14 +0900 | [diff] [blame] | 1284 | if (ONENAND_IS_DDP(this) && |
| 1285 | unlikely(from == (this->chipsize >> 1))) { |
| 1286 | this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2); |
Adrian Hunter | 0fc2cce | 2007-01-09 17:55:21 +0200 | [diff] [blame] | 1287 | boundary = 1; |
| 1288 | } else |
| 1289 | boundary = 0; |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1290 | ONENAND_SET_PREV_BUFFERRAM(this); |
| 1291 | } |
| 1292 | /* While load is going, read from last bufferRAM */ |
| 1293 | this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1294 | |
| 1295 | /* Read oob area if needed */ |
| 1296 | if (oobbuf) { |
| 1297 | thisooblen = oobsize - oobcolumn; |
| 1298 | thisooblen = min_t(int, thisooblen, ooblen - oobread); |
| 1299 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1300 | if (ops->mode == MTD_OPS_AUTO_OOB) |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1301 | onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen); |
| 1302 | else |
| 1303 | this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen); |
| 1304 | oobread += thisooblen; |
| 1305 | oobbuf += thisooblen; |
| 1306 | oobcolumn = 0; |
| 1307 | } |
| 1308 | |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1309 | /* See if we are done */ |
| 1310 | read += thislen; |
| 1311 | if (read == len) |
| 1312 | break; |
| 1313 | /* Set up for next read from bufferRAM */ |
Adrian Hunter | 0fc2cce | 2007-01-09 17:55:21 +0200 | [diff] [blame] | 1314 | if (unlikely(boundary)) |
Kyungmin Park | 738d61f | 2007-01-15 17:09:14 +0900 | [diff] [blame] | 1315 | this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2); |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1316 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 1317 | buf += thislen; |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1318 | thislen = min_t(int, writesize, len - read); |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1319 | column = 0; |
| 1320 | cond_resched(); |
| 1321 | /* Now wait for load */ |
| 1322 | ret = this->wait(mtd, FL_READING); |
| 1323 | onenand_update_bufferram(mtd, from, !ret); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 1324 | if (mtd_is_eccerr(ret)) |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1325 | ret = 0; |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1326 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1327 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1328 | /* |
| 1329 | * Return success, if no ECC failures, else -EBADMSG |
| 1330 | * fs driver will take care of that, because |
| 1331 | * retlen == desired len and result == -EBADMSG |
| 1332 | */ |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1333 | ops->retlen = read; |
| 1334 | ops->oobretlen = oobread; |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 1335 | |
Adrian Hunter | a8de85d | 2007-01-04 09:51:26 +0200 | [diff] [blame] | 1336 | if (ret) |
| 1337 | return ret; |
| 1338 | |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1339 | if (mtd->ecc_stats.failed - stats.failed) |
| 1340 | return -EBADMSG; |
| 1341 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1342 | /* return max bitflips per ecc step; ONENANDs correct 1 bit only */ |
| 1343 | return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /** |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1347 | * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1348 | * @param mtd MTD device structure |
| 1349 | * @param from offset to read from |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1350 | * @param ops: oob operation description structure |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1351 | * |
| 1352 | * OneNAND read out-of-band data from the spare area |
| 1353 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1354 | static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from, |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1355 | struct mtd_oob_ops *ops) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1356 | { |
| 1357 | struct onenand_chip *this = mtd->priv; |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1358 | struct mtd_ecc_stats stats; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1359 | int read = 0, thislen, column, oobsize; |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1360 | size_t len = ops->ooblen; |
Brian Norris | 905c6bc | 2011-08-30 18:45:39 -0700 | [diff] [blame] | 1361 | unsigned int mode = ops->mode; |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1362 | u_char *buf = ops->oobbuf; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1363 | int ret = 0, readcmd; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1364 | |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1365 | from += ops->ooboffs; |
| 1366 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1367 | pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from, |
| 1368 | (int)len); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1369 | |
| 1370 | /* Initialize return length value */ |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1371 | ops->oobretlen = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1372 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1373 | if (mode == MTD_OPS_AUTO_OOB) |
Boris BREZILLON | f5b8aa7 | 2016-03-07 10:46:51 +0100 | [diff] [blame] | 1374 | oobsize = mtd->oobavail; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1375 | else |
| 1376 | oobsize = mtd->oobsize; |
| 1377 | |
| 1378 | column = from & (mtd->oobsize - 1); |
| 1379 | |
| 1380 | if (unlikely(column >= oobsize)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1381 | printk(KERN_ERR "%s: Attempted to start read outside oob\n", |
| 1382 | __func__); |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1386 | /* Do not allow reads past end of device */ |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1387 | if (unlikely(from >= mtd->size || |
| 1388 | column + len > ((mtd->size >> this->page_shift) - |
| 1389 | (from >> this->page_shift)) * oobsize)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1390 | printk(KERN_ERR "%s: Attempted to read beyond end of device\n", |
| 1391 | __func__); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1392 | return -EINVAL; |
| 1393 | } |
| 1394 | |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1395 | stats = mtd->ecc_stats; |
| 1396 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 1397 | readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1398 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1399 | while (read < len) { |
Artem Bityutskiy | 61a7e19 | 2006-12-26 16:41:24 +0900 | [diff] [blame] | 1400 | cond_resched(); |
| 1401 | |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1402 | thislen = oobsize - column; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1403 | thislen = min_t(int, thislen, len); |
| 1404 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1405 | this->command(mtd, readcmd, from, mtd->oobsize); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1406 | |
| 1407 | onenand_update_bufferram(mtd, from, 0); |
| 1408 | |
| 1409 | ret = this->wait(mtd, FL_READING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1410 | if (unlikely(ret)) |
| 1411 | ret = onenand_recover_lsb(mtd, from, ret); |
| 1412 | |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 1413 | if (ret && !mtd_is_eccerr(ret)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1414 | printk(KERN_ERR "%s: read failed = 0x%x\n", |
| 1415 | __func__, ret); |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1416 | break; |
| 1417 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1418 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1419 | if (mode == MTD_OPS_AUTO_OOB) |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1420 | onenand_transfer_auto_oob(mtd, buf, column, thislen); |
| 1421 | else |
| 1422 | this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1423 | |
| 1424 | read += thislen; |
| 1425 | |
| 1426 | if (read == len) |
| 1427 | break; |
| 1428 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1429 | buf += thislen; |
| 1430 | |
| 1431 | /* Read more? */ |
| 1432 | if (read < len) { |
| 1433 | /* Page size */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1434 | from += mtd->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1435 | column = 0; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 1439 | ops->oobretlen = read; |
Adrian Hunter | 5f4d47d | 2007-11-06 09:17:25 +0200 | [diff] [blame] | 1440 | |
| 1441 | if (ret) |
| 1442 | return ret; |
| 1443 | |
| 1444 | if (mtd->ecc_stats.failed - stats.failed) |
| 1445 | return -EBADMSG; |
| 1446 | |
| 1447 | return 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1450 | /** |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1451 | * onenand_read - [MTD Interface] Read data from flash |
| 1452 | * @param mtd MTD device structure |
| 1453 | * @param from offset to read from |
| 1454 | * @param len number of bytes to read |
| 1455 | * @param retlen pointer to variable to store the number of read bytes |
| 1456 | * @param buf the databuffer to put data |
| 1457 | * |
| 1458 | * Read with ecc |
| 1459 | */ |
| 1460 | static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 1461 | size_t *retlen, u_char *buf) |
| 1462 | { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1463 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1464 | struct mtd_oob_ops ops = { |
| 1465 | .len = len, |
| 1466 | .ooblen = 0, |
| 1467 | .datbuf = buf, |
| 1468 | .oobbuf = NULL, |
| 1469 | }; |
| 1470 | int ret; |
| 1471 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1472 | onenand_get_device(mtd, FL_READING); |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 1473 | ret = ONENAND_IS_4KB_PAGE(this) ? |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1474 | onenand_mlc_read_ops_nolock(mtd, from, &ops) : |
| 1475 | onenand_read_ops_nolock(mtd, from, &ops); |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1476 | onenand_release_device(mtd); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1477 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1478 | *retlen = ops.retlen; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1479 | return ret; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * onenand_read_oob - [MTD Interface] Read main and/or out-of-band |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 1484 | * @param mtd: MTD device structure |
| 1485 | * @param from: offset to read from |
| 1486 | * @param ops: oob operation description structure |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1487 | |
| 1488 | * Read main and/or out-of-band |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1489 | */ |
| 1490 | static int onenand_read_oob(struct mtd_info *mtd, loff_t from, |
| 1491 | struct mtd_oob_ops *ops) |
| 1492 | { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1493 | struct onenand_chip *this = mtd->priv; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1494 | int ret; |
| 1495 | |
Kyungmin Park | 4f4fad2 | 2007-02-02 09:22:21 +0900 | [diff] [blame] | 1496 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1497 | case MTD_OPS_PLACE_OOB: |
| 1498 | case MTD_OPS_AUTO_OOB: |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1499 | break; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1500 | case MTD_OPS_RAW: |
Kyungmin Park | 4f4fad2 | 2007-02-02 09:22:21 +0900 | [diff] [blame] | 1501 | /* Not implemented yet */ |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1502 | default: |
| 1503 | return -EINVAL; |
| 1504 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1505 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1506 | onenand_get_device(mtd, FL_READING); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1507 | if (ops->datbuf) |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 1508 | ret = ONENAND_IS_4KB_PAGE(this) ? |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1509 | onenand_mlc_read_ops_nolock(mtd, from, ops) : |
| 1510 | onenand_read_ops_nolock(mtd, from, ops); |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1511 | else |
| 1512 | ret = onenand_read_oob_nolock(mtd, from, ops); |
| 1513 | onenand_release_device(mtd); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1514 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1515 | return ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1518 | /** |
| 1519 | * onenand_bbt_wait - [DEFAULT] wait until the command is done |
| 1520 | * @param mtd MTD device structure |
| 1521 | * @param state state to select the max. timeout value |
| 1522 | * |
| 1523 | * Wait for command done. |
| 1524 | */ |
| 1525 | static int onenand_bbt_wait(struct mtd_info *mtd, int state) |
| 1526 | { |
| 1527 | struct onenand_chip *this = mtd->priv; |
| 1528 | unsigned long timeout; |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1529 | unsigned int interrupt, ctrl, ecc, addr1, addr8; |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1530 | |
| 1531 | /* The 20 msec is enough */ |
| 1532 | timeout = jiffies + msecs_to_jiffies(20); |
| 1533 | while (time_before(jiffies, timeout)) { |
| 1534 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 1535 | if (interrupt & ONENAND_INT_MASTER) |
| 1536 | break; |
| 1537 | } |
| 1538 | /* To get correct interrupt status in timeout case */ |
| 1539 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 1540 | ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS); |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1541 | addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1); |
| 1542 | addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8); |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1543 | |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1544 | if (interrupt & ONENAND_INT_READ) { |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1545 | ecc = onenand_read_ecc(this); |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 1546 | if (ecc & ONENAND_ECC_2BIT_ALL) { |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1547 | printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x " |
| 1548 | "intr 0x%04x addr1 %#x addr8 %#x\n", |
| 1549 | __func__, ecc, ctrl, interrupt, addr1, addr8); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1550 | return ONENAND_BBT_READ_ECC_ERROR; |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 1551 | } |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1552 | } else { |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1553 | printk(KERN_ERR "%s: read timeout! ctrl 0x%04x " |
| 1554 | "intr 0x%04x addr1 %#x addr8 %#x\n", |
| 1555 | __func__, ctrl, interrupt, addr1, addr8); |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1556 | return ONENAND_BBT_READ_FATAL_ERROR; |
| 1557 | } |
| 1558 | |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 1559 | /* Initial bad block case: 0x2400 or 0x0400 */ |
| 1560 | if (ctrl & ONENAND_CTRL_ERROR) { |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 1561 | printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x " |
| 1562 | "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8); |
Kyungmin Park | 83973b8 | 2008-05-29 14:52:40 +0900 | [diff] [blame] | 1563 | return ONENAND_BBT_READ_ERROR; |
| 1564 | } |
| 1565 | |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | /** |
| 1570 | * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan |
| 1571 | * @param mtd MTD device structure |
| 1572 | * @param from offset to read from |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 1573 | * @param ops oob operation description structure |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1574 | * |
| 1575 | * OneNAND read out-of-band data from the spare area for bbt scan |
| 1576 | */ |
| 1577 | int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, |
| 1578 | struct mtd_oob_ops *ops) |
| 1579 | { |
| 1580 | struct onenand_chip *this = mtd->priv; |
| 1581 | int read = 0, thislen, column; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1582 | int ret = 0, readcmd; |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1583 | size_t len = ops->ooblen; |
| 1584 | u_char *buf = ops->oobbuf; |
| 1585 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1586 | pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from, |
| 1587 | len); |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1588 | |
| 1589 | /* Initialize return value */ |
| 1590 | ops->oobretlen = 0; |
| 1591 | |
| 1592 | /* Do not allow reads past end of device */ |
| 1593 | if (unlikely((from + len) > mtd->size)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1594 | printk(KERN_ERR "%s: Attempt read beyond end of device\n", |
| 1595 | __func__); |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1596 | return ONENAND_BBT_READ_FATAL_ERROR; |
| 1597 | } |
| 1598 | |
| 1599 | /* Grab the lock and see if the device is available */ |
| 1600 | onenand_get_device(mtd, FL_READING); |
| 1601 | |
| 1602 | column = from & (mtd->oobsize - 1); |
| 1603 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 1604 | readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1605 | |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1606 | while (read < len) { |
| 1607 | cond_resched(); |
| 1608 | |
| 1609 | thislen = mtd->oobsize - column; |
| 1610 | thislen = min_t(int, thislen, len); |
| 1611 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1612 | this->command(mtd, readcmd, from, mtd->oobsize); |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1613 | |
| 1614 | onenand_update_bufferram(mtd, from, 0); |
| 1615 | |
Kyungmin Park | 31bb999 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1616 | ret = this->bbt_wait(mtd, FL_READING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1617 | if (unlikely(ret)) |
| 1618 | ret = onenand_recover_lsb(mtd, from, ret); |
| 1619 | |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1620 | if (ret) |
| 1621 | break; |
| 1622 | |
| 1623 | this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen); |
| 1624 | read += thislen; |
| 1625 | if (read == len) |
| 1626 | break; |
| 1627 | |
| 1628 | buf += thislen; |
| 1629 | |
| 1630 | /* Read more? */ |
| 1631 | if (read < len) { |
| 1632 | /* Update Page size */ |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1633 | from += this->writesize; |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 1634 | column = 0; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | /* Deselect and wake up anyone waiting on the device */ |
| 1639 | onenand_release_device(mtd); |
| 1640 | |
| 1641 | ops->oobretlen = read; |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1645 | #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE |
| 1646 | /** |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1647 | * onenand_verify_oob - [GENERIC] verify the oob contents after a write |
| 1648 | * @param mtd MTD device structure |
| 1649 | * @param buf the databuffer to verify |
| 1650 | * @param to offset to read from |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1651 | */ |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1652 | static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to) |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1653 | { |
| 1654 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 69d7918 | 2007-12-14 14:47:21 +0900 | [diff] [blame] | 1655 | u_char *oob_buf = this->oob_buf; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1656 | int status, i, readcmd; |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1657 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 1658 | readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 1659 | |
| 1660 | this->command(mtd, readcmd, to, mtd->oobsize); |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1661 | onenand_update_bufferram(mtd, to, 0); |
| 1662 | status = this->wait(mtd, FL_READING); |
| 1663 | if (status) |
| 1664 | return status; |
| 1665 | |
Kyungmin Park | 69d7918 | 2007-12-14 14:47:21 +0900 | [diff] [blame] | 1666 | this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize); |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 1667 | for (i = 0; i < mtd->oobsize; i++) |
Kyungmin Park | 69d7918 | 2007-12-14 14:47:21 +0900 | [diff] [blame] | 1668 | if (buf[i] != 0xFF && buf[i] != oob_buf[i]) |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1669 | return -EBADMSG; |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | /** |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1675 | * onenand_verify - [GENERIC] verify the chip contents after a write |
| 1676 | * @param mtd MTD device structure |
| 1677 | * @param buf the databuffer to verify |
| 1678 | * @param addr offset to read from |
| 1679 | * @param len number of bytes to read and compare |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1680 | */ |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1681 | static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1682 | { |
| 1683 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1684 | int ret = 0; |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1685 | int thislen, column; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1686 | |
Roman Tereshonkov | e6da856 | 2011-02-08 12:02:42 +0200 | [diff] [blame] | 1687 | column = addr & (this->writesize - 1); |
| 1688 | |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1689 | while (len != 0) { |
Roman Tereshonkov | e6da856 | 2011-02-08 12:02:42 +0200 | [diff] [blame] | 1690 | thislen = min_t(int, this->writesize - column, len); |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 1691 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 1692 | this->command(mtd, ONENAND_CMD_READ, addr, this->writesize); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1693 | |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1694 | onenand_update_bufferram(mtd, addr, 0); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1695 | |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1696 | ret = this->wait(mtd, FL_READING); |
| 1697 | if (ret) |
| 1698 | return ret; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1699 | |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1700 | onenand_update_bufferram(mtd, addr, 1); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1701 | |
Kyungmin Park | 3328dc3 | 2010-04-28 17:46:47 +0200 | [diff] [blame] | 1702 | this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize); |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1703 | |
Roman Tereshonkov | e6da856 | 2011-02-08 12:02:42 +0200 | [diff] [blame] | 1704 | if (memcmp(buf, this->verify_buf + column, thislen)) |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1705 | return -EBADMSG; |
| 1706 | |
| 1707 | len -= thislen; |
| 1708 | buf += thislen; |
| 1709 | addr += thislen; |
Roman Tereshonkov | e6da856 | 2011-02-08 12:02:42 +0200 | [diff] [blame] | 1710 | column = 0; |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1711 | } |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 1712 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1713 | return 0; |
| 1714 | } |
| 1715 | #else |
Adrian Hunter | 8b29c0b | 2007-01-25 14:06:33 +0900 | [diff] [blame] | 1716 | #define onenand_verify(...) (0) |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 1717 | #define onenand_verify_oob(...) (0) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1718 | #endif |
| 1719 | |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 1720 | #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1721 | |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1722 | static void onenand_panic_wait(struct mtd_info *mtd) |
| 1723 | { |
| 1724 | struct onenand_chip *this = mtd->priv; |
| 1725 | unsigned int interrupt; |
| 1726 | int i; |
| 1727 | |
| 1728 | for (i = 0; i < 2000; i++) { |
| 1729 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 1730 | if (interrupt & ONENAND_INT_MASTER) |
| 1731 | break; |
| 1732 | udelay(10); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | /** |
| 1737 | * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context |
| 1738 | * @param mtd MTD device structure |
| 1739 | * @param to offset to write to |
| 1740 | * @param len number of bytes to write |
| 1741 | * @param retlen pointer to variable to store the number of written bytes |
| 1742 | * @param buf the data to write |
| 1743 | * |
| 1744 | * Write with ECC |
| 1745 | */ |
| 1746 | static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 1747 | size_t *retlen, const u_char *buf) |
| 1748 | { |
| 1749 | struct onenand_chip *this = mtd->priv; |
| 1750 | int column, subpage; |
| 1751 | int written = 0; |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1752 | |
| 1753 | if (this->state == FL_PM_SUSPENDED) |
| 1754 | return -EBUSY; |
| 1755 | |
| 1756 | /* Wait for any existing operation to clear */ |
| 1757 | onenand_panic_wait(mtd); |
| 1758 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1759 | pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to, |
| 1760 | (int)len); |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1761 | |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1762 | /* Reject writes, which are not page aligned */ |
Roel Kluin | b73d7e43 | 2008-02-16 18:14:35 +0100 | [diff] [blame] | 1763 | if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1764 | printk(KERN_ERR "%s: Attempt to write not page aligned data\n", |
| 1765 | __func__); |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1766 | return -EINVAL; |
| 1767 | } |
| 1768 | |
| 1769 | column = to & (mtd->writesize - 1); |
| 1770 | |
| 1771 | /* Loop until all data write */ |
| 1772 | while (written < len) { |
| 1773 | int thislen = min_t(int, mtd->writesize - column, len - written); |
| 1774 | u_char *wbuf = (u_char *) buf; |
| 1775 | |
| 1776 | this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen); |
| 1777 | |
| 1778 | /* Partial page write */ |
| 1779 | subpage = thislen < mtd->writesize; |
| 1780 | if (subpage) { |
| 1781 | memset(this->page_buf, 0xff, mtd->writesize); |
| 1782 | memcpy(this->page_buf + column, buf, thislen); |
| 1783 | wbuf = this->page_buf; |
| 1784 | } |
| 1785 | |
| 1786 | this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize); |
| 1787 | this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize); |
| 1788 | |
| 1789 | this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize); |
| 1790 | |
| 1791 | onenand_panic_wait(mtd); |
| 1792 | |
| 1793 | /* In partial page write we don't update bufferram */ |
Brian Norris | 7f2a7ce | 2015-02-28 02:02:25 -0800 | [diff] [blame] | 1794 | onenand_update_bufferram(mtd, to, !subpage); |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1795 | if (ONENAND_IS_2PLANE(this)) { |
| 1796 | ONENAND_SET_BUFFERRAM1(this); |
Brian Norris | 7f2a7ce | 2015-02-28 02:02:25 -0800 | [diff] [blame] | 1797 | onenand_update_bufferram(mtd, to + this->writesize, !subpage); |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | written += thislen; |
| 1801 | |
| 1802 | if (written == len) |
| 1803 | break; |
| 1804 | |
| 1805 | column = 0; |
| 1806 | to += thislen; |
| 1807 | buf += thislen; |
| 1808 | } |
| 1809 | |
| 1810 | *retlen = written; |
Brian Norris | 7f2a7ce | 2015-02-28 02:02:25 -0800 | [diff] [blame] | 1811 | return 0; |
Richard Purdie | 6c77fd6 | 2008-02-06 10:18:22 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 1814 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1815 | * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1816 | * @param mtd MTD device structure |
| 1817 | * @param oob_buf oob buffer |
| 1818 | * @param buf source address |
| 1819 | * @param column oob offset to write to |
| 1820 | * @param thislen oob length to write |
| 1821 | */ |
| 1822 | static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf, |
| 1823 | const u_char *buf, int column, int thislen) |
| 1824 | { |
Boris Brezillon | d30aae6 | 2016-02-03 20:12:31 +0100 | [diff] [blame] | 1825 | return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen); |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /** |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1829 | * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1830 | * @param mtd MTD device structure |
| 1831 | * @param to offset to write to |
| 1832 | * @param ops oob operation description structure |
| 1833 | * |
| 1834 | * Write main and/or oob with ECC |
| 1835 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 1836 | static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to, |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1837 | struct mtd_oob_ops *ops) |
| 1838 | { |
| 1839 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1840 | int written = 0, column, thislen = 0, subpage = 0; |
| 1841 | int prev = 0, prevlen = 0, prev_subpage = 0, first = 1; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1842 | int oobwritten = 0, oobcolumn, thisooblen, oobsize; |
| 1843 | size_t len = ops->len; |
| 1844 | size_t ooblen = ops->ooblen; |
| 1845 | const u_char *buf = ops->datbuf; |
| 1846 | const u_char *oob = ops->oobbuf; |
| 1847 | u_char *oobbuf; |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 1848 | int ret = 0, cmd; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1849 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 1850 | pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to, |
| 1851 | (int)len); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1852 | |
| 1853 | /* Initialize retlen, in case of early exit */ |
| 1854 | ops->retlen = 0; |
| 1855 | ops->oobretlen = 0; |
| 1856 | |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1857 | /* Reject writes, which are not page aligned */ |
Roel Kluin | b73d7e43 | 2008-02-16 18:14:35 +0100 | [diff] [blame] | 1858 | if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1859 | printk(KERN_ERR "%s: Attempt to write not page aligned data\n", |
| 1860 | __func__); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1861 | return -EINVAL; |
| 1862 | } |
| 1863 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1864 | /* Check zero length */ |
| 1865 | if (!len) |
| 1866 | return 0; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1867 | oobsize = mtd_oobavail(mtd, ops); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1868 | oobcolumn = to & (mtd->oobsize - 1); |
| 1869 | |
| 1870 | column = to & (mtd->writesize - 1); |
| 1871 | |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1872 | /* Loop until all data write */ |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1873 | while (1) { |
| 1874 | if (written < len) { |
| 1875 | u_char *wbuf = (u_char *) buf; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1876 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1877 | thislen = min_t(int, mtd->writesize - column, len - written); |
| 1878 | thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1879 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1880 | cond_resched(); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1881 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1882 | this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1883 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1884 | /* Partial page write */ |
| 1885 | subpage = thislen < mtd->writesize; |
| 1886 | if (subpage) { |
| 1887 | memset(this->page_buf, 0xff, mtd->writesize); |
| 1888 | memcpy(this->page_buf + column, buf, thislen); |
| 1889 | wbuf = this->page_buf; |
| 1890 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1891 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1892 | this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1893 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1894 | if (oob) { |
| 1895 | oobbuf = this->oob_buf; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1896 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1897 | /* We send data to spare ram with oobsize |
| 1898 | * to prevent byte access */ |
| 1899 | memset(oobbuf, 0xff, mtd->oobsize); |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1900 | if (ops->mode == MTD_OPS_AUTO_OOB) |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1901 | onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen); |
| 1902 | else |
| 1903 | memcpy(oobbuf + oobcolumn, oob, thisooblen); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1904 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1905 | oobwritten += thisooblen; |
| 1906 | oob += thisooblen; |
| 1907 | oobcolumn = 0; |
| 1908 | } else |
| 1909 | oobbuf = (u_char *) ffchars; |
| 1910 | |
| 1911 | this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1912 | } else |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1913 | ONENAND_SET_NEXT_BUFFERRAM(this); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1914 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1915 | /* |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 1916 | * 2 PLANE, MLC, and Flex-OneNAND do not support |
| 1917 | * write-while-program feature. |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1918 | */ |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 1919 | if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) { |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1920 | ONENAND_SET_PREV_BUFFERRAM(this); |
| 1921 | |
| 1922 | ret = this->wait(mtd, FL_WRITING); |
| 1923 | |
| 1924 | /* In partial page write we don't update bufferram */ |
| 1925 | onenand_update_bufferram(mtd, prev, !ret && !prev_subpage); |
| 1926 | if (ret) { |
| 1927 | written -= prevlen; |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1928 | printk(KERN_ERR "%s: write failed %d\n", |
| 1929 | __func__, ret); |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1930 | break; |
| 1931 | } |
| 1932 | |
| 1933 | if (written == len) { |
| 1934 | /* Only check verify write turn on */ |
| 1935 | ret = onenand_verify(mtd, buf - len, to - len, len); |
| 1936 | if (ret) |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1937 | printk(KERN_ERR "%s: verify failed %d\n", |
| 1938 | __func__, ret); |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1939 | break; |
| 1940 | } |
| 1941 | |
| 1942 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 1943 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1944 | |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 1945 | this->ongoing = 0; |
| 1946 | cmd = ONENAND_CMD_PROG; |
| 1947 | |
| 1948 | /* Exclude 1st OTP and OTP blocks for cache program feature */ |
| 1949 | if (ONENAND_IS_CACHE_PROGRAM(this) && |
| 1950 | likely(onenand_block(this, to) != 0) && |
| 1951 | ONENAND_IS_4KB_PAGE(this) && |
| 1952 | ((written + thislen) < len)) { |
| 1953 | cmd = ONENAND_CMD_2X_CACHE_PROG; |
| 1954 | this->ongoing = 1; |
| 1955 | } |
| 1956 | |
| 1957 | this->command(mtd, cmd, to, mtd->writesize); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1958 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1959 | /* |
| 1960 | * 2 PLANE, MLC, and Flex-OneNAND wait here |
| 1961 | */ |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 1962 | if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) { |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1963 | ret = this->wait(mtd, FL_WRITING); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1964 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1965 | /* In partial page write we don't update bufferram */ |
| 1966 | onenand_update_bufferram(mtd, to, !ret && !subpage); |
| 1967 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1968 | printk(KERN_ERR "%s: write failed %d\n", |
| 1969 | __func__, ret); |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1970 | break; |
| 1971 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1972 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1973 | /* Only check verify write turn on */ |
| 1974 | ret = onenand_verify(mtd, buf, to, thislen); |
| 1975 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 1976 | printk(KERN_ERR "%s: verify failed %d\n", |
| 1977 | __func__, ret); |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1978 | break; |
| 1979 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1980 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1981 | written += thislen; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1982 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1983 | if (written == len) |
| 1984 | break; |
| 1985 | |
| 1986 | } else |
| 1987 | written += thislen; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1988 | |
| 1989 | column = 0; |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1990 | prev_subpage = subpage; |
| 1991 | prev = to; |
| 1992 | prevlen = thislen; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1993 | to += thislen; |
| 1994 | buf += thislen; |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1995 | first = 0; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 1996 | } |
| 1997 | |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 1998 | /* In error case, clear all bufferrams */ |
| 1999 | if (written != len) |
| 2000 | onenand_invalidate_bufferram(mtd, 0, -1); |
| 2001 | |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2002 | ops->retlen = written; |
Kyungmin Park | 9ce9690 | 2008-11-17 17:54:28 +0900 | [diff] [blame] | 2003 | ops->oobretlen = oobwritten; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2004 | |
| 2005 | return ret; |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2010 | * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2011 | * @param mtd MTD device structure |
| 2012 | * @param to offset to write to |
| 2013 | * @param len number of bytes to write |
| 2014 | * @param retlen pointer to variable to store the number of written bytes |
| 2015 | * @param buf the data to write |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2016 | * @param mode operation mode |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2017 | * |
| 2018 | * OneNAND write out-of-band |
| 2019 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2020 | static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to, |
| 2021 | struct mtd_oob_ops *ops) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2022 | { |
| 2023 | struct onenand_chip *this = mtd->priv; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2024 | int column, ret = 0, oobsize; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2025 | int written = 0, oobcmd; |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2026 | u_char *oobbuf; |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2027 | size_t len = ops->ooblen; |
| 2028 | const u_char *buf = ops->oobbuf; |
Brian Norris | 905c6bc | 2011-08-30 18:45:39 -0700 | [diff] [blame] | 2029 | unsigned int mode = ops->mode; |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2030 | |
| 2031 | to += ops->ooboffs; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2032 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 2033 | pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to, |
| 2034 | (int)len); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2035 | |
| 2036 | /* Initialize retlen, in case of early exit */ |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2037 | ops->oobretlen = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2038 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2039 | if (mode == MTD_OPS_AUTO_OOB) |
Boris BREZILLON | f5b8aa7 | 2016-03-07 10:46:51 +0100 | [diff] [blame] | 2040 | oobsize = mtd->oobavail; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2041 | else |
| 2042 | oobsize = mtd->oobsize; |
| 2043 | |
| 2044 | column = to & (mtd->oobsize - 1); |
| 2045 | |
| 2046 | if (unlikely(column >= oobsize)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2047 | printk(KERN_ERR "%s: Attempted to start write outside oob\n", |
| 2048 | __func__); |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2049 | return -EINVAL; |
| 2050 | } |
| 2051 | |
Adrian Hunter | 52e4200 | 2007-02-06 09:15:39 +0900 | [diff] [blame] | 2052 | /* For compatibility with NAND: Do not allow write past end of page */ |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2053 | if (unlikely(column + len > oobsize)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2054 | printk(KERN_ERR "%s: Attempt to write past end of page\n", |
| 2055 | __func__); |
Adrian Hunter | 52e4200 | 2007-02-06 09:15:39 +0900 | [diff] [blame] | 2056 | return -EINVAL; |
| 2057 | } |
| 2058 | |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2059 | /* Do not allow reads past end of device */ |
| 2060 | if (unlikely(to >= mtd->size || |
| 2061 | column + len > ((mtd->size >> this->page_shift) - |
| 2062 | (to >> this->page_shift)) * oobsize)) { |
David Woodhouse | 8032747 | 2009-10-05 08:30:04 +0100 | [diff] [blame] | 2063 | printk(KERN_ERR "%s: Attempted to write past end of device\n", |
| 2064 | __func__); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2065 | return -EINVAL; |
| 2066 | } |
| 2067 | |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 2068 | oobbuf = this->oob_buf; |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2069 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 2070 | oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2071 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2072 | /* Loop until all data write */ |
| 2073 | while (written < len) { |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2074 | int thislen = min_t(int, oobsize, len - written); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2075 | |
Artem Bityutskiy | 61a7e19 | 2006-12-26 16:41:24 +0900 | [diff] [blame] | 2076 | cond_resched(); |
| 2077 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2078 | this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize); |
| 2079 | |
Kyungmin Park | 34c1060 | 2006-05-12 17:02:46 +0300 | [diff] [blame] | 2080 | /* We send data to spare ram with oobsize |
| 2081 | * to prevent byte access */ |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2082 | memset(oobbuf, 0xff, mtd->oobsize); |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2083 | if (mode == MTD_OPS_AUTO_OOB) |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2084 | onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen); |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2085 | else |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2086 | memcpy(oobbuf + column, buf, thislen); |
| 2087 | this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2088 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 2089 | if (ONENAND_IS_4KB_PAGE(this)) { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2090 | /* Set main area of DataRAM to 0xff*/ |
| 2091 | memset(this->page_buf, 0xff, mtd->writesize); |
| 2092 | this->write_bufferram(mtd, ONENAND_DATARAM, |
| 2093 | this->page_buf, 0, mtd->writesize); |
| 2094 | } |
| 2095 | |
| 2096 | this->command(mtd, oobcmd, to, mtd->oobsize); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2097 | |
| 2098 | onenand_update_bufferram(mtd, to, 0); |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 2099 | if (ONENAND_IS_2PLANE(this)) { |
| 2100 | ONENAND_SET_BUFFERRAM1(this); |
| 2101 | onenand_update_bufferram(mtd, to + this->writesize, 0); |
| 2102 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2103 | |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 2104 | ret = this->wait(mtd, FL_WRITING); |
| 2105 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2106 | printk(KERN_ERR "%s: write failed %d\n", __func__, ret); |
Kyungmin Park | 5b4246f | 2007-02-02 09:39:21 +0900 | [diff] [blame] | 2107 | break; |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 2108 | } |
| 2109 | |
Kyungmin Park | 91014e9 | 2007-02-12 10:34:39 +0900 | [diff] [blame] | 2110 | ret = onenand_verify_oob(mtd, oobbuf, to); |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 2111 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2112 | printk(KERN_ERR "%s: verify failed %d\n", |
| 2113 | __func__, ret); |
Kyungmin Park | 5b4246f | 2007-02-02 09:39:21 +0900 | [diff] [blame] | 2114 | break; |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 2115 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2116 | |
| 2117 | written += thislen; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2118 | if (written == len) |
| 2119 | break; |
| 2120 | |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2121 | to += mtd->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2122 | buf += thislen; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2123 | column = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2126 | ops->oobretlen = written; |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 2127 | |
Kyungmin Park | 8e6ec69 | 2006-05-12 17:02:41 +0300 | [diff] [blame] | 2128 | return ret; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /** |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2132 | * onenand_write - [MTD Interface] write buffer to FLASH |
| 2133 | * @param mtd MTD device structure |
| 2134 | * @param to offset to write to |
| 2135 | * @param len number of bytes to write |
| 2136 | * @param retlen pointer to variable to store the number of written bytes |
| 2137 | * @param buf the data to write |
| 2138 | * |
| 2139 | * Write with ECC |
| 2140 | */ |
| 2141 | static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2142 | size_t *retlen, const u_char *buf) |
| 2143 | { |
| 2144 | struct mtd_oob_ops ops = { |
| 2145 | .len = len, |
| 2146 | .ooblen = 0, |
| 2147 | .datbuf = (u_char *) buf, |
| 2148 | .oobbuf = NULL, |
| 2149 | }; |
| 2150 | int ret; |
| 2151 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2152 | onenand_get_device(mtd, FL_WRITING); |
| 2153 | ret = onenand_write_ops_nolock(mtd, to, &ops); |
| 2154 | onenand_release_device(mtd); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2155 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2156 | *retlen = ops.retlen; |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2157 | return ret; |
| 2158 | } |
| 2159 | |
| 2160 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2161 | * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 2162 | * @param mtd: MTD device structure |
| 2163 | * @param to: offset to write |
| 2164 | * @param ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2165 | */ |
| 2166 | static int onenand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2167 | struct mtd_oob_ops *ops) |
| 2168 | { |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2169 | int ret; |
| 2170 | |
Kyungmin Park | 4f4fad2 | 2007-02-02 09:22:21 +0900 | [diff] [blame] | 2171 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2172 | case MTD_OPS_PLACE_OOB: |
| 2173 | case MTD_OPS_AUTO_OOB: |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2174 | break; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2175 | case MTD_OPS_RAW: |
Kyungmin Park | 4f4fad2 | 2007-02-02 09:22:21 +0900 | [diff] [blame] | 2176 | /* Not implemented yet */ |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 2177 | default: |
| 2178 | return -EINVAL; |
| 2179 | } |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2180 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2181 | onenand_get_device(mtd, FL_WRITING); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2182 | if (ops->datbuf) |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2183 | ret = onenand_write_ops_nolock(mtd, to, ops); |
| 2184 | else |
| 2185 | ret = onenand_write_oob_nolock(mtd, to, ops); |
| 2186 | onenand_release_device(mtd); |
Kyungmin Park | d15057b | 2007-09-06 10:06:12 +0900 | [diff] [blame] | 2187 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2188 | return ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /** |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2192 | * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2193 | * @param mtd MTD device structure |
| 2194 | * @param ofs offset from device start |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2195 | * @param allowbbt 1, if its allowed to access the bbt area |
| 2196 | * |
| 2197 | * Check, if the block is bad. Either by reading the bad block table or |
| 2198 | * calling of the scan function. |
| 2199 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2200 | static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2201 | { |
| 2202 | struct onenand_chip *this = mtd->priv; |
| 2203 | struct bbm_info *bbm = this->bbm; |
| 2204 | |
| 2205 | /* Return info from the table */ |
| 2206 | return bbm->isbad_bbt(mtd, ofs, allowbbt); |
| 2207 | } |
| 2208 | |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 2209 | |
| 2210 | static int onenand_multiblock_erase_verify(struct mtd_info *mtd, |
| 2211 | struct erase_info *instr) |
| 2212 | { |
| 2213 | struct onenand_chip *this = mtd->priv; |
| 2214 | loff_t addr = instr->addr; |
| 2215 | int len = instr->len; |
| 2216 | unsigned int block_size = (1 << this->erase_shift); |
| 2217 | int ret = 0; |
| 2218 | |
| 2219 | while (len) { |
| 2220 | this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size); |
| 2221 | ret = this->wait(mtd, FL_VERIFYING_ERASE); |
| 2222 | if (ret) { |
| 2223 | printk(KERN_ERR "%s: Failed verify, block %d\n", |
| 2224 | __func__, onenand_block(this, addr)); |
| 2225 | instr->state = MTD_ERASE_FAILED; |
| 2226 | instr->fail_addr = addr; |
| 2227 | return -1; |
| 2228 | } |
| 2229 | len -= block_size; |
| 2230 | addr += block_size; |
| 2231 | } |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
| 2235 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2236 | * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 2237 | * @param mtd MTD device structure |
| 2238 | * @param instr erase instruction |
| 2239 | * @param region erase region |
| 2240 | * |
| 2241 | * Erase one or more blocks up to 64 block at a time |
| 2242 | */ |
| 2243 | static int onenand_multiblock_erase(struct mtd_info *mtd, |
| 2244 | struct erase_info *instr, |
| 2245 | unsigned int block_size) |
| 2246 | { |
| 2247 | struct onenand_chip *this = mtd->priv; |
| 2248 | loff_t addr = instr->addr; |
| 2249 | int len = instr->len; |
| 2250 | int eb_count = 0; |
| 2251 | int ret = 0; |
| 2252 | int bdry_block = 0; |
| 2253 | |
| 2254 | instr->state = MTD_ERASING; |
| 2255 | |
| 2256 | if (ONENAND_IS_DDP(this)) { |
| 2257 | loff_t bdry_addr = this->chipsize >> 1; |
| 2258 | if (addr < bdry_addr && (addr + len) > bdry_addr) |
| 2259 | bdry_block = bdry_addr >> this->erase_shift; |
| 2260 | } |
| 2261 | |
| 2262 | /* Pre-check bbs */ |
| 2263 | while (len) { |
| 2264 | /* Check if we have a bad block, we do not erase bad blocks */ |
| 2265 | if (onenand_block_isbad_nolock(mtd, addr, 0)) { |
| 2266 | printk(KERN_WARNING "%s: attempt to erase a bad block " |
| 2267 | "at addr 0x%012llx\n", |
| 2268 | __func__, (unsigned long long) addr); |
| 2269 | instr->state = MTD_ERASE_FAILED; |
| 2270 | return -EIO; |
| 2271 | } |
| 2272 | len -= block_size; |
| 2273 | addr += block_size; |
| 2274 | } |
| 2275 | |
| 2276 | len = instr->len; |
| 2277 | addr = instr->addr; |
| 2278 | |
| 2279 | /* loop over 64 eb batches */ |
| 2280 | while (len) { |
| 2281 | struct erase_info verify_instr = *instr; |
| 2282 | int max_eb_count = MB_ERASE_MAX_BLK_COUNT; |
| 2283 | |
| 2284 | verify_instr.addr = addr; |
| 2285 | verify_instr.len = 0; |
| 2286 | |
| 2287 | /* do not cross chip boundary */ |
| 2288 | if (bdry_block) { |
| 2289 | int this_block = (addr >> this->erase_shift); |
| 2290 | |
| 2291 | if (this_block < bdry_block) { |
| 2292 | max_eb_count = min(max_eb_count, |
| 2293 | (bdry_block - this_block)); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | eb_count = 0; |
| 2298 | |
| 2299 | while (len > block_size && eb_count < (max_eb_count - 1)) { |
| 2300 | this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE, |
| 2301 | addr, block_size); |
| 2302 | onenand_invalidate_bufferram(mtd, addr, block_size); |
| 2303 | |
| 2304 | ret = this->wait(mtd, FL_PREPARING_ERASE); |
| 2305 | if (ret) { |
| 2306 | printk(KERN_ERR "%s: Failed multiblock erase, " |
| 2307 | "block %d\n", __func__, |
| 2308 | onenand_block(this, addr)); |
| 2309 | instr->state = MTD_ERASE_FAILED; |
| 2310 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
| 2311 | return -EIO; |
| 2312 | } |
| 2313 | |
| 2314 | len -= block_size; |
| 2315 | addr += block_size; |
| 2316 | eb_count++; |
| 2317 | } |
| 2318 | |
| 2319 | /* last block of 64-eb series */ |
| 2320 | cond_resched(); |
| 2321 | this->command(mtd, ONENAND_CMD_ERASE, addr, block_size); |
| 2322 | onenand_invalidate_bufferram(mtd, addr, block_size); |
| 2323 | |
| 2324 | ret = this->wait(mtd, FL_ERASING); |
| 2325 | /* Check if it is write protected */ |
| 2326 | if (ret) { |
| 2327 | printk(KERN_ERR "%s: Failed erase, block %d\n", |
| 2328 | __func__, onenand_block(this, addr)); |
| 2329 | instr->state = MTD_ERASE_FAILED; |
| 2330 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
| 2331 | return -EIO; |
| 2332 | } |
| 2333 | |
| 2334 | len -= block_size; |
| 2335 | addr += block_size; |
| 2336 | eb_count++; |
| 2337 | |
| 2338 | /* verify */ |
| 2339 | verify_instr.len = eb_count * block_size; |
| 2340 | if (onenand_multiblock_erase_verify(mtd, &verify_instr)) { |
| 2341 | instr->state = verify_instr.state; |
| 2342 | instr->fail_addr = verify_instr.fail_addr; |
| 2343 | return -EIO; |
| 2344 | } |
| 2345 | |
| 2346 | } |
| 2347 | return 0; |
| 2348 | } |
| 2349 | |
| 2350 | |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2351 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2352 | * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2353 | * @param mtd MTD device structure |
| 2354 | * @param instr erase instruction |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2355 | * @param region erase region |
| 2356 | * @param block_size erase block size |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2357 | * |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2358 | * Erase one or more blocks one block at a time |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2359 | */ |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2360 | static int onenand_block_by_block_erase(struct mtd_info *mtd, |
| 2361 | struct erase_info *instr, |
| 2362 | struct mtd_erase_region_info *region, |
| 2363 | unsigned int block_size) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2364 | { |
| 2365 | struct onenand_chip *this = mtd->priv; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2366 | loff_t addr = instr->addr; |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2367 | int len = instr->len; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2368 | loff_t region_end = 0; |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2369 | int ret = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2370 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2371 | if (region) { |
| 2372 | /* region is set for Flex-OneNAND */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2373 | region_end = region->offset + region->erasesize * region->numblocks; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2376 | instr->state = MTD_ERASING; |
| 2377 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2378 | /* Loop through the blocks */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2379 | while (len) { |
Artem Bityutskiy | 61a7e19 | 2006-12-26 16:41:24 +0900 | [diff] [blame] | 2380 | cond_resched(); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2381 | |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2382 | /* Check if we have a bad block, we do not erase bad blocks */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2383 | if (onenand_block_isbad_nolock(mtd, addr, 0)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2384 | printk(KERN_WARNING "%s: attempt to erase a bad block " |
| 2385 | "at addr 0x%012llx\n", |
| 2386 | __func__, (unsigned long long) addr); |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2387 | instr->state = MTD_ERASE_FAILED; |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2388 | return -EIO; |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2389 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2390 | |
| 2391 | this->command(mtd, ONENAND_CMD_ERASE, addr, block_size); |
| 2392 | |
Adrian Hunter | 480b9df | 2007-02-07 13:55:19 +0200 | [diff] [blame] | 2393 | onenand_invalidate_bufferram(mtd, addr, block_size); |
| 2394 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2395 | ret = this->wait(mtd, FL_ERASING); |
| 2396 | /* Check, if it is write protected */ |
| 2397 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2398 | printk(KERN_ERR "%s: Failed erase, block %d\n", |
| 2399 | __func__, onenand_block(this, addr)); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2400 | instr->state = MTD_ERASE_FAILED; |
| 2401 | instr->fail_addr = addr; |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2402 | return -EIO; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | len -= block_size; |
| 2406 | addr += block_size; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2407 | |
john.maxin@nokia.com | eff3bba | 2011-05-06 09:17:21 +0000 | [diff] [blame] | 2408 | if (region && addr == region_end) { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2409 | if (!len) |
| 2410 | break; |
| 2411 | region++; |
| 2412 | |
| 2413 | block_size = region->erasesize; |
| 2414 | region_end = region->offset + region->erasesize * region->numblocks; |
| 2415 | |
| 2416 | if (len & (block_size - 1)) { |
| 2417 | /* FIXME: This should be handled at MTD partitioning level. */ |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2418 | printk(KERN_ERR "%s: Unaligned address\n", |
| 2419 | __func__); |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2420 | return -EIO; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2421 | } |
| 2422 | } |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2423 | } |
| 2424 | return 0; |
| 2425 | } |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2426 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2427 | /** |
| 2428 | * onenand_erase - [MTD Interface] erase block(s) |
| 2429 | * @param mtd MTD device structure |
| 2430 | * @param instr erase instruction |
| 2431 | * |
| 2432 | * Erase one or more blocks |
| 2433 | */ |
| 2434 | static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 2435 | { |
| 2436 | struct onenand_chip *this = mtd->priv; |
| 2437 | unsigned int block_size; |
| 2438 | loff_t addr = instr->addr; |
| 2439 | loff_t len = instr->len; |
| 2440 | int ret = 0; |
| 2441 | struct mtd_erase_region_info *region = NULL; |
| 2442 | loff_t region_offset = 0; |
| 2443 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2444 | pr_debug("%s: start=0x%012llx, len=%llu\n", __func__, |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 2445 | (unsigned long long)instr->addr, |
| 2446 | (unsigned long long)instr->len); |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2447 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2448 | if (FLEXONENAND(this)) { |
| 2449 | /* Find the eraseregion of this address */ |
| 2450 | int i = flexonenand_region(mtd, addr); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2451 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2452 | region = &mtd->eraseregions[i]; |
| 2453 | block_size = region->erasesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2454 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2455 | /* Start address within region must align on block boundary. |
| 2456 | * Erase region's start offset is always block start address. |
| 2457 | */ |
| 2458 | region_offset = region->offset; |
| 2459 | } else |
| 2460 | block_size = 1 << this->erase_shift; |
| 2461 | |
| 2462 | /* Start address must align on block boundary */ |
| 2463 | if (unlikely((addr - region_offset) & (block_size - 1))) { |
| 2464 | printk(KERN_ERR "%s: Unaligned address\n", __func__); |
| 2465 | return -EINVAL; |
| 2466 | } |
| 2467 | |
| 2468 | /* Length must align on block boundary */ |
| 2469 | if (unlikely(len & (block_size - 1))) { |
| 2470 | printk(KERN_ERR "%s: Length not block aligned\n", __func__); |
| 2471 | return -EINVAL; |
| 2472 | } |
| 2473 | |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2474 | /* Grab the lock and see if the device is available */ |
| 2475 | onenand_get_device(mtd, FL_ERASING); |
| 2476 | |
Kyungmin Park | d983c54 | 2010-12-06 09:05:18 +0900 | [diff] [blame] | 2477 | if (ONENAND_IS_4KB_PAGE(this) || region || |
| 2478 | instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) { |
Mika Korhonen | 7207302 | 2009-10-23 07:50:43 +0200 | [diff] [blame] | 2479 | /* region is set for Flex-OneNAND (no mb erase) */ |
| 2480 | ret = onenand_block_by_block_erase(mtd, instr, |
| 2481 | region, block_size); |
| 2482 | } else { |
| 2483 | ret = onenand_multiblock_erase(mtd, instr, block_size); |
| 2484 | } |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2485 | |
| 2486 | /* Deselect and wake up anyone waiting on the device */ |
| 2487 | onenand_release_device(mtd); |
| 2488 | |
Adrian Hunter | 3cd3a86 | 2007-10-12 10:34:01 +0300 | [diff] [blame] | 2489 | /* Do call back function */ |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2490 | if (!ret) { |
| 2491 | instr->state = MTD_ERASE_DONE; |
Adrian Hunter | 3cd3a86 | 2007-10-12 10:34:01 +0300 | [diff] [blame] | 2492 | mtd_erase_callback(instr); |
Mika Korhonen | 73885ae | 2009-10-23 07:50:42 +0200 | [diff] [blame] | 2493 | } |
Adrian Hunter | 3cd3a86 | 2007-10-12 10:34:01 +0300 | [diff] [blame] | 2494 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | /** |
| 2499 | * onenand_sync - [MTD Interface] sync |
| 2500 | * @param mtd MTD device structure |
| 2501 | * |
| 2502 | * Sync is actually a wait for chip ready function |
| 2503 | */ |
| 2504 | static void onenand_sync(struct mtd_info *mtd) |
| 2505 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2506 | pr_debug("%s: called\n", __func__); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2507 | |
| 2508 | /* Grab the lock and see if the device is available */ |
| 2509 | onenand_get_device(mtd, FL_SYNCING); |
| 2510 | |
| 2511 | /* Release it and go back */ |
| 2512 | onenand_release_device(mtd); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad |
| 2517 | * @param mtd MTD device structure |
| 2518 | * @param ofs offset relative to mtd start |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2519 | * |
| 2520 | * Check whether the block is bad |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2521 | */ |
| 2522 | static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 2523 | { |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2524 | int ret; |
| 2525 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2526 | onenand_get_device(mtd, FL_READING); |
| 2527 | ret = onenand_block_isbad_nolock(mtd, ofs, 0); |
| 2528 | onenand_release_device(mtd); |
| 2529 | return ret; |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * onenand_default_block_markbad - [DEFAULT] mark a block bad |
| 2534 | * @param mtd MTD device structure |
| 2535 | * @param ofs offset from device start |
| 2536 | * |
| 2537 | * This is the default implementation, which can be overridden by |
| 2538 | * a hardware specific driver. |
| 2539 | */ |
| 2540 | static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 2541 | { |
| 2542 | struct onenand_chip *this = mtd->priv; |
| 2543 | struct bbm_info *bbm = this->bbm; |
| 2544 | u_char buf[2] = {0, 0}; |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2545 | struct mtd_oob_ops ops = { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2546 | .mode = MTD_OPS_PLACE_OOB, |
Kyungmin Park | 12f77c9 | 2007-08-30 09:36:05 +0900 | [diff] [blame] | 2547 | .ooblen = 2, |
| 2548 | .oobbuf = buf, |
| 2549 | .ooboffs = 0, |
| 2550 | }; |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2551 | int block; |
| 2552 | |
| 2553 | /* Get block number */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2554 | block = onenand_block(this, ofs); |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2555 | if (bbm->bbt) |
| 2556 | bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
| 2557 | |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 2558 | /* We write two bytes, so we don't have to mess with 16-bit access */ |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2559 | ofs += mtd->oobsize + (bbm->badblockpos & ~0x01); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2560 | /* FIXME : What to do when marking SLC block in partition |
| 2561 | * with MLC erasesize? For now, it is not advisable to |
| 2562 | * create partitions containing both SLC and MLC regions. |
| 2563 | */ |
| 2564 | return onenand_write_oob_nolock(mtd, ofs, &ops); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | /** |
| 2568 | * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad |
| 2569 | * @param mtd MTD device structure |
| 2570 | * @param ofs offset relative to mtd start |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2571 | * |
| 2572 | * Mark the block as bad |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2573 | */ |
| 2574 | static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 2575 | { |
Aaro Koskinen | 5e64c29 | 2016-02-20 22:27:48 +0200 | [diff] [blame] | 2576 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 2577 | int ret; |
| 2578 | |
| 2579 | ret = onenand_block_isbad(mtd, ofs); |
| 2580 | if (ret) { |
| 2581 | /* If it was bad already, return success and do nothing */ |
| 2582 | if (ret > 0) |
| 2583 | return 0; |
| 2584 | return ret; |
| 2585 | } |
| 2586 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2587 | onenand_get_device(mtd, FL_WRITING); |
Aaro Koskinen | 5e64c29 | 2016-02-20 22:27:48 +0200 | [diff] [blame] | 2588 | ret = this->block_markbad(mtd, ofs); |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 2589 | onenand_release_device(mtd); |
| 2590 | return ret; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | /** |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2594 | * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2595 | * @param mtd MTD device structure |
| 2596 | * @param ofs offset relative to mtd start |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2597 | * @param len number of bytes to lock or unlock |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 2598 | * @param cmd lock or unlock command |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2599 | * |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2600 | * Lock or unlock one or more blocks |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2601 | */ |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2602 | static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2603 | { |
| 2604 | struct onenand_chip *this = mtd->priv; |
| 2605 | int start, end, block, value, status; |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2606 | int wp_status_mask; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2607 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2608 | start = onenand_block(this, ofs); |
| 2609 | end = onenand_block(this, ofs + len) - 1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2610 | |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2611 | if (cmd == ONENAND_CMD_LOCK) |
| 2612 | wp_status_mask = ONENAND_WP_LS; |
| 2613 | else |
| 2614 | wp_status_mask = ONENAND_WP_US; |
| 2615 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2616 | /* Continuous lock scheme */ |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2617 | if (this->options & ONENAND_HAS_CONT_LOCK) { |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2618 | /* Set start block address */ |
| 2619 | this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 2620 | /* Set end block address */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2621 | this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS); |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2622 | /* Write lock command */ |
| 2623 | this->command(mtd, cmd, 0, 0); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2624 | |
| 2625 | /* There's no return value */ |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2626 | this->wait(mtd, FL_LOCKING); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2627 | |
| 2628 | /* Sanity check */ |
| 2629 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 2630 | & ONENAND_CTRL_ONGO) |
| 2631 | continue; |
| 2632 | |
| 2633 | /* Check lock status */ |
| 2634 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2635 | if (!(status & wp_status_mask)) |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2636 | printk(KERN_ERR "%s: wp status = 0x%x\n", |
| 2637 | __func__, status); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2638 | |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | /* Block lock scheme */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2643 | for (block = start; block < end + 1; block++) { |
Kyungmin Park | 20ba89a | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 2644 | /* Set block address */ |
| 2645 | value = onenand_block_address(this, block); |
| 2646 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1); |
| 2647 | /* Select DataRAM for DDP */ |
| 2648 | value = onenand_bufferram_address(this, block); |
| 2649 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2650 | /* Set start block address */ |
| 2651 | this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2652 | /* Write lock command */ |
| 2653 | this->command(mtd, cmd, 0, 0); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2654 | |
| 2655 | /* There's no return value */ |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2656 | this->wait(mtd, FL_LOCKING); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2657 | |
| 2658 | /* Sanity check */ |
| 2659 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 2660 | & ONENAND_CTRL_ONGO) |
| 2661 | continue; |
| 2662 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2663 | /* Check lock status */ |
| 2664 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2665 | if (!(status & wp_status_mask)) |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2666 | printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n", |
| 2667 | __func__, block, status); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2668 | } |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 2669 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 2670 | return 0; |
| 2671 | } |
| 2672 | |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2673 | /** |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2674 | * onenand_lock - [MTD Interface] Lock block(s) |
| 2675 | * @param mtd MTD device structure |
| 2676 | * @param ofs offset relative to mtd start |
| 2677 | * @param len number of bytes to unlock |
| 2678 | * |
| 2679 | * Lock one or more blocks |
| 2680 | */ |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2681 | static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2682 | { |
Adrian Hunter | 34627f0 | 2007-10-12 10:19:26 +0300 | [diff] [blame] | 2683 | int ret; |
| 2684 | |
| 2685 | onenand_get_device(mtd, FL_LOCKING); |
| 2686 | ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK); |
| 2687 | onenand_release_device(mtd); |
| 2688 | return ret; |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2689 | } |
| 2690 | |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2691 | /** |
| 2692 | * onenand_unlock - [MTD Interface] Unlock block(s) |
| 2693 | * @param mtd MTD device structure |
| 2694 | * @param ofs offset relative to mtd start |
| 2695 | * @param len number of bytes to unlock |
| 2696 | * |
| 2697 | * Unlock one or more blocks |
| 2698 | */ |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2699 | static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2700 | { |
Adrian Hunter | 34627f0 | 2007-10-12 10:19:26 +0300 | [diff] [blame] | 2701 | int ret; |
| 2702 | |
| 2703 | onenand_get_device(mtd, FL_LOCKING); |
| 2704 | ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK); |
| 2705 | onenand_release_device(mtd); |
| 2706 | return ret; |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | /** |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2710 | * onenand_check_lock_status - [OneNAND Interface] Check lock status |
| 2711 | * @param this onenand chip data structure |
| 2712 | * |
| 2713 | * Check lock status |
| 2714 | */ |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2715 | static int onenand_check_lock_status(struct onenand_chip *this) |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2716 | { |
| 2717 | unsigned int value, block, status; |
| 2718 | unsigned int end; |
| 2719 | |
| 2720 | end = this->chipsize >> this->erase_shift; |
| 2721 | for (block = 0; block < end; block++) { |
| 2722 | /* Set block address */ |
| 2723 | value = onenand_block_address(this, block); |
| 2724 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1); |
| 2725 | /* Select DataRAM for DDP */ |
| 2726 | value = onenand_bufferram_address(this, block); |
| 2727 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 2728 | /* Set start block address */ |
| 2729 | this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 2730 | |
| 2731 | /* Check lock status */ |
| 2732 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2733 | if (!(status & ONENAND_WP_US)) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 2734 | printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n", |
| 2735 | __func__, block, status); |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2736 | return 0; |
| 2737 | } |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2738 | } |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2739 | |
| 2740 | return 1; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * onenand_unlock_all - [OneNAND Interface] unlock all blocks |
| 2745 | * @param mtd MTD device structure |
| 2746 | * |
| 2747 | * Unlock all blocks |
| 2748 | */ |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2749 | static void onenand_unlock_all(struct mtd_info *mtd) |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2750 | { |
| 2751 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2752 | loff_t ofs = 0; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2753 | loff_t len = mtd->size; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2754 | |
| 2755 | if (this->options & ONENAND_HAS_UNLOCK_ALL) { |
Kyungmin Park | 10b7a2b | 2007-01-12 05:45:34 +0900 | [diff] [blame] | 2756 | /* Set start block address */ |
| 2757 | this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2758 | /* Write unlock command */ |
| 2759 | this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0); |
| 2760 | |
| 2761 | /* There's no return value */ |
Kyungmin Park | 08f782b | 2006-11-16 11:29:39 +0900 | [diff] [blame] | 2762 | this->wait(mtd, FL_LOCKING); |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2763 | |
| 2764 | /* Sanity check */ |
| 2765 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 2766 | & ONENAND_CTRL_ONGO) |
| 2767 | continue; |
| 2768 | |
Kyungmin Park | 31bb999 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2769 | /* Don't check lock status */ |
| 2770 | if (this->options & ONENAND_SKIP_UNLOCK_CHECK) |
| 2771 | return; |
| 2772 | |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2773 | /* Check lock status */ |
| 2774 | if (onenand_check_lock_status(this)) |
| 2775 | return; |
| 2776 | |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2777 | /* Workaround for all block unlock in DDP */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 2778 | if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) { |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2779 | /* All blocks on another chip */ |
| 2780 | ofs = this->chipsize >> 1; |
| 2781 | len = this->chipsize >> 1; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2782 | } |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Kyungmin Park | 66a1050 | 2008-02-13 15:55:38 +0900 | [diff] [blame] | 2785 | onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK); |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 2788 | #ifdef CONFIG_MTD_ONENAND_OTP |
| 2789 | |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 2790 | /** |
| 2791 | * onenand_otp_command - Send OTP specific command to OneNAND device |
| 2792 | * @param mtd MTD device structure |
| 2793 | * @param cmd the command to be sent |
| 2794 | * @param addr offset to read from or write to |
| 2795 | * @param len number of bytes to read or write |
| 2796 | */ |
| 2797 | static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr, |
| 2798 | size_t len) |
| 2799 | { |
| 2800 | struct onenand_chip *this = mtd->priv; |
| 2801 | int value, block, page; |
| 2802 | |
| 2803 | /* Address translation */ |
| 2804 | switch (cmd) { |
| 2805 | case ONENAND_CMD_OTP_ACCESS: |
| 2806 | block = (int) (addr >> this->erase_shift); |
| 2807 | page = -1; |
| 2808 | break; |
| 2809 | |
| 2810 | default: |
| 2811 | block = (int) (addr >> this->erase_shift); |
| 2812 | page = (int) (addr >> this->page_shift); |
| 2813 | |
| 2814 | if (ONENAND_IS_2PLANE(this)) { |
| 2815 | /* Make the even block number */ |
| 2816 | block &= ~1; |
| 2817 | /* Is it the odd plane? */ |
| 2818 | if (addr & this->writesize) |
| 2819 | block++; |
| 2820 | page >>= 1; |
| 2821 | } |
| 2822 | page &= this->page_mask; |
| 2823 | break; |
| 2824 | } |
| 2825 | |
| 2826 | if (block != -1) { |
| 2827 | /* Write 'DFS, FBA' of Flash */ |
| 2828 | value = onenand_block_address(this, block); |
| 2829 | this->write_word(value, this->base + |
| 2830 | ONENAND_REG_START_ADDRESS1); |
| 2831 | } |
| 2832 | |
| 2833 | if (page != -1) { |
| 2834 | /* Now we use page size operation */ |
| 2835 | int sectors = 4, count = 4; |
| 2836 | int dataram; |
| 2837 | |
| 2838 | switch (cmd) { |
| 2839 | default: |
| 2840 | if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG) |
| 2841 | cmd = ONENAND_CMD_2X_PROG; |
| 2842 | dataram = ONENAND_CURRENT_BUFFERRAM(this); |
| 2843 | break; |
| 2844 | } |
| 2845 | |
| 2846 | /* Write 'FPA, FSA' of Flash */ |
| 2847 | value = onenand_page_address(page, sectors); |
| 2848 | this->write_word(value, this->base + |
| 2849 | ONENAND_REG_START_ADDRESS8); |
| 2850 | |
| 2851 | /* Write 'BSA, BSC' of DataRAM */ |
| 2852 | value = onenand_buffer_address(dataram, sectors, count); |
| 2853 | this->write_word(value, this->base + ONENAND_REG_START_BUFFER); |
| 2854 | } |
| 2855 | |
| 2856 | /* Interrupt clear */ |
| 2857 | this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT); |
| 2858 | |
| 2859 | /* Write command */ |
| 2860 | this->write_word(cmd, this->base + ONENAND_REG_COMMAND); |
| 2861 | |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2866 | * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 2867 | * @param mtd MTD device structure |
| 2868 | * @param to offset to write to |
| 2869 | * @param len number of bytes to write |
| 2870 | * @param retlen pointer to variable to store the number of written bytes |
| 2871 | * @param buf the data to write |
| 2872 | * |
| 2873 | * OneNAND write out-of-band only for OTP |
| 2874 | */ |
| 2875 | static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to, |
| 2876 | struct mtd_oob_ops *ops) |
| 2877 | { |
| 2878 | struct onenand_chip *this = mtd->priv; |
| 2879 | int column, ret = 0, oobsize; |
| 2880 | int written = 0; |
| 2881 | u_char *oobbuf; |
| 2882 | size_t len = ops->ooblen; |
| 2883 | const u_char *buf = ops->oobbuf; |
| 2884 | int block, value, status; |
| 2885 | |
| 2886 | to += ops->ooboffs; |
| 2887 | |
| 2888 | /* Initialize retlen, in case of early exit */ |
| 2889 | ops->oobretlen = 0; |
| 2890 | |
| 2891 | oobsize = mtd->oobsize; |
| 2892 | |
| 2893 | column = to & (mtd->oobsize - 1); |
| 2894 | |
| 2895 | oobbuf = this->oob_buf; |
| 2896 | |
| 2897 | /* Loop until all data write */ |
| 2898 | while (written < len) { |
| 2899 | int thislen = min_t(int, oobsize, len - written); |
| 2900 | |
| 2901 | cond_resched(); |
| 2902 | |
| 2903 | block = (int) (to >> this->erase_shift); |
| 2904 | /* |
| 2905 | * Write 'DFS, FBA' of Flash |
| 2906 | * Add: F100h DQ=DFS, FBA |
| 2907 | */ |
| 2908 | |
| 2909 | value = onenand_block_address(this, block); |
| 2910 | this->write_word(value, this->base + |
| 2911 | ONENAND_REG_START_ADDRESS1); |
| 2912 | |
| 2913 | /* |
| 2914 | * Select DataRAM for DDP |
| 2915 | * Add: F101h DQ=DBS |
| 2916 | */ |
| 2917 | |
| 2918 | value = onenand_bufferram_address(this, block); |
| 2919 | this->write_word(value, this->base + |
| 2920 | ONENAND_REG_START_ADDRESS2); |
| 2921 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 2922 | |
| 2923 | /* |
| 2924 | * Enter OTP access mode |
| 2925 | */ |
| 2926 | this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0); |
| 2927 | this->wait(mtd, FL_OTPING); |
| 2928 | |
| 2929 | /* We send data to spare ram with oobsize |
| 2930 | * to prevent byte access */ |
| 2931 | memcpy(oobbuf + column, buf, thislen); |
| 2932 | |
| 2933 | /* |
| 2934 | * Write Data into DataRAM |
| 2935 | * Add: 8th Word |
| 2936 | * in sector0/spare/page0 |
| 2937 | * DQ=XXFCh |
| 2938 | */ |
| 2939 | this->write_bufferram(mtd, ONENAND_SPARERAM, |
| 2940 | oobbuf, 0, mtd->oobsize); |
| 2941 | |
| 2942 | onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize); |
| 2943 | onenand_update_bufferram(mtd, to, 0); |
| 2944 | if (ONENAND_IS_2PLANE(this)) { |
| 2945 | ONENAND_SET_BUFFERRAM1(this); |
| 2946 | onenand_update_bufferram(mtd, to + this->writesize, 0); |
| 2947 | } |
| 2948 | |
| 2949 | ret = this->wait(mtd, FL_WRITING); |
| 2950 | if (ret) { |
| 2951 | printk(KERN_ERR "%s: write failed %d\n", __func__, ret); |
| 2952 | break; |
| 2953 | } |
| 2954 | |
| 2955 | /* Exit OTP access mode */ |
| 2956 | this->command(mtd, ONENAND_CMD_RESET, 0, 0); |
| 2957 | this->wait(mtd, FL_RESETING); |
| 2958 | |
| 2959 | status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS); |
| 2960 | status &= 0x60; |
| 2961 | |
| 2962 | if (status == 0x60) { |
| 2963 | printk(KERN_DEBUG "\nBLOCK\tSTATUS\n"); |
| 2964 | printk(KERN_DEBUG "1st Block\tLOCKED\n"); |
| 2965 | printk(KERN_DEBUG "OTP Block\tLOCKED\n"); |
| 2966 | } else if (status == 0x20) { |
| 2967 | printk(KERN_DEBUG "\nBLOCK\tSTATUS\n"); |
| 2968 | printk(KERN_DEBUG "1st Block\tLOCKED\n"); |
| 2969 | printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n"); |
| 2970 | } else if (status == 0x40) { |
| 2971 | printk(KERN_DEBUG "\nBLOCK\tSTATUS\n"); |
| 2972 | printk(KERN_DEBUG "1st Block\tUN-LOCKED\n"); |
| 2973 | printk(KERN_DEBUG "OTP Block\tLOCKED\n"); |
| 2974 | } else { |
| 2975 | printk(KERN_DEBUG "Reboot to check\n"); |
| 2976 | } |
| 2977 | |
| 2978 | written += thislen; |
| 2979 | if (written == len) |
| 2980 | break; |
| 2981 | |
| 2982 | to += mtd->writesize; |
| 2983 | buf += thislen; |
| 2984 | column = 0; |
| 2985 | } |
| 2986 | |
| 2987 | ops->oobretlen = written; |
| 2988 | |
| 2989 | return ret; |
| 2990 | } |
| 2991 | |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 2992 | /* Internal OTP operation */ |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 2993 | typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len, |
| 2994 | size_t *retlen, u_char *buf); |
| 2995 | |
| 2996 | /** |
| 2997 | * do_otp_read - [DEFAULT] Read OTP block area |
| 2998 | * @param mtd MTD device structure |
| 2999 | * @param from The offset to read |
| 3000 | * @param len number of bytes to read |
| 3001 | * @param retlen pointer to variable to store the number of readbytes |
| 3002 | * @param buf the databuffer to put/get data |
| 3003 | * |
| 3004 | * Read OTP block area. |
| 3005 | */ |
| 3006 | static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 3007 | size_t *retlen, u_char *buf) |
| 3008 | { |
| 3009 | struct onenand_chip *this = mtd->priv; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3010 | struct mtd_oob_ops ops = { |
| 3011 | .len = len, |
| 3012 | .ooblen = 0, |
| 3013 | .datbuf = buf, |
| 3014 | .oobbuf = NULL, |
| 3015 | }; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3016 | int ret; |
| 3017 | |
| 3018 | /* Enter OTP access mode */ |
| 3019 | this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0); |
| 3020 | this->wait(mtd, FL_OTPING); |
| 3021 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 3022 | ret = ONENAND_IS_4KB_PAGE(this) ? |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3023 | onenand_mlc_read_ops_nolock(mtd, from, &ops) : |
| 3024 | onenand_read_ops_nolock(mtd, from, &ops); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3025 | |
| 3026 | /* Exit OTP access mode */ |
| 3027 | this->command(mtd, ONENAND_CMD_RESET, 0, 0); |
| 3028 | this->wait(mtd, FL_RESETING); |
| 3029 | |
| 3030 | return ret; |
| 3031 | } |
| 3032 | |
| 3033 | /** |
| 3034 | * do_otp_write - [DEFAULT] Write OTP block area |
| 3035 | * @param mtd MTD device structure |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3036 | * @param to The offset to write |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3037 | * @param len number of bytes to write |
| 3038 | * @param retlen pointer to variable to store the number of write bytes |
| 3039 | * @param buf the databuffer to put/get data |
| 3040 | * |
| 3041 | * Write OTP block area. |
| 3042 | */ |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3043 | static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len, |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3044 | size_t *retlen, u_char *buf) |
| 3045 | { |
| 3046 | struct onenand_chip *this = mtd->priv; |
| 3047 | unsigned char *pbuf = buf; |
| 3048 | int ret; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3049 | struct mtd_oob_ops ops; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3050 | |
| 3051 | /* Force buffer page aligned */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3052 | if (len < mtd->writesize) { |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3053 | memcpy(this->page_buf, buf, len); |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3054 | memset(this->page_buf + len, 0xff, mtd->writesize - len); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3055 | pbuf = this->page_buf; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3056 | len = mtd->writesize; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /* Enter OTP access mode */ |
| 3060 | this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0); |
| 3061 | this->wait(mtd, FL_OTPING); |
| 3062 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3063 | ops.len = len; |
| 3064 | ops.ooblen = 0; |
Kyungmin Park | 1437085 | 2007-10-10 13:48:14 +0900 | [diff] [blame] | 3065 | ops.datbuf = pbuf; |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3066 | ops.oobbuf = NULL; |
| 3067 | ret = onenand_write_ops_nolock(mtd, to, &ops); |
| 3068 | *retlen = ops.retlen; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3069 | |
| 3070 | /* Exit OTP access mode */ |
| 3071 | this->command(mtd, ONENAND_CMD_RESET, 0, 0); |
| 3072 | this->wait(mtd, FL_RESETING); |
| 3073 | |
| 3074 | return ret; |
| 3075 | } |
| 3076 | |
| 3077 | /** |
| 3078 | * do_otp_lock - [DEFAULT] Lock OTP block area |
| 3079 | * @param mtd MTD device structure |
| 3080 | * @param from The offset to lock |
| 3081 | * @param len number of bytes to lock |
| 3082 | * @param retlen pointer to variable to store the number of lock bytes |
| 3083 | * @param buf the databuffer to put/get data |
| 3084 | * |
| 3085 | * Lock OTP block area. |
| 3086 | */ |
| 3087 | static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len, |
| 3088 | size_t *retlen, u_char *buf) |
| 3089 | { |
| 3090 | struct onenand_chip *this = mtd->priv; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3091 | struct mtd_oob_ops ops; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3092 | int ret; |
| 3093 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3094 | if (FLEXONENAND(this)) { |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3095 | |
| 3096 | /* Enter OTP access mode */ |
| 3097 | this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0); |
| 3098 | this->wait(mtd, FL_OTPING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3099 | /* |
| 3100 | * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of |
| 3101 | * main area of page 49. |
| 3102 | */ |
| 3103 | ops.len = mtd->writesize; |
| 3104 | ops.ooblen = 0; |
| 3105 | ops.datbuf = buf; |
| 3106 | ops.oobbuf = NULL; |
| 3107 | ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops); |
| 3108 | *retlen = ops.retlen; |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3109 | |
| 3110 | /* Exit OTP access mode */ |
| 3111 | this->command(mtd, ONENAND_CMD_RESET, 0, 0); |
| 3112 | this->wait(mtd, FL_RESETING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3113 | } else { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3114 | ops.mode = MTD_OPS_PLACE_OOB; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3115 | ops.ooblen = len; |
| 3116 | ops.oobbuf = buf; |
| 3117 | ops.ooboffs = 0; |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3118 | ret = onenand_otp_write_oob_nolock(mtd, from, &ops); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3119 | *retlen = ops.oobretlen; |
| 3120 | } |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3121 | |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3122 | return ret; |
| 3123 | } |
| 3124 | |
| 3125 | /** |
| 3126 | * onenand_otp_walk - [DEFAULT] Handle OTP operation |
| 3127 | * @param mtd MTD device structure |
| 3128 | * @param from The offset to read/write |
| 3129 | * @param len number of bytes to read/write |
| 3130 | * @param retlen pointer to variable to store the number of read bytes |
| 3131 | * @param buf the databuffer to put/get data |
| 3132 | * @param action do given action |
| 3133 | * @param mode specify user and factory |
| 3134 | * |
| 3135 | * Handle OTP operation. |
| 3136 | */ |
| 3137 | static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len, |
| 3138 | size_t *retlen, u_char *buf, |
| 3139 | otp_op_t action, int mode) |
| 3140 | { |
| 3141 | struct onenand_chip *this = mtd->priv; |
| 3142 | int otp_pages; |
| 3143 | int density; |
| 3144 | int ret = 0; |
| 3145 | |
| 3146 | *retlen = 0; |
| 3147 | |
Kyungmin Park | e71f04f | 2007-12-11 11:23:45 +0900 | [diff] [blame] | 3148 | density = onenand_get_density(this->device_id); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3149 | if (density < ONENAND_DEVICE_DENSITY_512Mb) |
| 3150 | otp_pages = 20; |
| 3151 | else |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3152 | otp_pages = 50; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3153 | |
| 3154 | if (mode == MTD_OTP_FACTORY) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3155 | from += mtd->writesize * otp_pages; |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3156 | otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* Check User/Factory boundary */ |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3160 | if (mode == MTD_OTP_USER) { |
Roel Kluin | 0a032a4 | 2009-12-16 01:37:17 +0100 | [diff] [blame] | 3161 | if (mtd->writesize * otp_pages < from + len) |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3162 | return 0; |
| 3163 | } else { |
Roel Kluin | 0a032a4 | 2009-12-16 01:37:17 +0100 | [diff] [blame] | 3164 | if (mtd->writesize * otp_pages < len) |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3165 | return 0; |
| 3166 | } |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3167 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3168 | onenand_get_device(mtd, FL_OTPING); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3169 | while (len > 0 && otp_pages > 0) { |
| 3170 | if (!action) { /* OTP Info functions */ |
| 3171 | struct otp_info *otpinfo; |
| 3172 | |
| 3173 | len -= sizeof(struct otp_info); |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3174 | if (len <= 0) { |
| 3175 | ret = -ENOSPC; |
| 3176 | break; |
| 3177 | } |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3178 | |
| 3179 | otpinfo = (struct otp_info *) buf; |
| 3180 | otpinfo->start = from; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3181 | otpinfo->length = mtd->writesize; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3182 | otpinfo->locked = 0; |
| 3183 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3184 | from += mtd->writesize; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3185 | buf += sizeof(struct otp_info); |
| 3186 | *retlen += sizeof(struct otp_info); |
| 3187 | } else { |
| 3188 | size_t tmp_retlen; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3189 | |
| 3190 | ret = action(mtd, from, len, &tmp_retlen, buf); |
Dan Carpenter | c3cb77f | 2016-04-13 09:42:34 +0300 | [diff] [blame] | 3191 | if (ret) |
| 3192 | break; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3193 | |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3194 | buf += tmp_retlen; |
| 3195 | len -= tmp_retlen; |
| 3196 | *retlen += tmp_retlen; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3197 | |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3198 | } |
| 3199 | otp_pages--; |
| 3200 | } |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3201 | onenand_release_device(mtd); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3202 | |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3203 | return ret; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /** |
| 3207 | * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info |
| 3208 | * @param mtd MTD device structure |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3209 | * @param len number of bytes to read |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3210 | * @param retlen pointer to variable to store the number of read bytes |
| 3211 | * @param buf the databuffer to put/get data |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3212 | * |
| 3213 | * Read factory OTP info. |
| 3214 | */ |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3215 | static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 3216 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3217 | { |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3218 | return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL, |
| 3219 | MTD_OTP_FACTORY); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | /** |
| 3223 | * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area |
| 3224 | * @param mtd MTD device structure |
| 3225 | * @param from The offset to read |
| 3226 | * @param len number of bytes to read |
| 3227 | * @param retlen pointer to variable to store the number of read bytes |
| 3228 | * @param buf the databuffer to put/get data |
| 3229 | * |
| 3230 | * Read factory OTP area. |
| 3231 | */ |
| 3232 | static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 3233 | size_t len, size_t *retlen, u_char *buf) |
| 3234 | { |
| 3235 | return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY); |
| 3236 | } |
| 3237 | |
| 3238 | /** |
| 3239 | * onenand_get_user_prot_info - [MTD Interface] Read user OTP info |
| 3240 | * @param mtd MTD device structure |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3241 | * @param retlen pointer to variable to store the number of read bytes |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3242 | * @param len number of bytes to read |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3243 | * @param buf the databuffer to put/get data |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3244 | * |
| 3245 | * Read user OTP info. |
| 3246 | */ |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3247 | static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 3248 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3249 | { |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 3250 | return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL, |
| 3251 | MTD_OTP_USER); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | /** |
| 3255 | * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area |
| 3256 | * @param mtd MTD device structure |
| 3257 | * @param from The offset to read |
| 3258 | * @param len number of bytes to read |
| 3259 | * @param retlen pointer to variable to store the number of read bytes |
| 3260 | * @param buf the databuffer to put/get data |
| 3261 | * |
| 3262 | * Read user OTP area. |
| 3263 | */ |
| 3264 | static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 3265 | size_t len, size_t *retlen, u_char *buf) |
| 3266 | { |
| 3267 | return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER); |
| 3268 | } |
| 3269 | |
| 3270 | /** |
| 3271 | * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area |
| 3272 | * @param mtd MTD device structure |
| 3273 | * @param from The offset to write |
| 3274 | * @param len number of bytes to write |
| 3275 | * @param retlen pointer to variable to store the number of write bytes |
| 3276 | * @param buf the databuffer to put/get data |
| 3277 | * |
| 3278 | * Write user OTP area. |
| 3279 | */ |
| 3280 | static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 3281 | size_t len, size_t *retlen, u_char *buf) |
| 3282 | { |
| 3283 | return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER); |
| 3284 | } |
| 3285 | |
| 3286 | /** |
| 3287 | * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area |
| 3288 | * @param mtd MTD device structure |
| 3289 | * @param from The offset to lock |
| 3290 | * @param len number of bytes to unlock |
| 3291 | * |
| 3292 | * Write lock mark on spare area in page 0 in OTP block |
| 3293 | */ |
| 3294 | static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 3295 | size_t len) |
| 3296 | { |
Kyungmin Park | 69d7918 | 2007-12-14 14:47:21 +0900 | [diff] [blame] | 3297 | struct onenand_chip *this = mtd->priv; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3298 | u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3299 | size_t retlen; |
| 3300 | int ret; |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3301 | unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3302 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3303 | memset(buf, 0xff, FLEXONENAND(this) ? this->writesize |
| 3304 | : mtd->oobsize); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3305 | /* |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3306 | * Write lock mark to 8th word of sector0 of page0 of the spare0. |
| 3307 | * We write 16 bytes spare area instead of 2 bytes. |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3308 | * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of |
| 3309 | * main area of page 49. |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3310 | */ |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3311 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3312 | from = 0; |
| 3313 | len = FLEXONENAND(this) ? mtd->writesize : 16; |
| 3314 | |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3315 | /* |
| 3316 | * Note: OTP lock operation |
| 3317 | * OTP block : 0xXXFC XX 1111 1100 |
| 3318 | * 1st block : 0xXXF3 (If chip support) XX 1111 0011 |
| 3319 | * Both : 0xXXF0 (If chip support) XX 1111 0000 |
| 3320 | */ |
| 3321 | if (FLEXONENAND(this)) |
| 3322 | otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET; |
| 3323 | |
| 3324 | /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */ |
| 3325 | if (otp == 1) |
| 3326 | buf[otp_lock_offset] = 0xFC; |
| 3327 | else if (otp == 2) |
| 3328 | buf[otp_lock_offset] = 0xF3; |
| 3329 | else if (otp == 3) |
| 3330 | buf[otp_lock_offset] = 0xF0; |
| 3331 | else if (otp != 0) |
| 3332 | printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n"); |
| 3333 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3334 | ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER); |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3335 | |
| 3336 | return ret ? : retlen; |
| 3337 | } |
Amul Kumar Saha | 3cf6025 | 2009-10-21 17:00:05 +0530 | [diff] [blame] | 3338 | |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 3339 | #endif /* CONFIG_MTD_ONENAND_OTP */ |
| 3340 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3341 | /** |
Kyungmin Park | 75384b0 | 2007-01-18 11:10:57 +0900 | [diff] [blame] | 3342 | * onenand_check_features - Check and set OneNAND features |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3343 | * @param mtd MTD data structure |
| 3344 | * |
Kyungmin Park | 75384b0 | 2007-01-18 11:10:57 +0900 | [diff] [blame] | 3345 | * Check and set OneNAND features |
| 3346 | * - lock scheme |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3347 | * - two plane |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3348 | */ |
Kyungmin Park | 75384b0 | 2007-01-18 11:10:57 +0900 | [diff] [blame] | 3349 | static void onenand_check_features(struct mtd_info *mtd) |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3350 | { |
| 3351 | struct onenand_chip *this = mtd->priv; |
Roman Tereshonkov | edb44b9 | 2010-10-11 14:47:32 +0300 | [diff] [blame] | 3352 | unsigned int density, process, numbufs; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3353 | |
| 3354 | /* Lock scheme depends on density and process */ |
Kyungmin Park | e71f04f | 2007-12-11 11:23:45 +0900 | [diff] [blame] | 3355 | density = onenand_get_density(this->device_id); |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3356 | process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT; |
Roman Tereshonkov | edb44b9 | 2010-10-11 14:47:32 +0300 | [diff] [blame] | 3357 | numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3358 | |
| 3359 | /* Lock scheme */ |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3360 | switch (density) { |
| 3361 | case ONENAND_DEVICE_DENSITY_4Gb: |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 3362 | if (ONENAND_IS_DDP(this)) |
| 3363 | this->options |= ONENAND_HAS_2PLANE; |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 3364 | else if (numbufs == 1) { |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 3365 | this->options |= ONENAND_HAS_4KB_PAGE; |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 3366 | this->options |= ONENAND_HAS_CACHE_PROGRAM; |
Kyungmin Park | e1c1024 | 2011-06-22 14:16:49 +0900 | [diff] [blame] | 3367 | /* |
| 3368 | * There are two different 4KiB pagesize chips |
| 3369 | * and no way to detect it by H/W config values. |
| 3370 | * |
| 3371 | * To detect the correct NOP for each chips, |
| 3372 | * It should check the version ID as workaround. |
| 3373 | * |
| 3374 | * Now it has as following |
| 3375 | * KFM4G16Q4M has NOP 4 with version ID 0x0131 |
| 3376 | * KFM4G16Q5M has NOP 1 with versoin ID 0x013e |
| 3377 | */ |
| 3378 | if ((this->version_id & 0xf) == 0xe) |
| 3379 | this->options |= ONENAND_HAS_NOP_1; |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 3380 | } |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3381 | |
| 3382 | case ONENAND_DEVICE_DENSITY_2Gb: |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 3383 | /* 2Gb DDP does not have 2 plane */ |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3384 | if (!ONENAND_IS_DDP(this)) |
| 3385 | this->options |= ONENAND_HAS_2PLANE; |
| 3386 | this->options |= ONENAND_HAS_UNLOCK_ALL; |
| 3387 | |
| 3388 | case ONENAND_DEVICE_DENSITY_1Gb: |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3389 | /* A-Die has all block unlock */ |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3390 | if (process) |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3391 | this->options |= ONENAND_HAS_UNLOCK_ALL; |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3392 | break; |
| 3393 | |
| 3394 | default: |
| 3395 | /* Some OneNAND has continuous lock scheme */ |
| 3396 | if (!process) |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3397 | this->options |= ONENAND_HAS_CONT_LOCK; |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3398 | break; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3399 | } |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3400 | |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 3401 | /* The MLC has 4KiB pagesize. */ |
| 3402 | if (ONENAND_IS_MLC(this)) |
| 3403 | this->options |= ONENAND_HAS_4KB_PAGE; |
| 3404 | |
| 3405 | if (ONENAND_IS_4KB_PAGE(this)) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3406 | this->options &= ~ONENAND_HAS_2PLANE; |
| 3407 | |
| 3408 | if (FLEXONENAND(this)) { |
| 3409 | this->options &= ~ONENAND_HAS_CONT_LOCK; |
| 3410 | this->options |= ONENAND_HAS_UNLOCK_ALL; |
| 3411 | } |
| 3412 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3413 | if (this->options & ONENAND_HAS_CONT_LOCK) |
| 3414 | printk(KERN_DEBUG "Lock scheme is Continuous Lock\n"); |
| 3415 | if (this->options & ONENAND_HAS_UNLOCK_ALL) |
| 3416 | printk(KERN_DEBUG "Chip support all block unlock\n"); |
| 3417 | if (this->options & ONENAND_HAS_2PLANE) |
| 3418 | printk(KERN_DEBUG "Chip has 2 plane\n"); |
Kyungmin Park | 6a88c47 | 2010-04-28 17:46:45 +0200 | [diff] [blame] | 3419 | if (this->options & ONENAND_HAS_4KB_PAGE) |
| 3420 | printk(KERN_DEBUG "Chip has 4KiB pagesize\n"); |
Roman Tereshonkov | ac80dac | 2010-11-03 12:55:21 +0200 | [diff] [blame] | 3421 | if (this->options & ONENAND_HAS_CACHE_PROGRAM) |
| 3422 | printk(KERN_DEBUG "Chip has cache program feature\n"); |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /** |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 3426 | * onenand_print_device_info - Print device & version ID |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3427 | * @param device device ID |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 3428 | * @param version version ID |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3429 | * |
Kyungmin Park | e3da806 | 2007-02-15 09:36:39 +0900 | [diff] [blame] | 3430 | * Print device & version ID |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3431 | */ |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3432 | static void onenand_print_device_info(int device, int version) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3433 | { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3434 | int vcc, demuxed, ddp, density, flexonenand; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3435 | |
| 3436 | vcc = device & ONENAND_DEVICE_VCC_MASK; |
| 3437 | demuxed = device & ONENAND_DEVICE_IS_DEMUX; |
| 3438 | ddp = device & ONENAND_DEVICE_IS_DDP; |
Kyungmin Park | e71f04f | 2007-12-11 11:23:45 +0900 | [diff] [blame] | 3439 | density = onenand_get_density(device); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3440 | flexonenand = device & DEVICE_IS_FLEXONENAND; |
| 3441 | printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n", |
| 3442 | demuxed ? "" : "Muxed ", |
| 3443 | flexonenand ? "Flex-" : "", |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3444 | ddp ? "(DDP)" : "", |
| 3445 | (16 << density), |
| 3446 | vcc ? "2.65/3.3" : "1.8", |
| 3447 | device); |
Artem Bityutskiy | 49dc08e | 2007-09-21 19:35:21 +0300 | [diff] [blame] | 3448 | printk(KERN_INFO "OneNAND version = 0x%04x\n", version); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | static const struct onenand_manufacturers onenand_manuf_ids[] = { |
| 3452 | {ONENAND_MFR_SAMSUNG, "Samsung"}, |
Adrian Hunter | ee8f376 | 2009-05-05 11:04:19 +0300 | [diff] [blame] | 3453 | {ONENAND_MFR_NUMONYX, "Numonyx"}, |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3454 | }; |
| 3455 | |
| 3456 | /** |
| 3457 | * onenand_check_maf - Check manufacturer ID |
| 3458 | * @param manuf manufacturer ID |
| 3459 | * |
| 3460 | * Check manufacturer ID |
| 3461 | */ |
| 3462 | static int onenand_check_maf(int manuf) |
| 3463 | { |
Kyungmin Park | 37b1cc3 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3464 | int size = ARRAY_SIZE(onenand_manuf_ids); |
| 3465 | char *name; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3466 | int i; |
| 3467 | |
Kyungmin Park | 37b1cc3 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3468 | for (i = 0; i < size; i++) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3469 | if (manuf == onenand_manuf_ids[i].id) |
| 3470 | break; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3471 | |
Kyungmin Park | 37b1cc3 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3472 | if (i < size) |
| 3473 | name = onenand_manuf_ids[i].name; |
| 3474 | else |
| 3475 | name = "Unknown"; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3476 | |
Kyungmin Park | 37b1cc3 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3477 | printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf); |
| 3478 | |
| 3479 | return (i == size); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | /** |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3483 | * flexonenand_get_boundary - Reads the SLC boundary |
| 3484 | * @param onenand_info - onenand info structure |
| 3485 | **/ |
| 3486 | static int flexonenand_get_boundary(struct mtd_info *mtd) |
| 3487 | { |
| 3488 | struct onenand_chip *this = mtd->priv; |
| 3489 | unsigned die, bdry; |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3490 | int syscfg, locked; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3491 | |
| 3492 | /* Disable ECC */ |
| 3493 | syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1); |
| 3494 | this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1); |
| 3495 | |
| 3496 | for (die = 0; die < this->dies; die++) { |
| 3497 | this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0); |
| 3498 | this->wait(mtd, FL_SYNCING); |
| 3499 | |
| 3500 | this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0); |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3501 | this->wait(mtd, FL_READING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3502 | |
| 3503 | bdry = this->read_word(this->base + ONENAND_DATARAM); |
| 3504 | if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3) |
| 3505 | locked = 0; |
| 3506 | else |
| 3507 | locked = 1; |
| 3508 | this->boundary[die] = bdry & FLEXONENAND_PI_MASK; |
| 3509 | |
| 3510 | this->command(mtd, ONENAND_CMD_RESET, 0, 0); |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3511 | this->wait(mtd, FL_RESETING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3512 | |
| 3513 | printk(KERN_INFO "Die %d boundary: %d%s\n", die, |
| 3514 | this->boundary[die], locked ? "(Locked)" : "(Unlocked)"); |
| 3515 | } |
| 3516 | |
| 3517 | /* Enable ECC */ |
| 3518 | this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1); |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | /** |
| 3523 | * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info |
| 3524 | * boundary[], diesize[], mtd->size, mtd->erasesize |
| 3525 | * @param mtd - MTD device structure |
| 3526 | */ |
| 3527 | static void flexonenand_get_size(struct mtd_info *mtd) |
| 3528 | { |
| 3529 | struct onenand_chip *this = mtd->priv; |
| 3530 | int die, i, eraseshift, density; |
| 3531 | int blksperdie, maxbdry; |
| 3532 | loff_t ofs; |
| 3533 | |
| 3534 | density = onenand_get_density(this->device_id); |
| 3535 | blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift); |
| 3536 | blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0; |
| 3537 | maxbdry = blksperdie - 1; |
| 3538 | eraseshift = this->erase_shift - 1; |
| 3539 | |
| 3540 | mtd->numeraseregions = this->dies << 1; |
| 3541 | |
| 3542 | /* This fills up the device boundary */ |
| 3543 | flexonenand_get_boundary(mtd); |
| 3544 | die = ofs = 0; |
| 3545 | i = -1; |
| 3546 | for (; die < this->dies; die++) { |
| 3547 | if (!die || this->boundary[die-1] != maxbdry) { |
| 3548 | i++; |
| 3549 | mtd->eraseregions[i].offset = ofs; |
| 3550 | mtd->eraseregions[i].erasesize = 1 << eraseshift; |
| 3551 | mtd->eraseregions[i].numblocks = |
| 3552 | this->boundary[die] + 1; |
| 3553 | ofs += mtd->eraseregions[i].numblocks << eraseshift; |
| 3554 | eraseshift++; |
| 3555 | } else { |
| 3556 | mtd->numeraseregions -= 1; |
| 3557 | mtd->eraseregions[i].numblocks += |
| 3558 | this->boundary[die] + 1; |
| 3559 | ofs += (this->boundary[die] + 1) << (eraseshift - 1); |
| 3560 | } |
| 3561 | if (this->boundary[die] != maxbdry) { |
| 3562 | i++; |
| 3563 | mtd->eraseregions[i].offset = ofs; |
| 3564 | mtd->eraseregions[i].erasesize = 1 << eraseshift; |
| 3565 | mtd->eraseregions[i].numblocks = maxbdry ^ |
| 3566 | this->boundary[die]; |
| 3567 | ofs += mtd->eraseregions[i].numblocks << eraseshift; |
| 3568 | eraseshift--; |
| 3569 | } else |
| 3570 | mtd->numeraseregions -= 1; |
| 3571 | } |
| 3572 | |
| 3573 | /* Expose MLC erase size except when all blocks are SLC */ |
| 3574 | mtd->erasesize = 1 << this->erase_shift; |
| 3575 | if (mtd->numeraseregions == 1) |
| 3576 | mtd->erasesize >>= 1; |
| 3577 | |
| 3578 | printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions); |
| 3579 | for (i = 0; i < mtd->numeraseregions; i++) |
| 3580 | printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x," |
| 3581 | " numblocks: %04u]\n", |
| 3582 | (unsigned int) mtd->eraseregions[i].offset, |
| 3583 | mtd->eraseregions[i].erasesize, |
| 3584 | mtd->eraseregions[i].numblocks); |
| 3585 | |
| 3586 | for (die = 0, mtd->size = 0; die < this->dies; die++) { |
| 3587 | this->diesize[die] = (loff_t)blksperdie << this->erase_shift; |
| 3588 | this->diesize[die] -= (loff_t)(this->boundary[die] + 1) |
| 3589 | << (this->erase_shift - 1); |
| 3590 | mtd->size += this->diesize[die]; |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | /** |
| 3595 | * flexonenand_check_blocks_erased - Check if blocks are erased |
| 3596 | * @param mtd_info - mtd info structure |
| 3597 | * @param start - first erase block to check |
| 3598 | * @param end - last erase block to check |
| 3599 | * |
| 3600 | * Converting an unerased block from MLC to SLC |
| 3601 | * causes byte values to change. Since both data and its ECC |
| 3602 | * have changed, reads on the block give uncorrectable error. |
| 3603 | * This might lead to the block being detected as bad. |
| 3604 | * |
| 3605 | * Avoid this by ensuring that the block to be converted is |
| 3606 | * erased. |
| 3607 | */ |
| 3608 | static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end) |
| 3609 | { |
| 3610 | struct onenand_chip *this = mtd->priv; |
| 3611 | int i, ret; |
| 3612 | int block; |
| 3613 | struct mtd_oob_ops ops = { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3614 | .mode = MTD_OPS_PLACE_OOB, |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3615 | .ooboffs = 0, |
| 3616 | .ooblen = mtd->oobsize, |
| 3617 | .datbuf = NULL, |
| 3618 | .oobbuf = this->oob_buf, |
| 3619 | }; |
| 3620 | loff_t addr; |
| 3621 | |
| 3622 | printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end); |
| 3623 | |
| 3624 | for (block = start; block <= end; block++) { |
| 3625 | addr = flexonenand_addr(this, block); |
| 3626 | if (onenand_block_isbad_nolock(mtd, addr, 0)) |
| 3627 | continue; |
| 3628 | |
| 3629 | /* |
| 3630 | * Since main area write results in ECC write to spare, |
| 3631 | * it is sufficient to check only ECC bytes for change. |
| 3632 | */ |
| 3633 | ret = onenand_read_oob_nolock(mtd, addr, &ops); |
| 3634 | if (ret) |
| 3635 | return ret; |
| 3636 | |
| 3637 | for (i = 0; i < mtd->oobsize; i++) |
| 3638 | if (this->oob_buf[i] != 0xff) |
| 3639 | break; |
| 3640 | |
| 3641 | if (i != mtd->oobsize) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3642 | printk(KERN_WARNING "%s: Block %d not erased.\n", |
| 3643 | __func__, block); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3644 | return 1; |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | return 0; |
| 3649 | } |
| 3650 | |
| 3651 | /** |
| 3652 | * flexonenand_set_boundary - Writes the SLC boundary |
| 3653 | * @param mtd - mtd info structure |
| 3654 | */ |
Sachin Kamat | 0131950 | 2012-09-22 11:42:31 +0530 | [diff] [blame] | 3655 | static int flexonenand_set_boundary(struct mtd_info *mtd, int die, |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3656 | int boundary, int lock) |
| 3657 | { |
| 3658 | struct onenand_chip *this = mtd->priv; |
| 3659 | int ret, density, blksperdie, old, new, thisboundary; |
| 3660 | loff_t addr; |
| 3661 | |
| 3662 | /* Change only once for SDP Flex-OneNAND */ |
| 3663 | if (die && (!ONENAND_IS_DDP(this))) |
| 3664 | return 0; |
| 3665 | |
| 3666 | /* boundary value of -1 indicates no required change */ |
| 3667 | if (boundary < 0 || boundary == this->boundary[die]) |
| 3668 | return 0; |
| 3669 | |
| 3670 | density = onenand_get_density(this->device_id); |
| 3671 | blksperdie = ((16 << density) << 20) >> this->erase_shift; |
| 3672 | blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0; |
| 3673 | |
| 3674 | if (boundary >= blksperdie) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3675 | printk(KERN_ERR "%s: Invalid boundary value. " |
| 3676 | "Boundary not changed.\n", __func__); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3677 | return -EINVAL; |
| 3678 | } |
| 3679 | |
| 3680 | /* Check if converting blocks are erased */ |
| 3681 | old = this->boundary[die] + (die * this->density_mask); |
| 3682 | new = boundary + (die * this->density_mask); |
| 3683 | ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new)); |
| 3684 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3685 | printk(KERN_ERR "%s: Please erase blocks " |
| 3686 | "before boundary change\n", __func__); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3687 | return ret; |
| 3688 | } |
| 3689 | |
| 3690 | this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0); |
| 3691 | this->wait(mtd, FL_SYNCING); |
| 3692 | |
| 3693 | /* Check is boundary is locked */ |
| 3694 | this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0); |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3695 | this->wait(mtd, FL_READING); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3696 | |
| 3697 | thisboundary = this->read_word(this->base + ONENAND_DATARAM); |
| 3698 | if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3699 | printk(KERN_ERR "%s: boundary locked\n", __func__); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3700 | ret = 1; |
| 3701 | goto out; |
| 3702 | } |
| 3703 | |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3704 | printk(KERN_INFO "Changing die %d boundary: %d%s\n", |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3705 | die, boundary, lock ? "(Locked)" : "(Unlocked)"); |
| 3706 | |
| 3707 | addr = die ? this->diesize[0] : 0; |
| 3708 | |
| 3709 | boundary &= FLEXONENAND_PI_MASK; |
| 3710 | boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT); |
| 3711 | |
| 3712 | this->command(mtd, ONENAND_CMD_ERASE, addr, 0); |
| 3713 | ret = this->wait(mtd, FL_ERASING); |
| 3714 | if (ret) { |
Mika Korhonen | f369c7e | 2009-10-23 07:50:44 +0200 | [diff] [blame] | 3715 | printk(KERN_ERR "%s: Failed PI erase for Die %d\n", |
| 3716 | __func__, die); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3717 | goto out; |
| 3718 | } |
| 3719 | |
| 3720 | this->write_word(boundary, this->base + ONENAND_DATARAM); |
| 3721 | this->command(mtd, ONENAND_CMD_PROG, addr, 0); |
| 3722 | ret = this->wait(mtd, FL_WRITING); |
| 3723 | if (ret) { |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3724 | printk(KERN_ERR "%s: Failed PI write for Die %d\n", |
| 3725 | __func__, die); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3726 | goto out; |
| 3727 | } |
| 3728 | |
| 3729 | this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0); |
| 3730 | ret = this->wait(mtd, FL_WRITING); |
| 3731 | out: |
| 3732 | this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND); |
| 3733 | this->wait(mtd, FL_RESETING); |
| 3734 | if (!ret) |
| 3735 | /* Recalculate device size on boundary change*/ |
| 3736 | flexonenand_get_size(mtd); |
| 3737 | |
| 3738 | return ret; |
| 3739 | } |
| 3740 | |
| 3741 | /** |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3742 | * onenand_chip_probe - [OneNAND Interface] The generic chip probe |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3743 | * @param mtd MTD device structure |
| 3744 | * |
| 3745 | * OneNAND detection method: |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 3746 | * Compare the values from command with ones from register |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3747 | */ |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3748 | static int onenand_chip_probe(struct mtd_info *mtd) |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3749 | { |
| 3750 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3751 | int bram_maf_id, bram_dev_id, maf_id, dev_id; |
Kyungmin Park | 47e777e | 2006-09-25 23:53:28 +0000 | [diff] [blame] | 3752 | int syscfg; |
| 3753 | |
| 3754 | /* Save system configuration 1 */ |
| 3755 | syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1); |
| 3756 | /* Clear Sync. Burst Read mode to read BootRAM */ |
Adrian Hunter | ee8f376 | 2009-05-05 11:04:19 +0300 | [diff] [blame] | 3757 | this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3758 | |
| 3759 | /* Send the command for reading device ID from BootRAM */ |
| 3760 | this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM); |
| 3761 | |
| 3762 | /* Read manufacturer and device IDs from BootRAM */ |
| 3763 | bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0); |
| 3764 | bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2); |
| 3765 | |
Kyungmin Park | 47e777e | 2006-09-25 23:53:28 +0000 | [diff] [blame] | 3766 | /* Reset OneNAND to read default register values */ |
| 3767 | this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM); |
| 3768 | /* Wait reset */ |
| 3769 | this->wait(mtd, FL_RESETING); |
| 3770 | |
| 3771 | /* Restore system configuration 1 */ |
| 3772 | this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1); |
| 3773 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3774 | /* Check manufacturer ID */ |
| 3775 | if (onenand_check_maf(bram_maf_id)) |
| 3776 | return -ENXIO; |
| 3777 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3778 | /* Read manufacturer and device IDs from Register */ |
| 3779 | maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID); |
| 3780 | dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID); |
| 3781 | |
| 3782 | /* Check OneNAND device */ |
| 3783 | if (maf_id != bram_maf_id || dev_id != bram_dev_id) |
| 3784 | return -ENXIO; |
| 3785 | |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3786 | return 0; |
| 3787 | } |
| 3788 | |
| 3789 | /** |
| 3790 | * onenand_probe - [OneNAND Interface] Probe the OneNAND device |
| 3791 | * @param mtd MTD device structure |
| 3792 | */ |
| 3793 | static int onenand_probe(struct mtd_info *mtd) |
| 3794 | { |
| 3795 | struct onenand_chip *this = mtd->priv; |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3796 | int dev_id, ver_id; |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3797 | int density; |
| 3798 | int ret; |
| 3799 | |
| 3800 | ret = this->chip_probe(mtd); |
| 3801 | if (ret) |
| 3802 | return ret; |
| 3803 | |
Brian Norris | 6b7368c | 2013-08-27 18:01:19 -0700 | [diff] [blame] | 3804 | /* Device and version IDs from Register */ |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3805 | dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID); |
| 3806 | ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID); |
| 3807 | this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY); |
| 3808 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3809 | /* Flash device information */ |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3810 | onenand_print_device_info(dev_id, ver_id); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3811 | this->device_id = dev_id; |
Kyungmin Park | 28b79ff | 2006-09-26 09:45:28 +0000 | [diff] [blame] | 3812 | this->version_id = ver_id; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3813 | |
Kyungmin Park | c37cb56 | 2010-04-28 17:46:48 +0200 | [diff] [blame] | 3814 | /* Check OneNAND features */ |
| 3815 | onenand_check_features(mtd); |
| 3816 | |
Kyungmin Park | e71f04f | 2007-12-11 11:23:45 +0900 | [diff] [blame] | 3817 | density = onenand_get_density(dev_id); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3818 | if (FLEXONENAND(this)) { |
| 3819 | this->dies = ONENAND_IS_DDP(this) ? 2 : 1; |
| 3820 | /* Maximum possible erase regions */ |
| 3821 | mtd->numeraseregions = this->dies << 1; |
| 3822 | mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info) |
| 3823 | * (this->dies << 1), GFP_KERNEL); |
| 3824 | if (!mtd->eraseregions) |
| 3825 | return -ENOMEM; |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | * For Flex-OneNAND, chipsize represents maximum possible device size. |
| 3830 | * mtd->size represents the actual device size. |
| 3831 | */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3832 | this->chipsize = (16 << density) << 20; |
| 3833 | |
| 3834 | /* OneNAND page size & block size */ |
| 3835 | /* The data buffer size is equal to page size */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3836 | mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3837 | /* We use the full BufferRAM */ |
Kyungmin Park | 8a8f632 | 2010-12-02 09:24:16 +0900 | [diff] [blame] | 3838 | if (ONENAND_IS_4KB_PAGE(this)) |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3839 | mtd->writesize <<= 1; |
| 3840 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3841 | mtd->oobsize = mtd->writesize >> 5; |
Kyungmin Park | 9bfbc9b | 2007-01-31 14:25:21 +0900 | [diff] [blame] | 3842 | /* Pages per a block are always 64 in OneNAND */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3843 | mtd->erasesize = mtd->writesize << 6; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3844 | /* |
| 3845 | * Flex-OneNAND SLC area has 64 pages per block. |
| 3846 | * Flex-OneNAND MLC area has 128 pages per block. |
| 3847 | * Expose MLC erase size to find erase_shift and page_mask. |
| 3848 | */ |
| 3849 | if (FLEXONENAND(this)) |
| 3850 | mtd->erasesize <<= 1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3851 | |
| 3852 | this->erase_shift = ffs(mtd->erasesize) - 1; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 3853 | this->page_shift = ffs(mtd->writesize) - 1; |
Kyungmin Park | 9bfbc9b | 2007-01-31 14:25:21 +0900 | [diff] [blame] | 3854 | this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3855 | /* Set density mask. it is used for DDP */ |
| 3856 | if (ONENAND_IS_DDP(this)) |
| 3857 | this->density_mask = this->chipsize >> (this->erase_shift + 1); |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3858 | /* It's real page size */ |
| 3859 | this->writesize = mtd->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3860 | |
Mika Korhonen | 492e150 | 2009-06-09 21:52:35 +0300 | [diff] [blame] | 3861 | /* REVISIT: Multichip handling */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3862 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3863 | if (FLEXONENAND(this)) |
| 3864 | flexonenand_get_size(mtd); |
| 3865 | else |
| 3866 | mtd->size = this->chipsize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3867 | |
Kyungmin Park | ee9745f | 2007-06-30 13:57:49 +0900 | [diff] [blame] | 3868 | /* |
| 3869 | * We emulate the 4KiB page and 256KiB erase block size |
| 3870 | * But oobsize is still 64 bytes. |
| 3871 | * It is only valid if you turn on 2X program support, |
| 3872 | * Otherwise it will be ignored by compiler. |
| 3873 | */ |
| 3874 | if (ONENAND_IS_2PLANE(this)) { |
| 3875 | mtd->writesize <<= 1; |
| 3876 | mtd->erasesize <<= 1; |
| 3877 | } |
| 3878 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3879 | return 0; |
| 3880 | } |
| 3881 | |
Kyungmin Park | a41371e | 2005-09-29 03:55:31 +0100 | [diff] [blame] | 3882 | /** |
| 3883 | * onenand_suspend - [MTD Interface] Suspend the OneNAND flash |
| 3884 | * @param mtd MTD device structure |
| 3885 | */ |
| 3886 | static int onenand_suspend(struct mtd_info *mtd) |
| 3887 | { |
| 3888 | return onenand_get_device(mtd, FL_PM_SUSPENDED); |
| 3889 | } |
| 3890 | |
| 3891 | /** |
| 3892 | * onenand_resume - [MTD Interface] Resume the OneNAND flash |
| 3893 | * @param mtd MTD device structure |
| 3894 | */ |
| 3895 | static void onenand_resume(struct mtd_info *mtd) |
| 3896 | { |
| 3897 | struct onenand_chip *this = mtd->priv; |
| 3898 | |
| 3899 | if (this->state == FL_PM_SUSPENDED) |
| 3900 | onenand_release_device(mtd); |
| 3901 | else |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 3902 | printk(KERN_ERR "%s: resume() called for the chip which is not " |
| 3903 | "in suspended state\n", __func__); |
Kyungmin Park | a41371e | 2005-09-29 03:55:31 +0100 | [diff] [blame] | 3904 | } |
| 3905 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3906 | /** |
| 3907 | * onenand_scan - [OneNAND Interface] Scan for the OneNAND device |
| 3908 | * @param mtd MTD device structure |
| 3909 | * @param maxchips Number of chips to scan for |
| 3910 | * |
| 3911 | * This fills out all the not initialized function pointers |
| 3912 | * with the defaults. |
| 3913 | * The flash ID is read and the mtd/chip structures are |
| 3914 | * filled with the appropriate values. |
| 3915 | */ |
| 3916 | int onenand_scan(struct mtd_info *mtd, int maxchips) |
| 3917 | { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3918 | int i, ret; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3919 | struct onenand_chip *this = mtd->priv; |
| 3920 | |
| 3921 | if (!this->read_word) |
| 3922 | this->read_word = onenand_readw; |
| 3923 | if (!this->write_word) |
| 3924 | this->write_word = onenand_writew; |
| 3925 | |
| 3926 | if (!this->command) |
| 3927 | this->command = onenand_command; |
| 3928 | if (!this->wait) |
Kyungmin Park | 2c22120 | 2006-11-16 11:23:48 +0900 | [diff] [blame] | 3929 | onenand_setup_wait(mtd); |
Kyungmin Park | 31bb999 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3930 | if (!this->bbt_wait) |
| 3931 | this->bbt_wait = onenand_bbt_wait; |
| 3932 | if (!this->unlock_all) |
| 3933 | this->unlock_all = onenand_unlock_all; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3934 | |
Kyungmin Park | ad0d363 | 2010-05-28 11:03:11 +0900 | [diff] [blame] | 3935 | if (!this->chip_probe) |
| 3936 | this->chip_probe = onenand_chip_probe; |
| 3937 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3938 | if (!this->read_bufferram) |
| 3939 | this->read_bufferram = onenand_read_bufferram; |
| 3940 | if (!this->write_bufferram) |
| 3941 | this->write_bufferram = onenand_write_bufferram; |
| 3942 | |
Kyungmin Park | cdc0013 | 2005-09-03 07:15:48 +0100 | [diff] [blame] | 3943 | if (!this->block_markbad) |
| 3944 | this->block_markbad = onenand_default_block_markbad; |
| 3945 | if (!this->scan_bbt) |
| 3946 | this->scan_bbt = onenand_default_bbt; |
| 3947 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3948 | if (onenand_probe(mtd)) |
| 3949 | return -ENXIO; |
| 3950 | |
Kyungmin Park | 52b0eea | 2005-09-03 07:07:19 +0100 | [diff] [blame] | 3951 | /* Set Sync. Burst Read after probing */ |
| 3952 | if (this->mmcontrol) { |
| 3953 | printk(KERN_INFO "OneNAND Sync. Burst Read support\n"); |
| 3954 | this->read_bufferram = onenand_sync_read_bufferram; |
| 3955 | } |
| 3956 | |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3957 | /* Allocate buffers, if necessary */ |
| 3958 | if (!this->page_buf) { |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 3959 | this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL); |
Jingoo Han | e4eec19 | 2014-02-06 15:15:38 +0900 | [diff] [blame] | 3960 | if (!this->page_buf) |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3961 | return -ENOMEM; |
Kyungmin Park | 4a8ce0b | 2010-04-28 17:46:46 +0200 | [diff] [blame] | 3962 | #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE |
| 3963 | this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL); |
| 3964 | if (!this->verify_buf) { |
| 3965 | kfree(this->page_buf); |
| 3966 | return -ENOMEM; |
| 3967 | } |
| 3968 | #endif |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3969 | this->options |= ONENAND_PAGEBUF_ALLOC; |
| 3970 | } |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 3971 | if (!this->oob_buf) { |
| 3972 | this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL); |
| 3973 | if (!this->oob_buf) { |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 3974 | if (this->options & ONENAND_PAGEBUF_ALLOC) { |
| 3975 | this->options &= ~ONENAND_PAGEBUF_ALLOC; |
| 3976 | kfree(this->page_buf); |
| 3977 | } |
| 3978 | return -ENOMEM; |
| 3979 | } |
| 3980 | this->options |= ONENAND_OOBBUF_ALLOC; |
| 3981 | } |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 3982 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3983 | this->state = FL_READY; |
| 3984 | init_waitqueue_head(&this->wq); |
| 3985 | spin_lock_init(&this->chip_lock); |
| 3986 | |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 3987 | /* |
| 3988 | * Allow subpage writes up to oobsize. |
| 3989 | */ |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 3990 | switch (mtd->oobsize) { |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 3991 | case 128: |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 3992 | if (FLEXONENAND(this)) { |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 3993 | mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops); |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 3994 | mtd->subpage_sft = 0; |
| 3995 | } else { |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 3996 | mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops); |
Roman Tereshonkov | 99b17c0 | 2011-04-11 12:52:01 +0300 | [diff] [blame] | 3997 | mtd->subpage_sft = 2; |
| 3998 | } |
Kyungmin Park | e1c1024 | 2011-06-22 14:16:49 +0900 | [diff] [blame] | 3999 | if (ONENAND_IS_NOP_1(this)) |
| 4000 | mtd->subpage_sft = 0; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 4001 | break; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4002 | case 64: |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 4003 | mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops); |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 4004 | mtd->subpage_sft = 2; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4005 | break; |
| 4006 | |
| 4007 | case 32: |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 4008 | mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops); |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 4009 | mtd->subpage_sft = 1; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4010 | break; |
| 4011 | |
| 4012 | default: |
Amul Kumar Saha | 297758f | 2009-10-02 16:59:11 +0530 | [diff] [blame] | 4013 | printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n", |
| 4014 | __func__, mtd->oobsize); |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 4015 | mtd->subpage_sft = 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4016 | /* To prevent kernel oops */ |
Boris Brezillon | a411679 | 2015-12-07 22:46:45 +0100 | [diff] [blame] | 4017 | mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4018 | break; |
| 4019 | } |
| 4020 | |
Kyungmin Park | 60d84f9 | 2006-12-22 16:21:54 +0900 | [diff] [blame] | 4021 | this->subpagesize = mtd->writesize >> mtd->subpage_sft; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 4022 | |
| 4023 | /* |
| 4024 | * The number of bytes available for a client to place data into |
| 4025 | * the out of band area |
| 4026 | */ |
Boris Brezillon | d30aae6 | 2016-02-03 20:12:31 +0100 | [diff] [blame] | 4027 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 4028 | if (ret < 0) |
| 4029 | ret = 0; |
| 4030 | |
| 4031 | mtd->oobavail = ret; |
Adrian Hunter | a5e7c7b | 2007-01-31 17:19:28 +0200 | [diff] [blame] | 4032 | |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 4033 | mtd->ecc_strength = 1; |
Thomas Gleixner | d5c5e78 | 2005-11-07 11:15:51 +0000 | [diff] [blame] | 4034 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4035 | /* Fill in remaining MTD driver data */ |
Rohit Hassan Sathyanarayan | c762680 | 2010-09-27 16:02:10 +0530 | [diff] [blame] | 4036 | mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH; |
Joern Engel | 5fa4339 | 2006-05-22 23:18:29 +0200 | [diff] [blame] | 4037 | mtd->flags = MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4038 | mtd->_erase = onenand_erase; |
| 4039 | mtd->_point = NULL; |
| 4040 | mtd->_unpoint = NULL; |
| 4041 | mtd->_read = onenand_read; |
| 4042 | mtd->_write = onenand_write; |
| 4043 | mtd->_read_oob = onenand_read_oob; |
| 4044 | mtd->_write_oob = onenand_write_oob; |
| 4045 | mtd->_panic_write = onenand_panic_write; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 4046 | #ifdef CONFIG_MTD_ONENAND_OTP |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4047 | mtd->_get_fact_prot_info = onenand_get_fact_prot_info; |
| 4048 | mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg; |
| 4049 | mtd->_get_user_prot_info = onenand_get_user_prot_info; |
| 4050 | mtd->_read_user_prot_reg = onenand_read_user_prot_reg; |
| 4051 | mtd->_write_user_prot_reg = onenand_write_user_prot_reg; |
| 4052 | mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg; |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 4053 | #endif |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4054 | mtd->_sync = onenand_sync; |
| 4055 | mtd->_lock = onenand_lock; |
| 4056 | mtd->_unlock = onenand_unlock; |
| 4057 | mtd->_suspend = onenand_suspend; |
| 4058 | mtd->_resume = onenand_resume; |
| 4059 | mtd->_block_isbad = onenand_block_isbad; |
| 4060 | mtd->_block_markbad = onenand_block_markbad; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4061 | mtd->owner = THIS_MODULE; |
Anatolij Gustschin | 25dcd29 | 2010-12-16 23:42:17 +0100 | [diff] [blame] | 4062 | mtd->writebufsize = mtd->writesize; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4063 | |
| 4064 | /* Unlock whole block */ |
Roman Tereshonkov | b3dcfd3 | 2011-02-17 13:44:41 +0200 | [diff] [blame] | 4065 | if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING)) |
| 4066 | this->unlock_all(mtd); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4067 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 4068 | ret = this->scan_bbt(mtd); |
| 4069 | if ((!FLEXONENAND(this)) || ret) |
| 4070 | return ret; |
| 4071 | |
| 4072 | /* Change Flex-OneNAND boundaries if required */ |
| 4073 | for (i = 0; i < MAX_DIES; i++) |
| 4074 | flexonenand_set_boundary(mtd, i, flex_bdry[2 * i], |
| 4075 | flex_bdry[(2 * i) + 1]); |
| 4076 | |
| 4077 | return 0; |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /** |
| 4081 | * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device |
| 4082 | * @param mtd MTD device structure |
| 4083 | */ |
| 4084 | void onenand_release(struct mtd_info *mtd) |
| 4085 | { |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 4086 | struct onenand_chip *this = mtd->priv; |
| 4087 | |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4088 | /* Deregister partitions */ |
Jamie Iles | 711a632 | 2011-05-23 10:22:56 +0100 | [diff] [blame] | 4089 | mtd_device_unregister(mtd); |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 4090 | |
| 4091 | /* Free bad block table memory, if allocated */ |
Adrian Hunter | f00b004 | 2007-01-22 17:01:01 +0900 | [diff] [blame] | 4092 | if (this->bbm) { |
| 4093 | struct bbm_info *bbm = this->bbm; |
| 4094 | kfree(bbm->bbt); |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 4095 | kfree(this->bbm); |
Adrian Hunter | f00b004 | 2007-01-22 17:01:01 +0900 | [diff] [blame] | 4096 | } |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 4097 | /* Buffers allocated by onenand_scan */ |
Kyungmin Park | 4a8ce0b | 2010-04-28 17:46:46 +0200 | [diff] [blame] | 4098 | if (this->options & ONENAND_PAGEBUF_ALLOC) { |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 4099 | kfree(this->page_buf); |
Kyungmin Park | 4a8ce0b | 2010-04-28 17:46:46 +0200 | [diff] [blame] | 4100 | #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE |
| 4101 | kfree(this->verify_buf); |
| 4102 | #endif |
| 4103 | } |
Kyungmin Park | 470bc84 | 2007-03-09 10:08:11 +0900 | [diff] [blame] | 4104 | if (this->options & ONENAND_OOBBUF_ALLOC) |
| 4105 | kfree(this->oob_buf); |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 4106 | kfree(mtd->eraseregions); |
Kyungmin Park | cd5f634 | 2005-07-11 11:41:53 +0100 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | EXPORT_SYMBOL_GPL(onenand_scan); |
| 4110 | EXPORT_SYMBOL_GPL(onenand_release); |
| 4111 | |
| 4112 | MODULE_LICENSE("GPL"); |
| 4113 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 4114 | MODULE_DESCRIPTION("Generic OneNAND flash driver code"); |