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