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