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