Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mtd/onenand/onenand_base.c |
| 3 | * |
| 4 | * Copyright (C) 2005-2007 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +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 | * |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 18 | #include <linux/mtd/compat.h> |
| 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/onenand.h> |
| 21 | |
| 22 | #include <asm/io.h> |
| 23 | #include <asm/errno.h> |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 24 | #include <malloc.h> |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 25 | |
Kyungmin Park | 6b85c9f | 2008-03-31 10:40:36 +0900 | [diff] [blame] | 26 | /* It should access 16-bit instead of 8-bit */ |
Wolfgang Denk | d7024b6 | 2008-05-01 21:30:16 +0200 | [diff] [blame] | 27 | static inline void *memcpy_16(void *dst, const void *src, unsigned int len) |
Kyungmin Park | 6b85c9f | 2008-03-31 10:40:36 +0900 | [diff] [blame] | 28 | { |
| 29 | void *ret = dst; |
| 30 | short *d = dst; |
| 31 | const short *s = src; |
| 32 | |
| 33 | len >>= 1; |
| 34 | while (len-- > 0) |
| 35 | *d++ = *s++; |
| 36 | return ret; |
| 37 | } |
| 38 | |
Stefan Roese | db7ce8c | 2008-12-02 11:06:47 +0100 | [diff] [blame] | 39 | /** |
| 40 | * onenand_oob_64 - oob info for large (2KB) page |
| 41 | */ |
| 42 | static struct nand_ecclayout onenand_oob_64 = { |
| 43 | .eccbytes = 20, |
| 44 | .eccpos = { |
| 45 | 8, 9, 10, 11, 12, |
| 46 | 24, 25, 26, 27, 28, |
| 47 | 40, 41, 42, 43, 44, |
| 48 | 56, 57, 58, 59, 60, |
| 49 | }, |
| 50 | .oobfree = { |
| 51 | {2, 3}, {14, 2}, {18, 3}, {30, 2}, |
| 52 | {34, 3}, {46, 2}, {50, 3}, {62, 2} |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * onenand_oob_32 - oob info for middle (1KB) page |
| 58 | */ |
| 59 | static struct nand_ecclayout onenand_oob_32 = { |
| 60 | .eccbytes = 10, |
| 61 | .eccpos = { |
| 62 | 8, 9, 10, 11, 12, |
| 63 | 24, 25, 26, 27, 28, |
| 64 | }, |
| 65 | .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} } |
| 66 | }; |
| 67 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 68 | static const unsigned char ffchars[] = { |
| 69 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 70 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */ |
| 71 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 72 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */ |
| 73 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 74 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */ |
| 75 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 76 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */ |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * onenand_readw - [OneNAND Interface] Read OneNAND register |
| 81 | * @param addr address to read |
| 82 | * |
| 83 | * Read OneNAND register |
| 84 | */ |
| 85 | static unsigned short onenand_readw(void __iomem * addr) |
| 86 | { |
| 87 | return readw(addr); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * onenand_writew - [OneNAND Interface] Write OneNAND register with value |
| 92 | * @param value value to write |
| 93 | * @param addr address to write |
| 94 | * |
| 95 | * Write OneNAND register with value |
| 96 | */ |
| 97 | static void onenand_writew(unsigned short value, void __iomem * addr) |
| 98 | { |
| 99 | writew(value, addr); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * onenand_block_address - [DEFAULT] Get block address |
| 104 | * @param device the device id |
| 105 | * @param block the block |
| 106 | * @return translated block address if DDP, otherwise same |
| 107 | * |
| 108 | * Setup Start Address 1 Register (F100h) |
| 109 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 110 | static int onenand_block_address(struct onenand_chip *this, int block) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 111 | { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 112 | /* Device Flash Core select, NAND Flash Block Address */ |
| 113 | if (block & this->density_mask) |
| 114 | return ONENAND_DDP_CHIP1 | (block ^ this->density_mask); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 115 | |
| 116 | return block; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * onenand_bufferram_address - [DEFAULT] Get bufferram address |
| 121 | * @param device the device id |
| 122 | * @param block the block |
| 123 | * @return set DBS value if DDP, otherwise 0 |
| 124 | * |
| 125 | * Setup Start Address 2 Register (F101h) for DDP |
| 126 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 127 | static int onenand_bufferram_address(struct onenand_chip *this, int block) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 128 | { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 129 | /* Device BufferRAM Select */ |
| 130 | if (block & this->density_mask) |
| 131 | return ONENAND_DDP_CHIP1; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 132 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 133 | return ONENAND_DDP_CHIP0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * onenand_page_address - [DEFAULT] Get page address |
| 138 | * @param page the page address |
| 139 | * @param sector the sector address |
| 140 | * @return combined page and sector address |
| 141 | * |
| 142 | * Setup Start Address 8 Register (F107h) |
| 143 | */ |
| 144 | static int onenand_page_address(int page, int sector) |
| 145 | { |
| 146 | /* Flash Page Address, Flash Sector Address */ |
| 147 | int fpa, fsa; |
| 148 | |
| 149 | fpa = page & ONENAND_FPA_MASK; |
| 150 | fsa = sector & ONENAND_FSA_MASK; |
| 151 | |
| 152 | return ((fpa << ONENAND_FPA_SHIFT) | fsa); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * onenand_buffer_address - [DEFAULT] Get buffer address |
| 157 | * @param dataram1 DataRAM index |
| 158 | * @param sectors the sector address |
| 159 | * @param count the number of sectors |
| 160 | * @return the start buffer value |
| 161 | * |
| 162 | * Setup Start Buffer Register (F200h) |
| 163 | */ |
| 164 | static int onenand_buffer_address(int dataram1, int sectors, int count) |
| 165 | { |
| 166 | int bsa, bsc; |
| 167 | |
| 168 | /* BufferRAM Sector Address */ |
| 169 | bsa = sectors & ONENAND_BSA_MASK; |
| 170 | |
| 171 | if (dataram1) |
| 172 | bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */ |
| 173 | else |
| 174 | bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */ |
| 175 | |
| 176 | /* BufferRAM Sector Count */ |
| 177 | bsc = count & ONENAND_BSC_MASK; |
| 178 | |
| 179 | return ((bsa << ONENAND_BSA_SHIFT) | bsc); |
| 180 | } |
| 181 | |
| 182 | /** |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 183 | * onenand_get_density - [DEFAULT] Get OneNAND density |
| 184 | * @param dev_id OneNAND device ID |
| 185 | * |
| 186 | * Get OneNAND density from device ID |
| 187 | */ |
| 188 | static inline int onenand_get_density(int dev_id) |
| 189 | { |
| 190 | int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT; |
| 191 | return (density & ONENAND_DEVICE_DENSITY_MASK); |
| 192 | } |
| 193 | |
| 194 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 195 | * onenand_command - [DEFAULT] Send command to OneNAND device |
| 196 | * @param mtd MTD device structure |
| 197 | * @param cmd the command to be sent |
| 198 | * @param addr offset to read from or write to |
| 199 | * @param len number of bytes to read or write |
| 200 | * |
| 201 | * Send command to OneNAND device. This function is used for middle/large page |
| 202 | * devices (1KB/2KB Bytes per page) |
| 203 | */ |
| 204 | static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, |
| 205 | size_t len) |
| 206 | { |
| 207 | struct onenand_chip *this = mtd->priv; |
| 208 | int value, readcmd = 0; |
| 209 | int block, page; |
| 210 | /* Now we use page size operation */ |
| 211 | int sectors = 4, count = 4; |
| 212 | |
| 213 | /* Address translation */ |
| 214 | switch (cmd) { |
| 215 | case ONENAND_CMD_UNLOCK: |
| 216 | case ONENAND_CMD_LOCK: |
| 217 | case ONENAND_CMD_LOCK_TIGHT: |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 218 | case ONENAND_CMD_UNLOCK_ALL: |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 219 | block = -1; |
| 220 | page = -1; |
| 221 | break; |
| 222 | |
| 223 | case ONENAND_CMD_ERASE: |
| 224 | case ONENAND_CMD_BUFFERRAM: |
| 225 | block = (int)(addr >> this->erase_shift); |
| 226 | page = -1; |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | block = (int)(addr >> this->erase_shift); |
| 231 | page = (int)(addr >> this->page_shift); |
| 232 | page &= this->page_mask; |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | /* NOTE: The setting order of the registers is very important! */ |
| 237 | if (cmd == ONENAND_CMD_BUFFERRAM) { |
| 238 | /* Select DataRAM for DDP */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 239 | value = onenand_bufferram_address(this, block); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 240 | this->write_word(value, |
| 241 | this->base + ONENAND_REG_START_ADDRESS2); |
| 242 | |
| 243 | /* Switch to the next data buffer */ |
| 244 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | if (block != -1) { |
| 250 | /* Write 'DFS, FBA' of Flash */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 251 | value = onenand_block_address(this, block); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 252 | this->write_word(value, |
| 253 | this->base + ONENAND_REG_START_ADDRESS1); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 254 | |
| 255 | /* Write 'DFS, FBA' of Flash */ |
| 256 | value = onenand_bufferram_address(this, block); |
| 257 | this->write_word(value, |
| 258 | this->base + ONENAND_REG_START_ADDRESS2); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (page != -1) { |
| 262 | int dataram; |
| 263 | |
| 264 | switch (cmd) { |
| 265 | case ONENAND_CMD_READ: |
| 266 | case ONENAND_CMD_READOOB: |
| 267 | dataram = ONENAND_SET_NEXT_BUFFERRAM(this); |
| 268 | readcmd = 1; |
| 269 | break; |
| 270 | |
| 271 | default: |
| 272 | dataram = ONENAND_CURRENT_BUFFERRAM(this); |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | /* Write 'FPA, FSA' of Flash */ |
| 277 | value = onenand_page_address(page, sectors); |
| 278 | this->write_word(value, |
| 279 | this->base + ONENAND_REG_START_ADDRESS8); |
| 280 | |
| 281 | /* Write 'BSA, BSC' of DataRAM */ |
| 282 | value = onenand_buffer_address(dataram, sectors, count); |
| 283 | this->write_word(value, this->base + ONENAND_REG_START_BUFFER); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Interrupt clear */ |
| 287 | this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT); |
| 288 | /* Write command */ |
| 289 | this->write_word(cmd, this->base + ONENAND_REG_COMMAND); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * onenand_wait - [DEFAULT] wait until the command is done |
| 296 | * @param mtd MTD device structure |
| 297 | * @param state state to select the max. timeout value |
| 298 | * |
| 299 | * Wait for command done. This applies to all OneNAND command |
| 300 | * Read can take up to 30us, erase up to 2ms and program up to 350us |
| 301 | * according to general OneNAND specs |
| 302 | */ |
| 303 | static int onenand_wait(struct mtd_info *mtd, int state) |
| 304 | { |
| 305 | struct onenand_chip *this = mtd->priv; |
| 306 | unsigned int flags = ONENAND_INT_MASTER; |
| 307 | unsigned int interrupt = 0; |
| 308 | unsigned int ctrl, ecc; |
| 309 | |
| 310 | while (1) { |
| 311 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 312 | if (interrupt & flags) |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS); |
| 317 | |
| 318 | if (ctrl & ONENAND_CTRL_ERROR) { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 319 | printk("onenand_wait: controller error = 0x%04x\n", ctrl); |
| 320 | if (ctrl & ONENAND_CTRL_LOCK) |
| 321 | printk("onenand_wait: it's locked error = 0x%04x\n", |
| 322 | ctrl); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 323 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 324 | return -EIO; |
| 325 | } |
| 326 | |
| 327 | if (interrupt & ONENAND_INT_READ) { |
| 328 | ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS); |
| 329 | if (ecc & ONENAND_ECC_2BIT_ALL) { |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 330 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 331 | "onenand_wait: ECC error = 0x%04x\n", ecc); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 332 | return -EBADMSG; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * onenand_bufferram_offset - [DEFAULT] BufferRAM offset |
| 341 | * @param mtd MTD data structure |
| 342 | * @param area BufferRAM area |
| 343 | * @return offset given area |
| 344 | * |
| 345 | * Return BufferRAM offset given area |
| 346 | */ |
| 347 | static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area) |
| 348 | { |
| 349 | struct onenand_chip *this = mtd->priv; |
| 350 | |
| 351 | if (ONENAND_CURRENT_BUFFERRAM(this)) { |
| 352 | if (area == ONENAND_DATARAM) |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 353 | return mtd->writesize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 354 | if (area == ONENAND_SPARERAM) |
| 355 | return mtd->oobsize; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area |
| 363 | * @param mtd MTD data structure |
| 364 | * @param area BufferRAM area |
| 365 | * @param buffer the databuffer to put/get data |
| 366 | * @param offset offset to read from or write to |
| 367 | * @param count number of bytes to read/write |
| 368 | * |
| 369 | * Read the BufferRAM area |
| 370 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 371 | static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area, |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 372 | unsigned char *buffer, int offset, |
| 373 | size_t count) |
| 374 | { |
| 375 | struct onenand_chip *this = mtd->priv; |
| 376 | void __iomem *bufferram; |
| 377 | |
| 378 | bufferram = this->base + area; |
| 379 | bufferram += onenand_bufferram_offset(mtd, area); |
| 380 | |
Wolfgang Denk | d7024b6 | 2008-05-01 21:30:16 +0200 | [diff] [blame] | 381 | memcpy_16(buffer, bufferram + offset, count); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode |
| 388 | * @param mtd MTD data structure |
| 389 | * @param area BufferRAM area |
| 390 | * @param buffer the databuffer to put/get data |
| 391 | * @param offset offset to read from or write to |
| 392 | * @param count number of bytes to read/write |
| 393 | * |
| 394 | * Read the BufferRAM area with Sync. Burst Mode |
| 395 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 396 | static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area, |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 397 | unsigned char *buffer, int offset, |
| 398 | size_t count) |
| 399 | { |
| 400 | struct onenand_chip *this = mtd->priv; |
| 401 | void __iomem *bufferram; |
| 402 | |
| 403 | bufferram = this->base + area; |
| 404 | bufferram += onenand_bufferram_offset(mtd, area); |
| 405 | |
| 406 | this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ); |
| 407 | |
Wolfgang Denk | d7024b6 | 2008-05-01 21:30:16 +0200 | [diff] [blame] | 408 | memcpy_16(buffer, bufferram + offset, count); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 409 | |
| 410 | this->mmcontrol(mtd, 0); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area |
| 417 | * @param mtd MTD data structure |
| 418 | * @param area BufferRAM area |
| 419 | * @param buffer the databuffer to put/get data |
| 420 | * @param offset offset to read from or write to |
| 421 | * @param count number of bytes to read/write |
| 422 | * |
| 423 | * Write the BufferRAM area |
| 424 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 425 | static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area, |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 426 | const unsigned char *buffer, int offset, |
| 427 | size_t count) |
| 428 | { |
| 429 | struct onenand_chip *this = mtd->priv; |
| 430 | void __iomem *bufferram; |
| 431 | |
| 432 | bufferram = this->base + area; |
| 433 | bufferram += onenand_bufferram_offset(mtd, area); |
| 434 | |
Wolfgang Denk | d7024b6 | 2008-05-01 21:30:16 +0200 | [diff] [blame] | 435 | memcpy_16(bufferram + offset, buffer, count); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /** |
Stefan Roese | 5ed79ae | 2008-11-11 10:28:53 +0100 | [diff] [blame] | 441 | * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode |
| 442 | * @param mtd MTD data structure |
| 443 | * @param addr address to check |
| 444 | * @return blockpage address |
| 445 | * |
| 446 | * Get blockpage address at 2x program mode |
| 447 | */ |
| 448 | static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr) |
| 449 | { |
| 450 | struct onenand_chip *this = mtd->priv; |
| 451 | int blockpage, block, page; |
| 452 | |
| 453 | /* Calculate the even block number */ |
| 454 | block = (int) (addr >> this->erase_shift) & ~1; |
| 455 | /* Is it the odd plane? */ |
| 456 | if (addr & this->writesize) |
| 457 | block++; |
| 458 | page = (int) (addr >> (this->page_shift + 1)) & this->page_mask; |
| 459 | blockpage = (block << 7) | page; |
| 460 | |
| 461 | return blockpage; |
| 462 | } |
| 463 | |
| 464 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 465 | * onenand_check_bufferram - [GENERIC] Check BufferRAM information |
| 466 | * @param mtd MTD data structure |
| 467 | * @param addr address to check |
| 468 | * @return 1 if there are valid data, otherwise 0 |
| 469 | * |
| 470 | * Check bufferram if there is data we required |
| 471 | */ |
| 472 | static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr) |
| 473 | { |
| 474 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 475 | int blockpage, found = 0; |
| 476 | unsigned int i; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 477 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 478 | #ifdef CONFIG_S3C64XX |
| 479 | return 0; |
| 480 | #endif |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 481 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 482 | if (ONENAND_IS_2PLANE(this)) |
| 483 | blockpage = onenand_get_2x_blockpage(mtd, addr); |
| 484 | else |
| 485 | blockpage = (int) (addr >> this->page_shift); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 486 | |
| 487 | /* Is there valid data? */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 488 | i = ONENAND_CURRENT_BUFFERRAM(this); |
| 489 | if (this->bufferram[i].blockpage == blockpage) |
| 490 | found = 1; |
| 491 | else { |
| 492 | /* Check another BufferRAM */ |
| 493 | i = ONENAND_NEXT_BUFFERRAM(this); |
| 494 | if (this->bufferram[i].blockpage == blockpage) { |
| 495 | ONENAND_SET_NEXT_BUFFERRAM(this); |
| 496 | found = 1; |
| 497 | } |
| 498 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 499 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 500 | if (found && ONENAND_IS_DDP(this)) { |
| 501 | /* Select DataRAM for DDP */ |
| 502 | int block = (int) (addr >> this->erase_shift); |
| 503 | int value = onenand_bufferram_address(this, block); |
| 504 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 505 | } |
| 506 | |
| 507 | return found; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * onenand_update_bufferram - [GENERIC] Update BufferRAM information |
| 512 | * @param mtd MTD data structure |
| 513 | * @param addr address to update |
| 514 | * @param valid valid flag |
| 515 | * |
| 516 | * Update BufferRAM information |
| 517 | */ |
| 518 | static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr, |
| 519 | int valid) |
| 520 | { |
| 521 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 522 | int blockpage; |
| 523 | unsigned int i; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 524 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 525 | if (ONENAND_IS_2PLANE(this)) |
| 526 | blockpage = onenand_get_2x_blockpage(mtd, addr); |
| 527 | else |
| 528 | blockpage = (int)(addr >> this->page_shift); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 529 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 530 | /* Invalidate another BufferRAM */ |
| 531 | i = ONENAND_NEXT_BUFFERRAM(this); |
| 532 | if (this->bufferram[i].blockpage == blockpage) |
| 533 | this->bufferram[i].blockpage = -1; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 534 | |
| 535 | /* Update BufferRAM */ |
| 536 | i = ONENAND_CURRENT_BUFFERRAM(this); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 537 | if (valid) |
| 538 | this->bufferram[i].blockpage = blockpage; |
| 539 | else |
| 540 | this->bufferram[i].blockpage = -1; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /** |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 546 | * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information |
| 547 | * @param mtd MTD data structure |
| 548 | * @param addr start address to invalidate |
| 549 | * @param len length to invalidate |
| 550 | * |
| 551 | * Invalidate BufferRAM information |
| 552 | */ |
| 553 | static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 554 | unsigned int len) |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 555 | { |
| 556 | struct onenand_chip *this = mtd->priv; |
| 557 | int i; |
| 558 | loff_t end_addr = addr + len; |
| 559 | |
| 560 | /* Invalidate BufferRAM */ |
| 561 | for (i = 0; i < MAX_BUFFERRAM; i++) { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 562 | loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift; |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 563 | |
| 564 | if (buf_addr >= addr && buf_addr < end_addr) |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 565 | this->bufferram[i].blockpage = -1; |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 570 | * onenand_get_device - [GENERIC] Get chip for selected access |
| 571 | * @param mtd MTD device structure |
| 572 | * @param new_state the state which is requested |
| 573 | * |
| 574 | * Get the device and lock it for exclusive access |
| 575 | */ |
| 576 | static void onenand_get_device(struct mtd_info *mtd, int new_state) |
| 577 | { |
| 578 | /* Do nothing */ |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * onenand_release_device - [GENERIC] release chip |
| 583 | * @param mtd MTD device structure |
| 584 | * |
| 585 | * Deselect, release chip lock and wake up anyone waiting on the device |
| 586 | */ |
| 587 | static void onenand_release_device(struct mtd_info *mtd) |
| 588 | { |
| 589 | /* Do nothing */ |
| 590 | } |
| 591 | |
| 592 | /** |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 593 | * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer |
| 594 | * @param mtd MTD device structure |
| 595 | * @param buf destination address |
| 596 | * @param column oob offset to read from |
| 597 | * @param thislen oob length to read |
| 598 | */ |
| 599 | static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, |
| 600 | int column, int thislen) |
| 601 | { |
| 602 | struct onenand_chip *this = mtd->priv; |
| 603 | struct nand_oobfree *free; |
| 604 | int readcol = column; |
| 605 | int readend = column + thislen; |
| 606 | int lastgap = 0; |
| 607 | unsigned int i; |
| 608 | uint8_t *oob_buf = this->oob_buf; |
| 609 | |
| 610 | free = this->ecclayout->oobfree; |
| 611 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) { |
| 612 | if (readcol >= lastgap) |
| 613 | readcol += free->offset - lastgap; |
| 614 | if (readend >= lastgap) |
| 615 | readend += free->offset - lastgap; |
| 616 | lastgap = free->offset + free->length; |
| 617 | } |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 618 | this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 619 | free = this->ecclayout->oobfree; |
| 620 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) { |
| 621 | int free_end = free->offset + free->length; |
| 622 | if (free->offset < readend && free_end > readcol) { |
| 623 | int st = max_t(int,free->offset,readcol); |
| 624 | int ed = min_t(int,free_end,readend); |
| 625 | int n = ed - st; |
| 626 | memcpy(buf, oob_buf + st, n); |
| 627 | buf += n; |
| 628 | } else if (column == 0) |
| 629 | break; |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 636 | * @param mtd MTD device structure |
| 637 | * @param from offset to read from |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 638 | * @param ops oob operation description structure |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 639 | * |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 640 | * OneNAND read main and/or out-of-band data |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 641 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 642 | static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from, |
| 643 | struct mtd_oob_ops *ops) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 644 | { |
| 645 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 646 | struct mtd_ecc_stats stats; |
| 647 | size_t len = ops->len; |
| 648 | size_t ooblen = ops->ooblen; |
| 649 | u_char *buf = ops->datbuf; |
| 650 | u_char *oobbuf = ops->oobbuf; |
| 651 | int read = 0, column, thislen; |
| 652 | int oobread = 0, oobcolumn, thisooblen, oobsize; |
| 653 | int ret = 0, boundary = 0; |
| 654 | int writesize = this->writesize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 655 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 656 | MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 657 | |
| 658 | if (ops->mode == MTD_OOB_AUTO) |
| 659 | oobsize = this->ecclayout->oobavail; |
| 660 | else |
| 661 | oobsize = mtd->oobsize; |
| 662 | |
| 663 | oobcolumn = from & (mtd->oobsize - 1); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 664 | |
| 665 | /* Do not allow reads past end of device */ |
| 666 | if ((from + len) > mtd->size) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 667 | printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n"); |
| 668 | ops->retlen = 0; |
| 669 | ops->oobretlen = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 670 | return -EINVAL; |
| 671 | } |
| 672 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 673 | stats = mtd->ecc_stats; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 674 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 675 | /* Read-while-load method */ |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 676 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 677 | /* Do first load to bufferRAM */ |
| 678 | if (read < len) { |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 679 | if (!onenand_check_bufferram(mtd, from)) { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 680 | this->main_buf = buf; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 681 | this->command(mtd, ONENAND_CMD_READ, from, writesize); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 682 | ret = this->wait(mtd, FL_READING); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 683 | onenand_update_bufferram(mtd, from, !ret); |
| 684 | if (ret == -EBADMSG) |
| 685 | ret = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 686 | } |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 687 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 688 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 689 | thislen = min_t(int, writesize, len - read); |
| 690 | column = from & (writesize - 1); |
| 691 | if (column + thislen > writesize) |
| 692 | thislen = writesize - column; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 693 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 694 | while (!ret) { |
| 695 | /* If there is more to load then start next load */ |
| 696 | from += thislen; |
| 697 | if (read + thislen < len) { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 698 | this->main_buf = buf + thislen; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 699 | this->command(mtd, ONENAND_CMD_READ, from, writesize); |
| 700 | /* |
| 701 | * Chip boundary handling in DDP |
| 702 | * Now we issued chip 1 read and pointed chip 1 |
| 703 | * bufferam so we have to point chip 0 bufferam. |
| 704 | */ |
| 705 | if (ONENAND_IS_DDP(this) && |
| 706 | unlikely(from == (this->chipsize >> 1))) { |
| 707 | this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2); |
| 708 | boundary = 1; |
| 709 | } else |
| 710 | boundary = 0; |
| 711 | ONENAND_SET_PREV_BUFFERRAM(this); |
| 712 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 713 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 714 | /* While load is going, read from last bufferRAM */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 715 | this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 716 | |
| 717 | /* Read oob area if needed */ |
| 718 | if (oobbuf) { |
| 719 | thisooblen = oobsize - oobcolumn; |
| 720 | thisooblen = min_t(int, thisooblen, ooblen - oobread); |
| 721 | |
| 722 | if (ops->mode == MTD_OOB_AUTO) |
| 723 | onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen); |
| 724 | else |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 725 | this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 726 | oobread += thisooblen; |
| 727 | oobbuf += thisooblen; |
| 728 | oobcolumn = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 729 | } |
| 730 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 731 | /* See if we are done */ |
| 732 | read += thislen; |
| 733 | if (read == len) |
| 734 | break; |
| 735 | /* Set up for next read from bufferRAM */ |
| 736 | if (unlikely(boundary)) |
| 737 | this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2); |
| 738 | ONENAND_SET_NEXT_BUFFERRAM(this); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 739 | buf += thislen; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 740 | thislen = min_t(int, writesize, len - read); |
| 741 | column = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 742 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 743 | /* Now wait for load */ |
| 744 | ret = this->wait(mtd, FL_READING); |
| 745 | onenand_update_bufferram(mtd, from, !ret); |
| 746 | if (ret == -EBADMSG) |
| 747 | ret = 0; |
| 748 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 749 | |
| 750 | /* |
| 751 | * Return success, if no ECC failures, else -EBADMSG |
| 752 | * fs driver will take care of that, because |
| 753 | * retlen == desired len and result == -EBADMSG |
| 754 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 755 | ops->retlen = read; |
| 756 | ops->oobretlen = oobread; |
| 757 | |
| 758 | if (ret) |
| 759 | return ret; |
| 760 | |
| 761 | if (mtd->ecc_stats.failed - stats.failed) |
| 762 | return -EBADMSG; |
| 763 | |
| 764 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 768 | * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band |
| 769 | * @param mtd MTD device structure |
| 770 | * @param from offset to read from |
| 771 | * @param ops oob operation description structure |
| 772 | * |
| 773 | * OneNAND read out-of-band data from the spare area |
| 774 | */ |
| 775 | static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from, |
| 776 | struct mtd_oob_ops *ops) |
| 777 | { |
| 778 | struct onenand_chip *this = mtd->priv; |
| 779 | struct mtd_ecc_stats stats; |
| 780 | int read = 0, thislen, column, oobsize; |
| 781 | size_t len = ops->ooblen; |
| 782 | mtd_oob_mode_t mode = ops->mode; |
| 783 | u_char *buf = ops->oobbuf; |
| 784 | int ret = 0; |
| 785 | |
| 786 | from += ops->ooboffs; |
| 787 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 788 | MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 789 | |
| 790 | /* Initialize return length value */ |
| 791 | ops->oobretlen = 0; |
| 792 | |
| 793 | if (mode == MTD_OOB_AUTO) |
| 794 | oobsize = this->ecclayout->oobavail; |
| 795 | else |
| 796 | oobsize = mtd->oobsize; |
| 797 | |
| 798 | column = from & (mtd->oobsize - 1); |
| 799 | |
| 800 | if (unlikely(column >= oobsize)) { |
| 801 | printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n"); |
| 802 | return -EINVAL; |
| 803 | } |
| 804 | |
| 805 | /* Do not allow reads past end of device */ |
| 806 | if (unlikely(from >= mtd->size || |
| 807 | column + len > ((mtd->size >> this->page_shift) - |
| 808 | (from >> this->page_shift)) * oobsize)) { |
| 809 | printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n"); |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | |
| 813 | stats = mtd->ecc_stats; |
| 814 | |
| 815 | while (read < len) { |
| 816 | thislen = oobsize - column; |
| 817 | thislen = min_t(int, thislen, len); |
| 818 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 819 | this->spare_buf = buf; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 820 | this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize); |
| 821 | |
| 822 | onenand_update_bufferram(mtd, from, 0); |
| 823 | |
| 824 | ret = this->wait(mtd, FL_READING); |
| 825 | if (ret && ret != -EBADMSG) { |
| 826 | printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret); |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | if (mode == MTD_OOB_AUTO) |
| 831 | onenand_transfer_auto_oob(mtd, buf, column, thislen); |
| 832 | else |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 833 | this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 834 | |
| 835 | read += thislen; |
| 836 | |
| 837 | if (read == len) |
| 838 | break; |
| 839 | |
| 840 | buf += thislen; |
| 841 | |
| 842 | /* Read more? */ |
| 843 | if (read < len) { |
| 844 | /* Page size */ |
| 845 | from += mtd->writesize; |
| 846 | column = 0; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | ops->oobretlen = read; |
| 851 | |
| 852 | if (ret) |
| 853 | return ret; |
| 854 | |
| 855 | if (mtd->ecc_stats.failed - stats.failed) |
| 856 | return -EBADMSG; |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 862 | * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc |
| 863 | * @param mtd MTD device structure |
| 864 | * @param from offset to read from |
| 865 | * @param len number of bytes to read |
| 866 | * @param retlen pointer to variable to store the number of read bytes |
| 867 | * @param buf the databuffer to put data |
| 868 | * |
| 869 | * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL |
| 870 | */ |
| 871 | int onenand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 872 | size_t * retlen, u_char * buf) |
| 873 | { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 874 | struct mtd_oob_ops ops = { |
| 875 | .len = len, |
| 876 | .ooblen = 0, |
| 877 | .datbuf = buf, |
| 878 | .oobbuf = NULL, |
| 879 | }; |
| 880 | int ret; |
| 881 | |
| 882 | onenand_get_device(mtd, FL_READING); |
| 883 | ret = onenand_read_ops_nolock(mtd, from, &ops); |
| 884 | onenand_release_device(mtd); |
| 885 | |
| 886 | *retlen = ops.retlen; |
| 887 | return ret; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /** |
| 891 | * onenand_read_oob - [MTD Interface] OneNAND read out-of-band |
| 892 | * @param mtd MTD device structure |
| 893 | * @param from offset to read from |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 894 | * @param ops oob operations description structure |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 895 | * |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 896 | * OneNAND main and/or out-of-band |
| 897 | */ |
| 898 | int onenand_read_oob(struct mtd_info *mtd, loff_t from, |
| 899 | struct mtd_oob_ops *ops) |
| 900 | { |
| 901 | int ret; |
| 902 | |
| 903 | switch (ops->mode) { |
| 904 | case MTD_OOB_PLACE: |
| 905 | case MTD_OOB_AUTO: |
| 906 | break; |
| 907 | case MTD_OOB_RAW: |
| 908 | /* Not implemented yet */ |
| 909 | default: |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
| 913 | onenand_get_device(mtd, FL_READING); |
| 914 | if (ops->datbuf) |
| 915 | ret = onenand_read_ops_nolock(mtd, from, ops); |
| 916 | else |
| 917 | ret = onenand_read_oob_nolock(mtd, from, ops); |
| 918 | onenand_release_device(mtd); |
| 919 | |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * onenand_bbt_wait - [DEFAULT] wait until the command is done |
| 925 | * @param mtd MTD device structure |
| 926 | * @param state state to select the max. timeout value |
| 927 | * |
| 928 | * Wait for command done. |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 929 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 930 | static int onenand_bbt_wait(struct mtd_info *mtd, int state) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 931 | { |
| 932 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 933 | unsigned int flags = ONENAND_INT_MASTER; |
| 934 | unsigned int interrupt; |
| 935 | unsigned int ctrl; |
| 936 | |
| 937 | while (1) { |
| 938 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 939 | if (interrupt & flags) |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | /* To get correct interrupt status in timeout case */ |
| 944 | interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT); |
| 945 | ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS); |
| 946 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 947 | if (interrupt & ONENAND_INT_READ) { |
| 948 | int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS); |
| 949 | if (ecc & ONENAND_ECC_2BIT_ALL) |
| 950 | return ONENAND_BBT_READ_ERROR; |
| 951 | } else { |
| 952 | printk(KERN_ERR "onenand_bbt_wait: read timeout!" |
| 953 | "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt); |
| 954 | return ONENAND_BBT_READ_FATAL_ERROR; |
| 955 | } |
| 956 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 957 | /* Initial bad block case: 0x2400 or 0x0400 */ |
| 958 | if (ctrl & ONENAND_CTRL_ERROR) { |
| 959 | printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl); |
| 960 | return ONENAND_BBT_READ_ERROR; |
| 961 | } |
| 962 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan |
| 968 | * @param mtd MTD device structure |
| 969 | * @param from offset to read from |
| 970 | * @param ops oob operation description structure |
| 971 | * |
| 972 | * OneNAND read out-of-band data from the spare area for bbt scan |
| 973 | */ |
| 974 | int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, |
| 975 | struct mtd_oob_ops *ops) |
| 976 | { |
| 977 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 978 | int read = 0, thislen, column; |
| 979 | int ret = 0; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 980 | size_t len = ops->ooblen; |
| 981 | u_char *buf = ops->oobbuf; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 982 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 983 | MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 984 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 985 | /* Initialize return value */ |
| 986 | ops->oobretlen = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 987 | |
| 988 | /* Do not allow reads past end of device */ |
| 989 | if (unlikely((from + len) > mtd->size)) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 990 | printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n"); |
| 991 | return ONENAND_BBT_READ_FATAL_ERROR; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* Grab the lock and see if the device is available */ |
| 995 | onenand_get_device(mtd, FL_READING); |
| 996 | |
| 997 | column = from & (mtd->oobsize - 1); |
| 998 | |
| 999 | while (read < len) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1000 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1001 | thislen = mtd->oobsize - column; |
| 1002 | thislen = min_t(int, thislen, len); |
| 1003 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1004 | this->spare_buf = buf; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1005 | this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize); |
| 1006 | |
| 1007 | onenand_update_bufferram(mtd, from, 0); |
| 1008 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1009 | ret = this->bbt_wait(mtd, FL_READING); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1010 | if (ret) |
| 1011 | break; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1012 | |
Kyungmin Park | fee18b9 | 2009-07-21 11:58:04 +0900 | [diff] [blame] | 1013 | this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1014 | read += thislen; |
| 1015 | if (read == len) |
| 1016 | break; |
| 1017 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1018 | buf += thislen; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1019 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1020 | /* Read more? */ |
| 1021 | if (read < len) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1022 | /* Update Page size */ |
| 1023 | from += this->writesize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1024 | column = 0; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* Deselect and wake up anyone waiting on the device */ |
| 1029 | onenand_release_device(mtd); |
| 1030 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1031 | ops->oobretlen = read; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1032 | return ret; |
| 1033 | } |
| 1034 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1035 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1036 | #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE |
| 1037 | /** |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1038 | * onenand_verify_oob - [GENERIC] verify the oob contents after a write |
| 1039 | * @param mtd MTD device structure |
| 1040 | * @param buf the databuffer to verify |
| 1041 | * @param to offset to read from |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1042 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1043 | static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1044 | { |
| 1045 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1046 | u_char *oob_buf = this->oob_buf; |
| 1047 | int status, i; |
| 1048 | |
| 1049 | this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize); |
| 1050 | onenand_update_bufferram(mtd, to, 0); |
| 1051 | status = this->wait(mtd, FL_READING); |
| 1052 | if (status) |
| 1053 | return status; |
| 1054 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1055 | this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1056 | for (i = 0; i < mtd->oobsize; i++) |
| 1057 | if (buf[i] != 0xFF && buf[i] != oob_buf[i]) |
| 1058 | return -EBADMSG; |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * onenand_verify - [GENERIC] verify the chip contents after a write |
| 1065 | * @param mtd MTD device structure |
| 1066 | * @param buf the databuffer to verify |
| 1067 | * @param addr offset to read from |
| 1068 | * @param len number of bytes to read and compare |
| 1069 | */ |
| 1070 | static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len) |
| 1071 | { |
| 1072 | struct onenand_chip *this = mtd->priv; |
| 1073 | void __iomem *dataram; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1074 | int ret = 0; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1075 | int thislen, column; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1076 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1077 | while (len != 0) { |
| 1078 | thislen = min_t(int, this->writesize, len); |
| 1079 | column = addr & (this->writesize - 1); |
| 1080 | if (column + thislen > this->writesize) |
| 1081 | thislen = this->writesize - column; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1082 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1083 | this->command(mtd, ONENAND_CMD_READ, addr, this->writesize); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1084 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1085 | onenand_update_bufferram(mtd, addr, 0); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1086 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1087 | ret = this->wait(mtd, FL_READING); |
| 1088 | if (ret) |
| 1089 | return ret; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1090 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1091 | onenand_update_bufferram(mtd, addr, 1); |
| 1092 | |
| 1093 | dataram = this->base + ONENAND_DATARAM; |
| 1094 | dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM); |
| 1095 | |
| 1096 | if (memcmp(buf, dataram + column, thislen)) |
| 1097 | return -EBADMSG; |
| 1098 | |
| 1099 | len -= thislen; |
| 1100 | buf += thislen; |
| 1101 | addr += thislen; |
| 1102 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | #else |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1107 | #define onenand_verify(...) (0) |
| 1108 | #define onenand_verify_oob(...) (0) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Stefan Roese | db7ce8c | 2008-12-02 11:06:47 +0100 | [diff] [blame] | 1111 | #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1112 | |
| 1113 | /** |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1114 | * onenand_fill_auto_oob - [Internal] oob auto-placement transfer |
| 1115 | * @param mtd MTD device structure |
| 1116 | * @param oob_buf oob buffer |
| 1117 | * @param buf source address |
| 1118 | * @param column oob offset to write to |
| 1119 | * @param thislen oob length to write |
| 1120 | */ |
| 1121 | static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf, |
| 1122 | const u_char *buf, int column, int thislen) |
| 1123 | { |
| 1124 | struct onenand_chip *this = mtd->priv; |
| 1125 | struct nand_oobfree *free; |
| 1126 | int writecol = column; |
| 1127 | int writeend = column + thislen; |
| 1128 | int lastgap = 0; |
| 1129 | unsigned int i; |
| 1130 | |
| 1131 | free = this->ecclayout->oobfree; |
| 1132 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) { |
| 1133 | if (writecol >= lastgap) |
| 1134 | writecol += free->offset - lastgap; |
| 1135 | if (writeend >= lastgap) |
| 1136 | writeend += free->offset - lastgap; |
| 1137 | lastgap = free->offset + free->length; |
| 1138 | } |
| 1139 | free = this->ecclayout->oobfree; |
| 1140 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) { |
| 1141 | int free_end = free->offset + free->length; |
| 1142 | if (free->offset < writeend && free_end > writecol) { |
| 1143 | int st = max_t(int,free->offset,writecol); |
| 1144 | int ed = min_t(int,free_end,writeend); |
| 1145 | int n = ed - st; |
| 1146 | memcpy(oob_buf + st, buf, n); |
| 1147 | buf += n; |
| 1148 | } else if (column == 0) |
| 1149 | break; |
| 1150 | } |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band |
| 1156 | * @param mtd MTD device structure |
| 1157 | * @param to offset to write to |
| 1158 | * @param ops oob operation description structure |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1159 | * |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1160 | * Write main and/or oob with ECC |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1161 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1162 | static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to, |
| 1163 | struct mtd_oob_ops *ops) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1164 | { |
| 1165 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1166 | int written = 0, column, thislen, subpage; |
| 1167 | int oobwritten = 0, oobcolumn, thisooblen, oobsize; |
| 1168 | size_t len = ops->len; |
| 1169 | size_t ooblen = ops->ooblen; |
| 1170 | const u_char *buf = ops->datbuf; |
| 1171 | const u_char *oob = ops->oobbuf; |
| 1172 | u_char *oobbuf; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1173 | int ret = 0; |
| 1174 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1175 | MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1176 | |
| 1177 | /* Initialize retlen, in case of early exit */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1178 | ops->retlen = 0; |
| 1179 | ops->oobretlen = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1180 | |
| 1181 | /* Do not allow writes past end of device */ |
| 1182 | if (unlikely((to + len) > mtd->size)) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1183 | printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1184 | return -EINVAL; |
| 1185 | } |
| 1186 | |
| 1187 | /* Reject writes, which are not page aligned */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1188 | if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) { |
| 1189 | printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1190 | return -EINVAL; |
| 1191 | } |
| 1192 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1193 | if (ops->mode == MTD_OOB_AUTO) |
| 1194 | oobsize = this->ecclayout->oobavail; |
| 1195 | else |
| 1196 | oobsize = mtd->oobsize; |
| 1197 | |
| 1198 | oobcolumn = to & (mtd->oobsize - 1); |
| 1199 | |
| 1200 | column = to & (mtd->writesize - 1); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1201 | |
| 1202 | /* Loop until all data write */ |
| 1203 | while (written < len) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1204 | u_char *wbuf = (u_char *) buf; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1205 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1206 | thislen = min_t(int, mtd->writesize - column, len - written); |
| 1207 | thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1208 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1209 | this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1210 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1211 | /* Partial page write */ |
| 1212 | subpage = thislen < mtd->writesize; |
| 1213 | if (subpage) { |
| 1214 | memset(this->page_buf, 0xff, mtd->writesize); |
| 1215 | memcpy(this->page_buf + column, buf, thislen); |
| 1216 | wbuf = this->page_buf; |
| 1217 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1218 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1219 | this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1220 | |
| 1221 | if (oob) { |
| 1222 | oobbuf = this->oob_buf; |
| 1223 | |
| 1224 | /* We send data to spare ram with oobsize |
| 1225 | * * to prevent byte access */ |
| 1226 | memset(oobbuf, 0xff, mtd->oobsize); |
| 1227 | if (ops->mode == MTD_OOB_AUTO) |
| 1228 | onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen); |
| 1229 | else |
| 1230 | memcpy(oobbuf + oobcolumn, oob, thisooblen); |
| 1231 | |
| 1232 | oobwritten += thisooblen; |
| 1233 | oob += thisooblen; |
| 1234 | oobcolumn = 0; |
| 1235 | } else |
| 1236 | oobbuf = (u_char *) ffchars; |
| 1237 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1238 | this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1239 | |
| 1240 | this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1241 | |
| 1242 | ret = this->wait(mtd, FL_WRITING); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1243 | |
| 1244 | /* In partial page write we don't update bufferram */ |
| 1245 | onenand_update_bufferram(mtd, to, !ret && !subpage); |
| 1246 | if (ONENAND_IS_2PLANE(this)) { |
| 1247 | ONENAND_SET_BUFFERRAM1(this); |
| 1248 | onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage); |
| 1249 | } |
| 1250 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1251 | if (ret) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1252 | printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1253 | break; |
| 1254 | } |
| 1255 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1256 | /* Only check verify write turn on */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1257 | ret = onenand_verify(mtd, buf, to, thislen); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1258 | if (ret) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1259 | printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1260 | break; |
| 1261 | } |
| 1262 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1263 | written += thislen; |
| 1264 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1265 | if (written == len) |
| 1266 | break; |
| 1267 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1268 | column = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1269 | to += thislen; |
| 1270 | buf += thislen; |
| 1271 | } |
| 1272 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1273 | ops->retlen = written; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1274 | |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | /** |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1279 | * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band |
| 1280 | * @param mtd MTD device structure |
| 1281 | * @param to offset to write to |
| 1282 | * @param len number of bytes to write |
| 1283 | * @param retlen pointer to variable to store the number of written bytes |
| 1284 | * @param buf the data to write |
| 1285 | * @param mode operation mode |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1286 | * |
| 1287 | * OneNAND write out-of-band |
| 1288 | */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1289 | static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to, |
| 1290 | struct mtd_oob_ops *ops) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1291 | { |
| 1292 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1293 | int column, ret = 0, oobsize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1294 | int written = 0; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1295 | u_char *oobbuf; |
| 1296 | size_t len = ops->ooblen; |
| 1297 | const u_char *buf = ops->oobbuf; |
| 1298 | mtd_oob_mode_t mode = ops->mode; |
| 1299 | |
| 1300 | to += ops->ooboffs; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1301 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1302 | MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1303 | |
| 1304 | /* Initialize retlen, in case of early exit */ |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1305 | ops->oobretlen = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1306 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1307 | if (mode == MTD_OOB_AUTO) |
| 1308 | oobsize = this->ecclayout->oobavail; |
| 1309 | else |
| 1310 | oobsize = mtd->oobsize; |
| 1311 | |
| 1312 | column = to & (mtd->oobsize - 1); |
| 1313 | |
| 1314 | if (unlikely(column >= oobsize)) { |
| 1315 | printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1316 | return -EINVAL; |
| 1317 | } |
| 1318 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1319 | /* For compatibility with NAND: Do not allow write past end of page */ |
| 1320 | if (unlikely(column + len > oobsize)) { |
| 1321 | printk(KERN_ERR "onenand_write_oob_nolock: " |
| 1322 | "Attempt to write past end of page\n"); |
| 1323 | return -EINVAL; |
| 1324 | } |
| 1325 | |
| 1326 | /* Do not allow reads past end of device */ |
| 1327 | if (unlikely(to >= mtd->size || |
| 1328 | column + len > ((mtd->size >> this->page_shift) - |
| 1329 | (to >> this->page_shift)) * oobsize)) { |
| 1330 | printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n"); |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | |
| 1334 | oobbuf = this->oob_buf; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1335 | |
| 1336 | /* Loop until all data write */ |
| 1337 | while (written < len) { |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1338 | int thislen = min_t(int, oobsize, len - written); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1339 | |
| 1340 | this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize); |
| 1341 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1342 | /* We send data to spare ram with oobsize |
| 1343 | * to prevent byte access */ |
| 1344 | memset(oobbuf, 0xff, mtd->oobsize); |
| 1345 | if (mode == MTD_OOB_AUTO) |
| 1346 | onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen); |
| 1347 | else |
| 1348 | memcpy(oobbuf + column, buf, thislen); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1349 | this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1350 | |
| 1351 | this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize); |
| 1352 | |
| 1353 | onenand_update_bufferram(mtd, to, 0); |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1354 | if (ONENAND_IS_2PLANE(this)) { |
| 1355 | ONENAND_SET_BUFFERRAM1(this); |
| 1356 | onenand_update_bufferram(mtd, to + this->writesize, 0); |
| 1357 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1358 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1359 | ret = this->wait(mtd, FL_WRITING); |
| 1360 | if (ret) { |
| 1361 | printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1362 | break; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | ret = onenand_verify_oob(mtd, oobbuf, to); |
| 1366 | if (ret) { |
| 1367 | printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret); |
| 1368 | break; |
| 1369 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1370 | |
| 1371 | written += thislen; |
| 1372 | if (written == len) |
| 1373 | break; |
| 1374 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1375 | to += mtd->writesize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1376 | buf += thislen; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1377 | column = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1378 | } |
| 1379 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1380 | ops->oobretlen = written; |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
| 1385 | /** |
| 1386 | * onenand_write - [MTD Interface] compability function for onenand_write_ecc |
| 1387 | * @param mtd MTD device structure |
| 1388 | * @param to offset to write to |
| 1389 | * @param len number of bytes to write |
| 1390 | * @param retlen pointer to variable to store the number of written bytes |
| 1391 | * @param buf the data to write |
| 1392 | * |
| 1393 | * Write with ECC |
| 1394 | */ |
| 1395 | int onenand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 1396 | size_t * retlen, const u_char * buf) |
| 1397 | { |
| 1398 | struct mtd_oob_ops ops = { |
| 1399 | .len = len, |
| 1400 | .ooblen = 0, |
| 1401 | .datbuf = (u_char *) buf, |
| 1402 | .oobbuf = NULL, |
| 1403 | }; |
| 1404 | int ret; |
| 1405 | |
| 1406 | onenand_get_device(mtd, FL_WRITING); |
| 1407 | ret = onenand_write_ops_nolock(mtd, to, &ops); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1408 | onenand_release_device(mtd); |
| 1409 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1410 | *retlen = ops.retlen; |
| 1411 | return ret; |
| 1412 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1413 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1414 | /** |
| 1415 | * onenand_write_oob - [MTD Interface] OneNAND write out-of-band |
| 1416 | * @param mtd MTD device structure |
| 1417 | * @param to offset to write to |
| 1418 | * @param ops oob operation description structure |
| 1419 | * |
| 1420 | * OneNAND write main and/or out-of-band |
| 1421 | */ |
| 1422 | int onenand_write_oob(struct mtd_info *mtd, loff_t to, |
| 1423 | struct mtd_oob_ops *ops) |
| 1424 | { |
| 1425 | int ret; |
| 1426 | |
| 1427 | switch (ops->mode) { |
| 1428 | case MTD_OOB_PLACE: |
| 1429 | case MTD_OOB_AUTO: |
| 1430 | break; |
| 1431 | case MTD_OOB_RAW: |
| 1432 | /* Not implemented yet */ |
| 1433 | default: |
| 1434 | return -EINVAL; |
| 1435 | } |
| 1436 | |
| 1437 | onenand_get_device(mtd, FL_WRITING); |
| 1438 | if (ops->datbuf) |
| 1439 | ret = onenand_write_ops_nolock(mtd, to, ops); |
| 1440 | else |
| 1441 | ret = onenand_write_oob_nolock(mtd, to, ops); |
| 1442 | onenand_release_device(mtd); |
| 1443 | |
| 1444 | return ret; |
| 1445 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1446 | } |
| 1447 | |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1448 | /** |
| 1449 | * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad |
| 1450 | * @param mtd MTD device structure |
| 1451 | * @param ofs offset from device start |
| 1452 | * @param allowbbt 1, if its allowed to access the bbt area |
| 1453 | * |
| 1454 | * Check, if the block is bad, Either by reading the bad block table or |
| 1455 | * calling of the scan function. |
| 1456 | */ |
| 1457 | static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
| 1458 | { |
| 1459 | struct onenand_chip *this = mtd->priv; |
| 1460 | struct bbm_info *bbm = this->bbm; |
| 1461 | |
| 1462 | /* Return info from the table */ |
| 1463 | return bbm->isbad_bbt(mtd, ofs, allowbbt); |
| 1464 | } |
| 1465 | |
| 1466 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1467 | /** |
| 1468 | * onenand_erase - [MTD Interface] erase block(s) |
| 1469 | * @param mtd MTD device structure |
| 1470 | * @param instr erase instruction |
| 1471 | * |
| 1472 | * Erase one ore more blocks |
| 1473 | */ |
| 1474 | int onenand_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 1475 | { |
| 1476 | struct onenand_chip *this = mtd->priv; |
| 1477 | unsigned int block_size; |
| 1478 | loff_t addr; |
| 1479 | int len; |
| 1480 | int ret = 0; |
| 1481 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1482 | MTDDEBUG (MTD_DEBUG_LEVEL3, |
| 1483 | "onenand_erase: start = 0x%08x, len = %i\n", |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1484 | (unsigned int)instr->addr, (unsigned int)instr->len); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1485 | |
| 1486 | block_size = (1 << this->erase_shift); |
| 1487 | |
| 1488 | /* Start address must align on block boundary */ |
| 1489 | if (unlikely(instr->addr & (block_size - 1))) { |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1490 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1491 | "onenand_erase: Unaligned address\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1492 | return -EINVAL; |
| 1493 | } |
| 1494 | |
| 1495 | /* Length must align on block boundary */ |
| 1496 | if (unlikely(instr->len & (block_size - 1))) { |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1497 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1498 | "onenand_erase: Length not block aligned\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1499 | return -EINVAL; |
| 1500 | } |
| 1501 | |
| 1502 | /* Do not allow erase past end of device */ |
| 1503 | if (unlikely((instr->len + instr->addr) > mtd->size)) { |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1504 | MTDDEBUG (MTD_DEBUG_LEVEL0, |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 1505 | "onenand_erase: Erase past end of device\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1506 | return -EINVAL; |
| 1507 | } |
| 1508 | |
| 1509 | instr->fail_addr = 0xffffffff; |
| 1510 | |
| 1511 | /* Grab the lock and see if the device is available */ |
| 1512 | onenand_get_device(mtd, FL_ERASING); |
| 1513 | |
| 1514 | /* Loop throught the pages */ |
| 1515 | len = instr->len; |
| 1516 | addr = instr->addr; |
| 1517 | |
| 1518 | instr->state = MTD_ERASING; |
| 1519 | |
| 1520 | while (len) { |
| 1521 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1522 | /* Check if we have a bad block, we do not erase bad blocks */ |
| 1523 | if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) { |
| 1524 | printk(KERN_WARNING "onenand_erase: attempt to erase" |
| 1525 | " a bad block at addr 0x%08x\n", |
| 1526 | (unsigned int) addr); |
| 1527 | instr->state = MTD_ERASE_FAILED; |
| 1528 | goto erase_exit; |
| 1529 | } |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1530 | |
| 1531 | this->command(mtd, ONENAND_CMD_ERASE, addr, block_size); |
| 1532 | |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1533 | onenand_invalidate_bufferram(mtd, addr, block_size); |
| 1534 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1535 | ret = this->wait(mtd, FL_ERASING); |
| 1536 | /* Check, if it is write protected */ |
| 1537 | if (ret) { |
| 1538 | if (ret == -EPERM) |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1539 | MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: " |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1540 | "Device is write protected!!!\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1541 | else |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1542 | MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: " |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1543 | "Failed erase, block %d\n", |
| 1544 | (unsigned)(addr >> this->erase_shift)); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1545 | if (ret == -EPERM) |
| 1546 | printk("onenand_erase: " |
| 1547 | "Device is write protected!!!\n"); |
| 1548 | else |
| 1549 | printk("onenand_erase: " |
| 1550 | "Failed erase, block %d\n", |
| 1551 | (unsigned)(addr >> this->erase_shift)); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1552 | instr->state = MTD_ERASE_FAILED; |
| 1553 | instr->fail_addr = addr; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1554 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1555 | goto erase_exit; |
| 1556 | } |
| 1557 | |
| 1558 | len -= block_size; |
| 1559 | addr += block_size; |
| 1560 | } |
| 1561 | |
| 1562 | instr->state = MTD_ERASE_DONE; |
| 1563 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1564 | erase_exit: |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1565 | |
| 1566 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
| 1567 | /* Do call back function */ |
| 1568 | if (!ret) |
| 1569 | mtd_erase_callback(instr); |
| 1570 | |
| 1571 | /* Deselect and wake up anyone waiting on the device */ |
| 1572 | onenand_release_device(mtd); |
| 1573 | |
| 1574 | return ret; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * onenand_sync - [MTD Interface] sync |
| 1579 | * @param mtd MTD device structure |
| 1580 | * |
| 1581 | * Sync is actually a wait for chip ready function |
| 1582 | */ |
| 1583 | void onenand_sync(struct mtd_info *mtd) |
| 1584 | { |
Scott Wood | df83c47 | 2008-06-20 12:38:57 -0500 | [diff] [blame] | 1585 | MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n"); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1586 | |
| 1587 | /* Grab the lock and see if the device is available */ |
| 1588 | onenand_get_device(mtd, FL_SYNCING); |
| 1589 | |
| 1590 | /* Release it and go back */ |
| 1591 | onenand_release_device(mtd); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad |
| 1596 | * @param mtd MTD device structure |
| 1597 | * @param ofs offset relative to mtd start |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1598 | * |
| 1599 | * Check whether the block is bad |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1600 | */ |
| 1601 | int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 1602 | { |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1603 | int ret; |
| 1604 | |
| 1605 | /* Check for invalid offset */ |
| 1606 | if (ofs > mtd->size) |
| 1607 | return -EINVAL; |
| 1608 | |
| 1609 | onenand_get_device(mtd, FL_READING); |
| 1610 | ret = onenand_block_isbad_nolock(mtd,ofs, 0); |
| 1611 | onenand_release_device(mtd); |
| 1612 | return ret; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
Kyungmin Park | 87b4950 | 2008-11-13 15:14:33 +0900 | [diff] [blame] | 1616 | * onenand_default_block_markbad - [DEFAULT] mark a block bad |
| 1617 | * @param mtd MTD device structure |
| 1618 | * @param ofs offset from device start |
| 1619 | * |
| 1620 | * This is the default implementation, which can be overridden by |
| 1621 | * a hardware specific driver. |
| 1622 | */ |
| 1623 | static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1624 | { |
| 1625 | struct onenand_chip *this = mtd->priv; |
| 1626 | struct bbm_info *bbm = this->bbm; |
| 1627 | u_char buf[2] = {0, 0}; |
| 1628 | struct mtd_oob_ops ops = { |
| 1629 | .mode = MTD_OOB_PLACE, |
| 1630 | .ooblen = 2, |
| 1631 | .oobbuf = buf, |
| 1632 | .ooboffs = 0, |
| 1633 | }; |
| 1634 | int block; |
| 1635 | |
| 1636 | /* Get block number */ |
| 1637 | block = ((int) ofs) >> bbm->bbt_erase_shift; |
| 1638 | if (bbm->bbt) |
| 1639 | bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
| 1640 | |
| 1641 | /* We write two bytes, so we dont have to mess with 16 bit access */ |
| 1642 | ofs += mtd->oobsize + (bbm->badblockpos & ~0x01); |
| 1643 | return onenand_write_oob_nolock(mtd, ofs, &ops); |
| 1644 | } |
| 1645 | |
| 1646 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1647 | * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad |
| 1648 | * @param mtd MTD device structure |
| 1649 | * @param ofs offset relative to mtd start |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1650 | * |
| 1651 | * Mark the block as bad |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1652 | */ |
| 1653 | int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1654 | { |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 1655 | struct onenand_chip *this = mtd->priv; |
| 1656 | int ret; |
| 1657 | |
| 1658 | ret = onenand_block_isbad(mtd, ofs); |
| 1659 | if (ret) { |
| 1660 | /* If it was bad already, return success and do nothing */ |
| 1661 | if (ret > 0) |
| 1662 | return 0; |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | ret = this->block_markbad(mtd, ofs); |
| 1667 | return ret; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /** |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1671 | * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s) |
| 1672 | * @param mtd MTD device structure |
| 1673 | * @param ofs offset relative to mtd start |
| 1674 | * @param len number of bytes to lock or unlock |
| 1675 | * @param cmd lock or unlock command |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1676 | * |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1677 | * Lock or unlock one or more blocks |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1678 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1679 | static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1680 | { |
| 1681 | struct onenand_chip *this = mtd->priv; |
| 1682 | int start, end, block, value, status; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1683 | int wp_status_mask; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1684 | |
| 1685 | start = ofs >> this->erase_shift; |
| 1686 | end = len >> this->erase_shift; |
| 1687 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1688 | if (cmd == ONENAND_CMD_LOCK) |
| 1689 | wp_status_mask = ONENAND_WP_LS; |
| 1690 | else |
| 1691 | wp_status_mask = ONENAND_WP_US; |
| 1692 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1693 | /* Continuous lock scheme */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1694 | if (this->options & ONENAND_HAS_CONT_LOCK) { |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1695 | /* Set start block address */ |
| 1696 | this->write_word(start, |
| 1697 | this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 1698 | /* Set end block address */ |
| 1699 | this->write_word(end - 1, |
| 1700 | this->base + ONENAND_REG_END_BLOCK_ADDRESS); |
| 1701 | /* Write unlock command */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1702 | this->command(mtd, cmd, 0, 0); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1703 | |
| 1704 | /* There's no return value */ |
| 1705 | this->wait(mtd, FL_UNLOCKING); |
| 1706 | |
| 1707 | /* Sanity check */ |
| 1708 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 1709 | & ONENAND_CTRL_ONGO) |
| 1710 | continue; |
| 1711 | |
| 1712 | /* Check lock status */ |
| 1713 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
| 1714 | if (!(status & ONENAND_WP_US)) |
| 1715 | printk(KERN_ERR "wp status = 0x%x\n", status); |
| 1716 | |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | /* Block lock scheme */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1721 | for (block = start; block < start + end; block++) { |
| 1722 | /* Set block address */ |
| 1723 | value = onenand_block_address(this, block); |
| 1724 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1); |
| 1725 | /* Select DataRAM for DDP */ |
| 1726 | value = onenand_bufferram_address(this, block); |
| 1727 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 1728 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1729 | /* Set start block address */ |
| 1730 | this->write_word(block, |
| 1731 | this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 1732 | /* Write unlock command */ |
| 1733 | this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0); |
| 1734 | |
| 1735 | /* There's no return value */ |
| 1736 | this->wait(mtd, FL_UNLOCKING); |
| 1737 | |
| 1738 | /* Sanity check */ |
| 1739 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 1740 | & ONENAND_CTRL_ONGO) |
| 1741 | continue; |
| 1742 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1743 | /* Check lock status */ |
| 1744 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
| 1745 | if (!(status & ONENAND_WP_US)) |
| 1746 | printk(KERN_ERR "block = %d, wp status = 0x%x\n", |
| 1747 | block, status); |
| 1748 | } |
| 1749 | |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Stefan Roese | 5ed79ae | 2008-11-11 10:28:53 +0100 | [diff] [blame] | 1753 | #ifdef ONENAND_LINUX |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1754 | /** |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1755 | * onenand_lock - [MTD Interface] Lock block(s) |
| 1756 | * @param mtd MTD device structure |
| 1757 | * @param ofs offset relative to mtd start |
| 1758 | * @param len number of bytes to unlock |
| 1759 | * |
| 1760 | * Lock one or more blocks |
| 1761 | */ |
| 1762 | static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 1763 | { |
| 1764 | int ret; |
| 1765 | |
| 1766 | onenand_get_device(mtd, FL_LOCKING); |
| 1767 | ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK); |
| 1768 | onenand_release_device(mtd); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | /** |
| 1773 | * onenand_unlock - [MTD Interface] Unlock block(s) |
| 1774 | * @param mtd MTD device structure |
| 1775 | * @param ofs offset relative to mtd start |
| 1776 | * @param len number of bytes to unlock |
| 1777 | * |
| 1778 | * Unlock one or more blocks |
| 1779 | */ |
| 1780 | static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 1781 | { |
| 1782 | int ret; |
| 1783 | |
| 1784 | onenand_get_device(mtd, FL_LOCKING); |
| 1785 | ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK); |
| 1786 | onenand_release_device(mtd); |
| 1787 | return ret; |
| 1788 | } |
Stefan Roese | 5ed79ae | 2008-11-11 10:28:53 +0100 | [diff] [blame] | 1789 | #endif |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1790 | |
| 1791 | /** |
| 1792 | * onenand_check_lock_status - [OneNAND Interface] Check lock status |
| 1793 | * @param this onenand chip data structure |
| 1794 | * |
| 1795 | * Check lock status |
| 1796 | */ |
| 1797 | static int onenand_check_lock_status(struct onenand_chip *this) |
| 1798 | { |
| 1799 | unsigned int value, block, status; |
| 1800 | unsigned int end; |
| 1801 | |
| 1802 | end = this->chipsize >> this->erase_shift; |
| 1803 | for (block = 0; block < end; block++) { |
| 1804 | /* Set block address */ |
| 1805 | value = onenand_block_address(this, block); |
| 1806 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1); |
| 1807 | /* Select DataRAM for DDP */ |
| 1808 | value = onenand_bufferram_address(this, block); |
| 1809 | this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2); |
| 1810 | /* Set start block address */ |
| 1811 | this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 1812 | |
| 1813 | /* Check lock status */ |
| 1814 | status = this->read_word(this->base + ONENAND_REG_WP_STATUS); |
| 1815 | if (!(status & ONENAND_WP_US)) { |
| 1816 | printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status); |
| 1817 | return 0; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | return 1; |
| 1822 | } |
| 1823 | |
| 1824 | /** |
| 1825 | * onenand_unlock_all - [OneNAND Interface] unlock all blocks |
| 1826 | * @param mtd MTD device structure |
| 1827 | * |
| 1828 | * Unlock all blocks |
| 1829 | */ |
| 1830 | static void onenand_unlock_all(struct mtd_info *mtd) |
| 1831 | { |
| 1832 | struct onenand_chip *this = mtd->priv; |
| 1833 | loff_t ofs = 0; |
| 1834 | size_t len = this->chipsize; |
| 1835 | |
| 1836 | if (this->options & ONENAND_HAS_UNLOCK_ALL) { |
| 1837 | /* Set start block address */ |
| 1838 | this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS); |
| 1839 | /* Write unlock command */ |
| 1840 | this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0); |
| 1841 | |
| 1842 | /* There's no return value */ |
| 1843 | this->wait(mtd, FL_LOCKING); |
| 1844 | |
| 1845 | /* Sanity check */ |
| 1846 | while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS) |
| 1847 | & ONENAND_CTRL_ONGO) |
| 1848 | continue; |
| 1849 | |
| 1850 | return; |
| 1851 | |
| 1852 | /* Check lock status */ |
| 1853 | if (onenand_check_lock_status(this)) |
| 1854 | return; |
| 1855 | |
| 1856 | /* Workaround for all block unlock in DDP */ |
| 1857 | if (ONENAND_IS_DDP(this)) { |
| 1858 | /* All blocks on another chip */ |
| 1859 | ofs = this->chipsize >> 1; |
| 1860 | len = this->chipsize >> 1; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK); |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | /** |
| 1869 | * onenand_check_features - Check and set OneNAND features |
| 1870 | * @param mtd MTD data structure |
| 1871 | * |
| 1872 | * Check and set OneNAND features |
| 1873 | * - lock scheme |
| 1874 | * - two plane |
| 1875 | */ |
| 1876 | static void onenand_check_features(struct mtd_info *mtd) |
| 1877 | { |
| 1878 | struct onenand_chip *this = mtd->priv; |
| 1879 | unsigned int density, process; |
| 1880 | |
| 1881 | /* Lock scheme depends on density and process */ |
| 1882 | density = onenand_get_density(this->device_id); |
| 1883 | process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT; |
| 1884 | |
| 1885 | /* Lock scheme */ |
| 1886 | switch (density) { |
| 1887 | case ONENAND_DEVICE_DENSITY_4Gb: |
| 1888 | this->options |= ONENAND_HAS_2PLANE; |
| 1889 | |
| 1890 | case ONENAND_DEVICE_DENSITY_2Gb: |
| 1891 | /* 2Gb DDP don't have 2 plane */ |
| 1892 | if (!ONENAND_IS_DDP(this)) |
| 1893 | this->options |= ONENAND_HAS_2PLANE; |
| 1894 | this->options |= ONENAND_HAS_UNLOCK_ALL; |
| 1895 | |
| 1896 | case ONENAND_DEVICE_DENSITY_1Gb: |
| 1897 | /* A-Die has all block unlock */ |
| 1898 | if (process) |
| 1899 | this->options |= ONENAND_HAS_UNLOCK_ALL; |
| 1900 | break; |
| 1901 | |
| 1902 | default: |
| 1903 | /* Some OneNAND has continuous lock scheme */ |
| 1904 | if (!process) |
| 1905 | this->options |= ONENAND_HAS_CONT_LOCK; |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | if (this->options & ONENAND_HAS_CONT_LOCK) |
| 1910 | printk(KERN_DEBUG "Lock scheme is Continuous Lock\n"); |
| 1911 | if (this->options & ONENAND_HAS_UNLOCK_ALL) |
| 1912 | printk(KERN_DEBUG "Chip support all block unlock\n"); |
| 1913 | if (this->options & ONENAND_HAS_2PLANE) |
| 1914 | printk(KERN_DEBUG "Chip has 2 plane\n"); |
| 1915 | } |
| 1916 | |
| 1917 | /** |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1918 | * onenand_print_device_info - Print device ID |
| 1919 | * @param device device ID |
| 1920 | * |
| 1921 | * Print device ID |
| 1922 | */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1923 | char *onenand_print_device_info(int device, int version) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1924 | { |
| 1925 | int vcc, demuxed, ddp, density; |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 1926 | char *dev_info = malloc(80); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1927 | char *p = dev_info; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1928 | |
| 1929 | vcc = device & ONENAND_DEVICE_VCC_MASK; |
| 1930 | demuxed = device & ONENAND_DEVICE_IS_DEMUX; |
| 1931 | ddp = device & ONENAND_DEVICE_IS_DDP; |
| 1932 | density = device >> ONENAND_DEVICE_DENSITY_SHIFT; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1933 | p += sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)", |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1934 | demuxed ? "" : "Muxed ", |
| 1935 | ddp ? "(DDP)" : "", |
| 1936 | (16 << density), vcc ? "2.65/3.3" : "1.8", device); |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 1937 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1938 | sprintf(p, "\nOneNAND version = 0x%04x", version); |
| 1939 | printk("%s\n", dev_info); |
| 1940 | |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 1941 | return dev_info; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | static const struct onenand_manufacturers onenand_manuf_ids[] = { |
| 1945 | {ONENAND_MFR_SAMSUNG, "Samsung"}, |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1946 | }; |
| 1947 | |
| 1948 | /** |
| 1949 | * onenand_check_maf - Check manufacturer ID |
| 1950 | * @param manuf manufacturer ID |
| 1951 | * |
| 1952 | * Check manufacturer ID |
| 1953 | */ |
| 1954 | static int onenand_check_maf(int manuf) |
| 1955 | { |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1956 | int size = ARRAY_SIZE(onenand_manuf_ids); |
| 1957 | char *name; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1958 | int i; |
| 1959 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1960 | for (i = 0; size; i++) |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1961 | if (manuf == onenand_manuf_ids[i].id) |
| 1962 | break; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1963 | |
| 1964 | if (i < size) |
| 1965 | name = onenand_manuf_ids[i].name; |
| 1966 | else |
| 1967 | name = "Unknown"; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1968 | |
| 1969 | #ifdef ONENAND_DEBUG |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1970 | printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1971 | #endif |
| 1972 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1973 | return i == size; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /** |
| 1977 | * onenand_probe - [OneNAND Interface] Probe the OneNAND device |
| 1978 | * @param mtd MTD device structure |
| 1979 | * |
| 1980 | * OneNAND detection method: |
| 1981 | * Compare the the values from command with ones from register |
| 1982 | */ |
| 1983 | static int onenand_probe(struct mtd_info *mtd) |
| 1984 | { |
| 1985 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1986 | int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1987 | int density; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 1988 | int syscfg; |
| 1989 | |
| 1990 | /* Save system configuration 1 */ |
| 1991 | syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1); |
| 1992 | /* Clear Sync. Burst Read mode to read BootRAM */ |
| 1993 | this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 1994 | |
| 1995 | /* Send the command for reading device ID from BootRAM */ |
| 1996 | this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM); |
| 1997 | |
| 1998 | /* Read manufacturer and device IDs from BootRAM */ |
| 1999 | bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0); |
| 2000 | bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2); |
| 2001 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2002 | /* Reset OneNAND to read default register values */ |
| 2003 | this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM); |
| 2004 | |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2005 | /* Wait reset */ |
| 2006 | this->wait(mtd, FL_RESETING); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2007 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2008 | /* Restore system configuration 1 */ |
| 2009 | this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1); |
| 2010 | |
| 2011 | /* Check manufacturer ID */ |
| 2012 | if (onenand_check_maf(bram_maf_id)) |
| 2013 | return -ENXIO; |
| 2014 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2015 | /* Read manufacturer and device IDs from Register */ |
| 2016 | maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID); |
| 2017 | dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID); |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2018 | ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2019 | |
| 2020 | /* Check OneNAND device */ |
| 2021 | if (maf_id != bram_maf_id || dev_id != bram_dev_id) |
| 2022 | return -ENXIO; |
| 2023 | |
Kyungmin Park | 05d2318 | 2008-03-17 08:54:06 +0900 | [diff] [blame] | 2024 | /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */ |
| 2025 | if (dev_id & (1 << 9)) { |
| 2026 | printk("Not yet support Flex-OneNAND\n"); |
| 2027 | return -ENXIO; |
| 2028 | } |
| 2029 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2030 | /* Flash device information */ |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2031 | mtd->name = onenand_print_device_info(dev_id, ver_id); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2032 | this->device_id = dev_id; |
Stefan Roese | 7c9aafe | 2008-11-11 10:29:09 +0100 | [diff] [blame] | 2033 | this->version_id = ver_id; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2034 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2035 | density = onenand_get_density(dev_id); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2036 | this->chipsize = (16 << density) << 20; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2037 | /* Set density mask. it is used for DDP */ |
| 2038 | if (ONENAND_IS_DDP(this)) |
| 2039 | this->density_mask = (1 << (density + 6)); |
| 2040 | else |
| 2041 | this->density_mask = 0; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2042 | |
| 2043 | /* OneNAND page size & block size */ |
| 2044 | /* The data buffer size is equal to page size */ |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2045 | mtd->writesize = |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2046 | this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE); |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2047 | mtd->oobsize = mtd->writesize >> 5; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2048 | /* Pagers per block is always 64 in OneNAND */ |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2049 | mtd->erasesize = mtd->writesize << 6; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2050 | |
| 2051 | this->erase_shift = ffs(mtd->erasesize) - 1; |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2052 | this->page_shift = ffs(mtd->writesize) - 1; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2053 | this->ppb_shift = (this->erase_shift - this->page_shift); |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2054 | this->page_mask = (mtd->erasesize / mtd->writesize) - 1; |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 2055 | /* It's real page size */ |
| 2056 | this->writesize = mtd->writesize; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2057 | |
| 2058 | /* REVIST: Multichip handling */ |
| 2059 | |
| 2060 | mtd->size = this->chipsize; |
| 2061 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2062 | /* Check OneNAND features */ |
| 2063 | onenand_check_features(mtd); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2064 | |
Kyungmin Park | 396b0c4 | 2008-08-13 09:11:02 +0900 | [diff] [blame] | 2065 | mtd->flags = MTD_CAP_NANDFLASH; |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 2066 | mtd->erase = onenand_erase; |
| 2067 | mtd->read = onenand_read; |
| 2068 | mtd->write = onenand_write; |
Fathi BOUDRA | 95feb70 | 2008-08-06 10:06:20 +0200 | [diff] [blame] | 2069 | mtd->read_oob = onenand_read_oob; |
| 2070 | mtd->write_oob = onenand_write_oob; |
| 2071 | mtd->sync = onenand_sync; |
| 2072 | mtd->block_isbad = onenand_block_isbad; |
| 2073 | mtd->block_markbad = onenand_block_markbad; |
| 2074 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | /** |
| 2079 | * onenand_scan - [OneNAND Interface] Scan for the OneNAND device |
| 2080 | * @param mtd MTD device structure |
| 2081 | * @param maxchips Number of chips to scan for |
| 2082 | * |
| 2083 | * This fills out all the not initialized function pointers |
| 2084 | * with the defaults. |
| 2085 | * The flash ID is read and the mtd/chip structures are |
| 2086 | * filled with the appropriate values. |
| 2087 | */ |
| 2088 | int onenand_scan(struct mtd_info *mtd, int maxchips) |
| 2089 | { |
Stefan Roese | db7ce8c | 2008-12-02 11:06:47 +0100 | [diff] [blame] | 2090 | int i; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2091 | struct onenand_chip *this = mtd->priv; |
| 2092 | |
| 2093 | if (!this->read_word) |
| 2094 | this->read_word = onenand_readw; |
| 2095 | if (!this->write_word) |
| 2096 | this->write_word = onenand_writew; |
| 2097 | |
| 2098 | if (!this->command) |
| 2099 | this->command = onenand_command; |
| 2100 | if (!this->wait) |
| 2101 | this->wait = onenand_wait; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2102 | if (!this->bbt_wait) |
| 2103 | this->bbt_wait = onenand_bbt_wait; |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2104 | |
| 2105 | if (!this->read_bufferram) |
| 2106 | this->read_bufferram = onenand_read_bufferram; |
| 2107 | if (!this->write_bufferram) |
| 2108 | this->write_bufferram = onenand_write_bufferram; |
| 2109 | |
Kyungmin Park | 87b4950 | 2008-11-13 15:14:33 +0900 | [diff] [blame] | 2110 | if (!this->block_markbad) |
| 2111 | this->block_markbad = onenand_default_block_markbad; |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2112 | if (!this->scan_bbt) |
| 2113 | this->scan_bbt = onenand_default_bbt; |
| 2114 | |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2115 | if (onenand_probe(mtd)) |
| 2116 | return -ENXIO; |
| 2117 | |
| 2118 | /* Set Sync. Burst Read after probing */ |
| 2119 | if (this->mmcontrol) { |
| 2120 | printk(KERN_INFO "OneNAND Sync. Burst Read support\n"); |
| 2121 | this->read_bufferram = onenand_sync_read_bufferram; |
| 2122 | } |
| 2123 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 2124 | /* Allocate buffers, if necessary */ |
| 2125 | if (!this->page_buf) { |
| 2126 | this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL); |
| 2127 | if (!this->page_buf) { |
| 2128 | printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n"); |
| 2129 | return -ENOMEM; |
| 2130 | } |
| 2131 | this->options |= ONENAND_PAGEBUF_ALLOC; |
| 2132 | } |
| 2133 | if (!this->oob_buf) { |
| 2134 | this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL); |
| 2135 | if (!this->oob_buf) { |
| 2136 | printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n"); |
| 2137 | if (this->options & ONENAND_PAGEBUF_ALLOC) { |
| 2138 | this->options &= ~ONENAND_PAGEBUF_ALLOC; |
| 2139 | kfree(this->page_buf); |
| 2140 | } |
| 2141 | return -ENOMEM; |
| 2142 | } |
| 2143 | this->options |= ONENAND_OOBBUF_ALLOC; |
| 2144 | } |
| 2145 | |
Stefan Roese | db7ce8c | 2008-12-02 11:06:47 +0100 | [diff] [blame] | 2146 | this->state = FL_READY; |
| 2147 | |
| 2148 | /* |
| 2149 | * Allow subpage writes up to oobsize. |
| 2150 | */ |
| 2151 | switch (mtd->oobsize) { |
| 2152 | case 64: |
| 2153 | this->ecclayout = &onenand_oob_64; |
| 2154 | mtd->subpage_sft = 2; |
| 2155 | break; |
| 2156 | |
| 2157 | case 32: |
| 2158 | this->ecclayout = &onenand_oob_32; |
| 2159 | mtd->subpage_sft = 1; |
| 2160 | break; |
| 2161 | |
| 2162 | default: |
| 2163 | printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n", |
| 2164 | mtd->oobsize); |
| 2165 | mtd->subpage_sft = 0; |
| 2166 | /* To prevent kernel oops */ |
| 2167 | this->ecclayout = &onenand_oob_32; |
| 2168 | break; |
| 2169 | } |
| 2170 | |
| 2171 | this->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 2172 | |
| 2173 | /* |
| 2174 | * The number of bytes available for a client to place data into |
| 2175 | * the out of band area |
| 2176 | */ |
| 2177 | this->ecclayout->oobavail = 0; |
| 2178 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && |
| 2179 | this->ecclayout->oobfree[i].length; i++) |
| 2180 | this->ecclayout->oobavail += |
| 2181 | this->ecclayout->oobfree[i].length; |
| 2182 | mtd->oobavail = this->ecclayout->oobavail; |
| 2183 | |
| 2184 | mtd->ecclayout = this->ecclayout; |
| 2185 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2186 | /* Unlock whole block */ |
| 2187 | onenand_unlock_all(mtd); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2188 | |
Kyungmin Park | dc7a79c | 2008-11-04 09:24:07 +0900 | [diff] [blame] | 2189 | return this->scan_bbt(mtd); |
Kyungmin Park | 79e9046 | 2007-09-10 17:13:49 +0900 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /** |
| 2193 | * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device |
| 2194 | * @param mtd MTD device structure |
| 2195 | */ |
| 2196 | void onenand_release(struct mtd_info *mtd) |
| 2197 | { |
| 2198 | } |