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