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