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