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