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