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