blob: 03deabce102341881a7b430f35164eea9c455ce6 [file] [log] [blame]
Kyungmin Park79e90462007-09-10 17:13:49 +09001/*
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 Park5d7a01c2008-08-19 08:42:53 +09007 * 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 Sahaed886a22009-11-06 17:15:31 +053012 * 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 Park79e90462007-09-10 17:13:49 +090017 * 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>
Mike Frysinger11d1a092012-04-09 13:39:55 +000023#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090024#include <linux/mtd/mtd.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020025#include "linux/mtd/flashchip.h"
Kyungmin Park79e90462007-09-10 17:13:49 +090026#include <linux/mtd/onenand.h>
27
28#include <asm/io.h>
29#include <asm/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020030#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090031
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090032/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053033static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090034{
35 void *ret = dst;
36 short *d = dst;
37 const short *s = src;
38
39 len >>= 1;
40 while (len-- > 0)
41 *d++ = *s++;
42 return ret;
43}
44
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010045/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053046 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
47 * For now, we expose only 64 out of 80 ecc bytes
48 */
49static struct nand_ecclayout onenand_oob_128 = {
50 .eccbytes = 64,
51 .eccpos = {
52 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
53 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
54 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
55 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
56 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
57 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
58 102, 103, 104, 105
59 },
60 .oobfree = {
61 {2, 4}, {18, 4}, {34, 4}, {50, 4},
62 {66, 4}, {82, 4}, {98, 4}, {114, 4}
63 }
64};
65
66/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010067 * onenand_oob_64 - oob info for large (2KB) page
68 */
69static struct nand_ecclayout onenand_oob_64 = {
70 .eccbytes = 20,
71 .eccpos = {
72 8, 9, 10, 11, 12,
73 24, 25, 26, 27, 28,
74 40, 41, 42, 43, 44,
75 56, 57, 58, 59, 60,
76 },
77 .oobfree = {
78 {2, 3}, {14, 2}, {18, 3}, {30, 2},
79 {34, 3}, {46, 2}, {50, 3}, {62, 2}
80 }
81};
82
83/**
84 * onenand_oob_32 - oob info for middle (1KB) page
85 */
86static struct nand_ecclayout onenand_oob_32 = {
87 .eccbytes = 10,
88 .eccpos = {
89 8, 9, 10, 11, 12,
90 24, 25, 26, 27, 28,
91 },
92 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
93};
94
Marek Vasut41120122013-12-26 01:01:24 +010095/*
96 * Warning! This array is used with the memcpy_16() function, thus
97 * it must be aligned to 2 bytes. GCC can make this array unaligned
98 * as the array is made of unsigned char, which memcpy16() doesn't
99 * like and will cause unaligned access.
100 */
101static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park79e90462007-09-10 17:13:49 +0900102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900118};
119
120/**
121 * onenand_readw - [OneNAND Interface] Read OneNAND register
122 * @param addr address to read
123 *
124 * Read OneNAND register
125 */
126static unsigned short onenand_readw(void __iomem * addr)
127{
128 return readw(addr);
129}
130
131/**
132 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
133 * @param value value to write
134 * @param addr address to write
135 *
136 * Write OneNAND register with value
137 */
138static void onenand_writew(unsigned short value, void __iomem * addr)
139{
140 writew(value, addr);
141}
142
143/**
144 * onenand_block_address - [DEFAULT] Get block address
145 * @param device the device id
146 * @param block the block
147 * @return translated block address if DDP, otherwise same
148 *
149 * Setup Start Address 1 Register (F100h)
150 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900151static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900152{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900153 /* Device Flash Core select, NAND Flash Block Address */
154 if (block & this->density_mask)
155 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900156
157 return block;
158}
159
160/**
161 * onenand_bufferram_address - [DEFAULT] Get bufferram address
162 * @param device the device id
163 * @param block the block
164 * @return set DBS value if DDP, otherwise 0
165 *
166 * Setup Start Address 2 Register (F101h) for DDP
167 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900168static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900169{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900170 /* Device BufferRAM Select */
171 if (block & this->density_mask)
172 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900173
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900174 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900175}
176
177/**
178 * onenand_page_address - [DEFAULT] Get page address
179 * @param page the page address
180 * @param sector the sector address
181 * @return combined page and sector address
182 *
183 * Setup Start Address 8 Register (F107h)
184 */
185static int onenand_page_address(int page, int sector)
186{
187 /* Flash Page Address, Flash Sector Address */
188 int fpa, fsa;
189
190 fpa = page & ONENAND_FPA_MASK;
191 fsa = sector & ONENAND_FSA_MASK;
192
193 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
194}
195
196/**
197 * onenand_buffer_address - [DEFAULT] Get buffer address
198 * @param dataram1 DataRAM index
199 * @param sectors the sector address
200 * @param count the number of sectors
201 * @return the start buffer value
202 *
203 * Setup Start Buffer Register (F200h)
204 */
205static int onenand_buffer_address(int dataram1, int sectors, int count)
206{
207 int bsa, bsc;
208
209 /* BufferRAM Sector Address */
210 bsa = sectors & ONENAND_BSA_MASK;
211
212 if (dataram1)
213 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
214 else
215 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
216
217 /* BufferRAM Sector Count */
218 bsc = count & ONENAND_BSC_MASK;
219
220 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
221}
222
223/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530224 * flexonenand_block - Return block number for flash address
225 * @param this - OneNAND device structure
226 * @param addr - Address for which block number is needed
227 */
228static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
229{
230 unsigned int boundary, blk, die = 0;
231
232 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
233 die = 1;
234 addr -= this->diesize[0];
235 }
236
237 boundary = this->boundary[die];
238
239 blk = addr >> (this->erase_shift - 1);
240 if (blk > boundary)
241 blk = (blk + boundary + 1) >> 1;
242
243 blk += die ? this->density_mask : 0;
244 return blk;
245}
246
247unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
248{
249 if (!FLEXONENAND(this))
250 return addr >> this->erase_shift;
251 return flexonenand_block(this, addr);
252}
253
254/**
255 * flexonenand_addr - Return address of the block
256 * @this: OneNAND device structure
257 * @block: Block number on Flex-OneNAND
258 *
259 * Return address of the block
260 */
261static loff_t flexonenand_addr(struct onenand_chip *this, int block)
262{
263 loff_t ofs = 0;
264 int die = 0, boundary;
265
266 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
267 block -= this->density_mask;
268 die = 1;
269 ofs = this->diesize[0];
270 }
271
272 boundary = this->boundary[die];
273 ofs += (loff_t) block << (this->erase_shift - 1);
274 if (block > (boundary + 1))
275 ofs += (loff_t) (block - boundary - 1)
276 << (this->erase_shift - 1);
277 return ofs;
278}
279
280loff_t onenand_addr(struct onenand_chip *this, int block)
281{
282 if (!FLEXONENAND(this))
283 return (loff_t) block << this->erase_shift;
284 return flexonenand_addr(this, block);
285}
286
287/**
288 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
289 * @param mtd MTD device structure
290 * @param addr address whose erase region needs to be identified
291 */
292int flexonenand_region(struct mtd_info *mtd, loff_t addr)
293{
294 int i;
295
296 for (i = 0; i < mtd->numeraseregions; i++)
297 if (addr < mtd->eraseregions[i].offset)
298 break;
299 return i - 1;
300}
301
302/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900303 * onenand_get_density - [DEFAULT] Get OneNAND density
304 * @param dev_id OneNAND device ID
305 *
306 * Get OneNAND density from device ID
307 */
308static inline int onenand_get_density(int dev_id)
309{
310 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
311 return (density & ONENAND_DEVICE_DENSITY_MASK);
312}
313
314/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900315 * onenand_command - [DEFAULT] Send command to OneNAND device
316 * @param mtd MTD device structure
317 * @param cmd the command to be sent
318 * @param addr offset to read from or write to
319 * @param len number of bytes to read or write
320 *
321 * Send command to OneNAND device. This function is used for middle/large page
322 * devices (1KB/2KB Bytes per page)
323 */
324static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
325 size_t len)
326{
327 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530328 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900329 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530330
Kyungmin Park79e90462007-09-10 17:13:49 +0900331 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530332 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900333
334 /* Address translation */
335 switch (cmd) {
336 case ONENAND_CMD_UNLOCK:
337 case ONENAND_CMD_LOCK:
338 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900339 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900340 block = -1;
341 page = -1;
342 break;
343
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530344 case FLEXONENAND_CMD_PI_ACCESS:
345 /* addr contains die index */
346 block = addr * this->density_mask;
347 page = -1;
348 break;
349
Kyungmin Park79e90462007-09-10 17:13:49 +0900350 case ONENAND_CMD_ERASE:
351 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530352 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900353 page = -1;
354 break;
355
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530356 case FLEXONENAND_CMD_READ_PI:
357 cmd = ONENAND_CMD_READ;
358 block = addr * this->density_mask;
359 page = 0;
360 break;
361
Kyungmin Park79e90462007-09-10 17:13:49 +0900362 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530363 block = onenand_block(this, addr);
364 page = (int) (addr
365 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900366 page &= this->page_mask;
367 break;
368 }
369
370 /* NOTE: The setting order of the registers is very important! */
371 if (cmd == ONENAND_CMD_BUFFERRAM) {
372 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900373 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900374 this->write_word(value,
375 this->base + ONENAND_REG_START_ADDRESS2);
376
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100377 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530378 ONENAND_SET_BUFFERRAM0(this);
379 else
380 /* Switch to the next data buffer */
381 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900382
383 return 0;
384 }
385
386 if (block != -1) {
387 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900388 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900389 this->write_word(value,
390 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900391
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530392 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900393 value = onenand_bufferram_address(this, block);
394 this->write_word(value,
395 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900396 }
397
398 if (page != -1) {
399 int dataram;
400
401 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530402 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900403 case ONENAND_CMD_READ:
404 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100405 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530406 dataram = ONENAND_SET_BUFFERRAM0(this);
407 else
408 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
409
Kyungmin Park79e90462007-09-10 17:13:49 +0900410 break;
411
412 default:
413 dataram = ONENAND_CURRENT_BUFFERRAM(this);
414 break;
415 }
416
417 /* Write 'FPA, FSA' of Flash */
418 value = onenand_page_address(page, sectors);
419 this->write_word(value,
420 this->base + ONENAND_REG_START_ADDRESS8);
421
422 /* Write 'BSA, BSC' of DataRAM */
423 value = onenand_buffer_address(dataram, sectors, count);
424 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900425 }
426
427 /* Interrupt clear */
428 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
429 /* Write command */
430 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
431
432 return 0;
433}
434
435/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530436 * onenand_read_ecc - return ecc status
437 * @param this onenand chip structure
438 */
439static int onenand_read_ecc(struct onenand_chip *this)
440{
441 int ecc, i;
442
443 if (!FLEXONENAND(this))
444 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
445
446 for (i = 0; i < 4; i++) {
447 ecc = this->read_word(this->base
448 + ((ONENAND_REG_ECC_STATUS + i) << 1));
449 if (likely(!ecc))
450 continue;
451 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
452 return ONENAND_ECC_2BIT_ALL;
453 }
454
455 return 0;
456}
457
458/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900459 * onenand_wait - [DEFAULT] wait until the command is done
460 * @param mtd MTD device structure
461 * @param state state to select the max. timeout value
462 *
463 * Wait for command done. This applies to all OneNAND command
464 * Read can take up to 30us, erase up to 2ms and program up to 350us
465 * according to general OneNAND specs
466 */
467static int onenand_wait(struct mtd_info *mtd, int state)
468{
469 struct onenand_chip *this = mtd->priv;
470 unsigned int flags = ONENAND_INT_MASTER;
471 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530472 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900473
474 while (1) {
475 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
476 if (interrupt & flags)
477 break;
478 }
479
480 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
481
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530482 if (interrupt & ONENAND_INT_READ) {
483 int ecc = onenand_read_ecc(this);
484 if (ecc & ONENAND_ECC_2BIT_ALL) {
485 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
486 return -EBADMSG;
487 }
488 }
489
Kyungmin Park79e90462007-09-10 17:13:49 +0900490 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900491 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
492 if (ctrl & ONENAND_CTRL_LOCK)
493 printk("onenand_wait: it's locked error = 0x%04x\n",
494 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900495
Kyungmin Park79e90462007-09-10 17:13:49 +0900496 return -EIO;
497 }
498
Kyungmin Park79e90462007-09-10 17:13:49 +0900499
500 return 0;
501}
502
503/**
504 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
505 * @param mtd MTD data structure
506 * @param area BufferRAM area
507 * @return offset given area
508 *
509 * Return BufferRAM offset given area
510 */
511static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
512{
513 struct onenand_chip *this = mtd->priv;
514
515 if (ONENAND_CURRENT_BUFFERRAM(this)) {
516 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900517 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900518 if (area == ONENAND_SPARERAM)
519 return mtd->oobsize;
520 }
521
522 return 0;
523}
524
525/**
526 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
527 * @param mtd MTD data structure
528 * @param area BufferRAM area
529 * @param buffer the databuffer to put/get data
530 * @param offset offset to read from or write to
531 * @param count number of bytes to read/write
532 *
533 * Read the BufferRAM area
534 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900535static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900536 unsigned char *buffer, int offset,
537 size_t count)
538{
539 struct onenand_chip *this = mtd->priv;
540 void __iomem *bufferram;
541
542 bufferram = this->base + area;
543 bufferram += onenand_bufferram_offset(mtd, area);
544
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200545 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900546
547 return 0;
548}
549
550/**
551 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
552 * @param mtd MTD data structure
553 * @param area BufferRAM area
554 * @param buffer the databuffer to put/get data
555 * @param offset offset to read from or write to
556 * @param count number of bytes to read/write
557 *
558 * Read the BufferRAM area with Sync. Burst Mode
559 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900560static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900561 unsigned char *buffer, int offset,
562 size_t count)
563{
564 struct onenand_chip *this = mtd->priv;
565 void __iomem *bufferram;
566
567 bufferram = this->base + area;
568 bufferram += onenand_bufferram_offset(mtd, area);
569
570 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
571
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200572 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900573
574 this->mmcontrol(mtd, 0);
575
576 return 0;
577}
578
579/**
580 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
581 * @param mtd MTD data structure
582 * @param area BufferRAM area
583 * @param buffer the databuffer to put/get data
584 * @param offset offset to read from or write to
585 * @param count number of bytes to read/write
586 *
587 * Write the BufferRAM area
588 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900589static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900590 const unsigned char *buffer, int offset,
591 size_t count)
592{
593 struct onenand_chip *this = mtd->priv;
594 void __iomem *bufferram;
595
596 bufferram = this->base + area;
597 bufferram += onenand_bufferram_offset(mtd, area);
598
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200599 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900600
601 return 0;
602}
603
604/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100605 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
606 * @param mtd MTD data structure
607 * @param addr address to check
608 * @return blockpage address
609 *
610 * Get blockpage address at 2x program mode
611 */
612static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
613{
614 struct onenand_chip *this = mtd->priv;
615 int blockpage, block, page;
616
617 /* Calculate the even block number */
618 block = (int) (addr >> this->erase_shift) & ~1;
619 /* Is it the odd plane? */
620 if (addr & this->writesize)
621 block++;
622 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
623 blockpage = (block << 7) | page;
624
625 return blockpage;
626}
627
628/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900629 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
630 * @param mtd MTD data structure
631 * @param addr address to check
632 * @return 1 if there are valid data, otherwise 0
633 *
634 * Check bufferram if there is data we required
635 */
636static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
637{
638 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900639 int blockpage, found = 0;
640 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900641
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900642 if (ONENAND_IS_2PLANE(this))
643 blockpage = onenand_get_2x_blockpage(mtd, addr);
644 else
645 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900646
647 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900648 i = ONENAND_CURRENT_BUFFERRAM(this);
649 if (this->bufferram[i].blockpage == blockpage)
650 found = 1;
651 else {
652 /* Check another BufferRAM */
653 i = ONENAND_NEXT_BUFFERRAM(this);
654 if (this->bufferram[i].blockpage == blockpage) {
655 ONENAND_SET_NEXT_BUFFERRAM(this);
656 found = 1;
657 }
658 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900659
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900660 if (found && ONENAND_IS_DDP(this)) {
661 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530662 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900663 int value = onenand_bufferram_address(this, block);
664 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
665 }
666
667 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900668}
669
670/**
671 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
672 * @param mtd MTD data structure
673 * @param addr address to update
674 * @param valid valid flag
675 *
676 * Update BufferRAM information
677 */
678static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
679 int valid)
680{
681 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900682 int blockpage;
683 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900684
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900685 if (ONENAND_IS_2PLANE(this))
686 blockpage = onenand_get_2x_blockpage(mtd, addr);
687 else
688 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900689
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900690 /* Invalidate another BufferRAM */
691 i = ONENAND_NEXT_BUFFERRAM(this);
692 if (this->bufferram[i].blockpage == blockpage)
693 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900694
695 /* Update BufferRAM */
696 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900697 if (valid)
698 this->bufferram[i].blockpage = blockpage;
699 else
700 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900701
702 return 0;
703}
704
705/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900706 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
707 * @param mtd MTD data structure
708 * @param addr start address to invalidate
709 * @param len length to invalidate
710 *
711 * Invalidate BufferRAM information
712 */
713static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200714 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900715{
716 struct onenand_chip *this = mtd->priv;
717 int i;
718 loff_t end_addr = addr + len;
719
720 /* Invalidate BufferRAM */
721 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900722 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900723
724 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900725 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900726 }
727}
728
729/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900730 * onenand_get_device - [GENERIC] Get chip for selected access
731 * @param mtd MTD device structure
732 * @param new_state the state which is requested
733 *
734 * Get the device and lock it for exclusive access
735 */
736static void onenand_get_device(struct mtd_info *mtd, int new_state)
737{
738 /* Do nothing */
739}
740
741/**
742 * onenand_release_device - [GENERIC] release chip
743 * @param mtd MTD device structure
744 *
745 * Deselect, release chip lock and wake up anyone waiting on the device
746 */
747static void onenand_release_device(struct mtd_info *mtd)
748{
749 /* Do nothing */
750}
751
752/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000753 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900754 * @param mtd MTD device structure
755 * @param buf destination address
756 * @param column oob offset to read from
757 * @param thislen oob length to read
758 */
759static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
760 int column, int thislen)
761{
762 struct onenand_chip *this = mtd->priv;
763 struct nand_oobfree *free;
764 int readcol = column;
765 int readend = column + thislen;
766 int lastgap = 0;
767 unsigned int i;
768 uint8_t *oob_buf = this->oob_buf;
769
770 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530771 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
772 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900773 if (readcol >= lastgap)
774 readcol += free->offset - lastgap;
775 if (readend >= lastgap)
776 readend += free->offset - lastgap;
777 lastgap = free->offset + free->length;
778 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900779 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900780 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530781 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
782 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900783 int free_end = free->offset + free->length;
784 if (free->offset < readend && free_end > readcol) {
785 int st = max_t(int,free->offset,readcol);
786 int ed = min_t(int,free_end,readend);
787 int n = ed - st;
788 memcpy(buf, oob_buf + st, n);
789 buf += n;
790 } else if (column == 0)
791 break;
792 }
793 return 0;
794}
795
796/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530797 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
798 * @param mtd MTD device structure
799 * @param addr address to recover
800 * @param status return value from onenand_wait
801 *
802 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
803 * lower page address and MSB page has higher page address in paired pages.
804 * If power off occurs during MSB page program, the paired LSB page data can
805 * become corrupt. LSB page recovery read is a way to read LSB page though page
806 * data are corrupted. When uncorrectable error occurs as a result of LSB page
807 * read after power up, issue LSB page recovery read.
808 */
809static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
810{
811 struct onenand_chip *this = mtd->priv;
812 int i;
813
814 /* Recovery is only for Flex-OneNAND */
815 if (!FLEXONENAND(this))
816 return status;
817
818 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000819 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530820 return status;
821
822 /* check if address lies in MLC region */
823 i = flexonenand_region(mtd, addr);
824 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
825 return status;
826
827 printk("onenand_recover_lsb:"
828 "Attempting to recover from uncorrectable read\n");
829
830 /* Issue the LSB page recovery command */
831 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
832 return this->wait(mtd, FL_READING);
833}
834
835/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900836 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900837 * @param mtd MTD device structure
838 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900839 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900840 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900841 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900842 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900843static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
844 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900845{
846 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900847 struct mtd_ecc_stats stats;
848 size_t len = ops->len;
849 size_t ooblen = ops->ooblen;
850 u_char *buf = ops->datbuf;
851 u_char *oobbuf = ops->oobbuf;
852 int read = 0, column, thislen;
853 int oobread = 0, oobcolumn, thisooblen, oobsize;
854 int ret = 0, boundary = 0;
855 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900856
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900857 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900858
Sergey Lapin3a38a552013-01-14 03:46:50 +0000859 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900860 oobsize = this->ecclayout->oobavail;
861 else
862 oobsize = mtd->oobsize;
863
864 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900865
866 /* Do not allow reads past end of device */
867 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900868 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
869 ops->retlen = 0;
870 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900871 return -EINVAL;
872 }
873
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900874 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900875
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900876 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530877 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900878
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900879 /* Do first load to bufferRAM */
880 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900881 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900882 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900883 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900884 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530885 if (unlikely(ret))
886 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900887 onenand_update_bufferram(mtd, from, !ret);
888 if (ret == -EBADMSG)
889 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900890 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900891 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900892
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900893 thislen = min_t(int, writesize, len - read);
894 column = from & (writesize - 1);
895 if (column + thislen > writesize)
896 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900897
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900898 while (!ret) {
899 /* If there is more to load then start next load */
900 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100901 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900902 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900903 this->command(mtd, ONENAND_CMD_READ, from, writesize);
904 /*
905 * Chip boundary handling in DDP
906 * Now we issued chip 1 read and pointed chip 1
907 * bufferam so we have to point chip 0 bufferam.
908 */
909 if (ONENAND_IS_DDP(this) &&
910 unlikely(from == (this->chipsize >> 1))) {
911 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
912 boundary = 1;
913 } else
914 boundary = 0;
915 ONENAND_SET_PREV_BUFFERRAM(this);
916 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900917
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900918 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900919 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900920
921 /* Read oob area if needed */
922 if (oobbuf) {
923 thisooblen = oobsize - oobcolumn;
924 thisooblen = min_t(int, thisooblen, ooblen - oobread);
925
Sergey Lapin3a38a552013-01-14 03:46:50 +0000926 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900927 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
928 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900929 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900930 oobread += thisooblen;
931 oobbuf += thisooblen;
932 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900933 }
934
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100935 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530936 this->command(mtd, ONENAND_CMD_READ, from, writesize);
937 ret = this->wait(mtd, FL_READING);
938 if (unlikely(ret))
939 ret = onenand_recover_lsb(mtd, from, ret);
940 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000941 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530942 ret = 0;
943 }
944
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900945 /* See if we are done */
946 read += thislen;
947 if (read == len)
948 break;
949 /* Set up for next read from bufferRAM */
950 if (unlikely(boundary))
951 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100952 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530953 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900954 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900955 thislen = min_t(int, writesize, len - read);
956 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900957
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100958 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530959 /* Now wait for load */
960 ret = this->wait(mtd, FL_READING);
961 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000962 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530963 ret = 0;
964 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900965 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900966
967 /*
968 * Return success, if no ECC failures, else -EBADMSG
969 * fs driver will take care of that, because
970 * retlen == desired len and result == -EBADMSG
971 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900972 ops->retlen = read;
973 ops->oobretlen = oobread;
974
975 if (ret)
976 return ret;
977
978 if (mtd->ecc_stats.failed - stats.failed)
979 return -EBADMSG;
980
Paul Burton700a76c2013-09-04 15:16:56 +0100981 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
982 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900983}
984
985/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900986 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
987 * @param mtd MTD device structure
988 * @param from offset to read from
989 * @param ops oob operation description structure
990 *
991 * OneNAND read out-of-band data from the spare area
992 */
993static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
994 struct mtd_oob_ops *ops)
995{
996 struct onenand_chip *this = mtd->priv;
997 struct mtd_ecc_stats stats;
998 int read = 0, thislen, column, oobsize;
999 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001000 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001001 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301002 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001003
1004 from += ops->ooboffs;
1005
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001006 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001007
1008 /* Initialize return length value */
1009 ops->oobretlen = 0;
1010
Sergey Lapin3a38a552013-01-14 03:46:50 +00001011 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001012 oobsize = this->ecclayout->oobavail;
1013 else
1014 oobsize = mtd->oobsize;
1015
1016 column = from & (mtd->oobsize - 1);
1017
1018 if (unlikely(column >= oobsize)) {
1019 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1020 return -EINVAL;
1021 }
1022
1023 /* Do not allow reads past end of device */
1024 if (unlikely(from >= mtd->size ||
1025 column + len > ((mtd->size >> this->page_shift) -
1026 (from >> this->page_shift)) * oobsize)) {
1027 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1028 return -EINVAL;
1029 }
1030
1031 stats = mtd->ecc_stats;
1032
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001033 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1034 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301035
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001036 while (read < len) {
1037 thislen = oobsize - column;
1038 thislen = min_t(int, thislen, len);
1039
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001040 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301041 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001042
1043 onenand_update_bufferram(mtd, from, 0);
1044
1045 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301046 if (unlikely(ret))
1047 ret = onenand_recover_lsb(mtd, from, ret);
1048
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001049 if (ret && ret != -EBADMSG) {
1050 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1051 break;
1052 }
1053
Sergey Lapin3a38a552013-01-14 03:46:50 +00001054 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001055 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1056 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001057 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001058
1059 read += thislen;
1060
1061 if (read == len)
1062 break;
1063
1064 buf += thislen;
1065
1066 /* Read more? */
1067 if (read < len) {
1068 /* Page size */
1069 from += mtd->writesize;
1070 column = 0;
1071 }
1072 }
1073
1074 ops->oobretlen = read;
1075
1076 if (ret)
1077 return ret;
1078
1079 if (mtd->ecc_stats.failed - stats.failed)
1080 return -EBADMSG;
1081
1082 return 0;
1083}
1084
1085/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001086 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1087 * @param mtd MTD device structure
1088 * @param from offset to read from
1089 * @param len number of bytes to read
1090 * @param retlen pointer to variable to store the number of read bytes
1091 * @param buf the databuffer to put data
1092 *
1093 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1094*/
1095int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1096 size_t * retlen, u_char * buf)
1097{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001098 struct mtd_oob_ops ops = {
1099 .len = len,
1100 .ooblen = 0,
1101 .datbuf = buf,
1102 .oobbuf = NULL,
1103 };
1104 int ret;
1105
1106 onenand_get_device(mtd, FL_READING);
1107 ret = onenand_read_ops_nolock(mtd, from, &ops);
1108 onenand_release_device(mtd);
1109
1110 *retlen = ops.retlen;
1111 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001112}
1113
1114/**
1115 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1116 * @param mtd MTD device structure
1117 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001118 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001119 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001120 * OneNAND main and/or out-of-band
1121 */
1122int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1123 struct mtd_oob_ops *ops)
1124{
1125 int ret;
1126
1127 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001128 case MTD_OPS_PLACE_OOB:
1129 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001130 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001131 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001132 /* Not implemented yet */
1133 default:
1134 return -EINVAL;
1135 }
1136
1137 onenand_get_device(mtd, FL_READING);
1138 if (ops->datbuf)
1139 ret = onenand_read_ops_nolock(mtd, from, ops);
1140 else
1141 ret = onenand_read_oob_nolock(mtd, from, ops);
1142 onenand_release_device(mtd);
1143
1144 return ret;
1145}
1146
1147/**
1148 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1149 * @param mtd MTD device structure
1150 * @param state state to select the max. timeout value
1151 *
1152 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001153 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001154static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001155{
1156 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001157 unsigned int flags = ONENAND_INT_MASTER;
1158 unsigned int interrupt;
1159 unsigned int ctrl;
1160
1161 while (1) {
1162 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1163 if (interrupt & flags)
1164 break;
1165 }
1166
1167 /* To get correct interrupt status in timeout case */
1168 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1169 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1170
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001171 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301172 int ecc = onenand_read_ecc(this);
1173 if (ecc & ONENAND_ECC_2BIT_ALL) {
1174 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1175 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001176 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301177 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001178 } else {
1179 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1180 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1181 return ONENAND_BBT_READ_FATAL_ERROR;
1182 }
1183
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001184 /* Initial bad block case: 0x2400 or 0x0400 */
1185 if (ctrl & ONENAND_CTRL_ERROR) {
1186 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1187 return ONENAND_BBT_READ_ERROR;
1188 }
1189
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001190 return 0;
1191}
1192
1193/**
1194 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1195 * @param mtd MTD device structure
1196 * @param from offset to read from
1197 * @param ops oob operation description structure
1198 *
1199 * OneNAND read out-of-band data from the spare area for bbt scan
1200 */
1201int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1202 struct mtd_oob_ops *ops)
1203{
1204 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001205 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301206 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001207 size_t len = ops->ooblen;
1208 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001209
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001210 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001211
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001212 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1213 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301214
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001215 /* Initialize return value */
1216 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001217
1218 /* Do not allow reads past end of device */
1219 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001220 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1221 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001222 }
1223
1224 /* Grab the lock and see if the device is available */
1225 onenand_get_device(mtd, FL_READING);
1226
1227 column = from & (mtd->oobsize - 1);
1228
1229 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001230
Kyungmin Park79e90462007-09-10 17:13:49 +09001231 thislen = mtd->oobsize - column;
1232 thislen = min_t(int, thislen, len);
1233
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001234 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301235 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001236
1237 onenand_update_bufferram(mtd, from, 0);
1238
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001239 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301240 if (unlikely(ret))
1241 ret = onenand_recover_lsb(mtd, from, ret);
1242
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001243 if (ret)
1244 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001245
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001246 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001247 read += thislen;
1248 if (read == len)
1249 break;
1250
Kyungmin Park79e90462007-09-10 17:13:49 +09001251 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001252
Kyungmin Park79e90462007-09-10 17:13:49 +09001253 /* Read more? */
1254 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001255 /* Update Page size */
1256 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001257 column = 0;
1258 }
1259 }
1260
1261 /* Deselect and wake up anyone waiting on the device */
1262 onenand_release_device(mtd);
1263
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001264 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001265 return ret;
1266}
1267
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001268
Kyungmin Park79e90462007-09-10 17:13:49 +09001269#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1270/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001271 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1272 * @param mtd MTD device structure
1273 * @param buf the databuffer to verify
1274 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001275 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001276static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001277{
1278 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001279 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301280 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001281
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001282 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1283 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301284
1285 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001286 onenand_update_bufferram(mtd, to, 0);
1287 status = this->wait(mtd, FL_READING);
1288 if (status)
1289 return status;
1290
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001291 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001292 for (i = 0; i < mtd->oobsize; i++)
1293 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1294 return -EBADMSG;
1295
1296 return 0;
1297}
1298
1299/**
1300 * onenand_verify - [GENERIC] verify the chip contents after a write
1301 * @param mtd MTD device structure
1302 * @param buf the databuffer to verify
1303 * @param addr offset to read from
1304 * @param len number of bytes to read and compare
1305 */
1306static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1307{
1308 struct onenand_chip *this = mtd->priv;
1309 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001310 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001311 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001312
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001313 while (len != 0) {
1314 thislen = min_t(int, this->writesize, len);
1315 column = addr & (this->writesize - 1);
1316 if (column + thislen > this->writesize)
1317 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001318
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001319 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001320
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001321 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001322
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001323 ret = this->wait(mtd, FL_READING);
1324 if (ret)
1325 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001326
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001327 onenand_update_bufferram(mtd, addr, 1);
1328
1329 dataram = this->base + ONENAND_DATARAM;
1330 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1331
1332 if (memcmp(buf, dataram + column, thislen))
1333 return -EBADMSG;
1334
1335 len -= thislen;
1336 buf += thislen;
1337 addr += thislen;
1338 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001339
1340 return 0;
1341}
1342#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001343#define onenand_verify(...) (0)
1344#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001345#endif
1346
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001347#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001348
1349/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001350 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001351 * @param mtd MTD device structure
1352 * @param oob_buf oob buffer
1353 * @param buf source address
1354 * @param column oob offset to write to
1355 * @param thislen oob length to write
1356 */
1357static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1358 const u_char *buf, int column, int thislen)
1359{
1360 struct onenand_chip *this = mtd->priv;
1361 struct nand_oobfree *free;
1362 int writecol = column;
1363 int writeend = column + thislen;
1364 int lastgap = 0;
1365 unsigned int i;
1366
1367 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301368 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1369 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001370 if (writecol >= lastgap)
1371 writecol += free->offset - lastgap;
1372 if (writeend >= lastgap)
1373 writeend += free->offset - lastgap;
1374 lastgap = free->offset + free->length;
1375 }
1376 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301377 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1378 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001379 int free_end = free->offset + free->length;
1380 if (free->offset < writeend && free_end > writecol) {
1381 int st = max_t(int,free->offset,writecol);
1382 int ed = min_t(int,free_end,writeend);
1383 int n = ed - st;
1384 memcpy(oob_buf + st, buf, n);
1385 buf += n;
1386 } else if (column == 0)
1387 break;
1388 }
1389 return 0;
1390}
1391
1392/**
1393 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1394 * @param mtd MTD device structure
1395 * @param to offset to write to
1396 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001397 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001398 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001399 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001400static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1401 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001402{
1403 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001404 int written = 0, column, thislen, subpage;
1405 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1406 size_t len = ops->len;
1407 size_t ooblen = ops->ooblen;
1408 const u_char *buf = ops->datbuf;
1409 const u_char *oob = ops->oobbuf;
1410 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001411 int ret = 0;
1412
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001413 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001414
1415 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001416 ops->retlen = 0;
1417 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001418
Kyungmin Park79e90462007-09-10 17:13:49 +09001419 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001420 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1421 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001422 return -EINVAL;
1423 }
1424
Sergey Lapin3a38a552013-01-14 03:46:50 +00001425 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001426 oobsize = this->ecclayout->oobavail;
1427 else
1428 oobsize = mtd->oobsize;
1429
1430 oobcolumn = to & (mtd->oobsize - 1);
1431
1432 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001433
1434 /* Loop until all data write */
1435 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001436 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001437
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001438 thislen = min_t(int, mtd->writesize - column, len - written);
1439 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001440
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001441 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001442
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001443 /* Partial page write */
1444 subpage = thislen < mtd->writesize;
1445 if (subpage) {
1446 memset(this->page_buf, 0xff, mtd->writesize);
1447 memcpy(this->page_buf + column, buf, thislen);
1448 wbuf = this->page_buf;
1449 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001450
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001451 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001452
1453 if (oob) {
1454 oobbuf = this->oob_buf;
1455
1456 /* We send data to spare ram with oobsize
1457 * * to prevent byte access */
1458 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001459 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001460 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1461 else
1462 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1463
1464 oobwritten += thisooblen;
1465 oob += thisooblen;
1466 oobcolumn = 0;
1467 } else
1468 oobbuf = (u_char *) ffchars;
1469
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001470 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001471
1472 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001473
1474 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001475
1476 /* In partial page write we don't update bufferram */
1477 onenand_update_bufferram(mtd, to, !ret && !subpage);
1478 if (ONENAND_IS_2PLANE(this)) {
1479 ONENAND_SET_BUFFERRAM1(this);
1480 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1481 }
1482
Kyungmin Park79e90462007-09-10 17:13:49 +09001483 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001484 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001485 break;
1486 }
1487
Kyungmin Park79e90462007-09-10 17:13:49 +09001488 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001489 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001490 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001491 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001492 break;
1493 }
1494
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001495 written += thislen;
1496
Kyungmin Park79e90462007-09-10 17:13:49 +09001497 if (written == len)
1498 break;
1499
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001500 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001501 to += thislen;
1502 buf += thislen;
1503 }
1504
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001505 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001506
1507 return ret;
1508}
1509
1510/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001511 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001512 * @param mtd MTD device structure
1513 * @param to offset to write to
1514 * @param len number of bytes to write
1515 * @param retlen pointer to variable to store the number of written bytes
1516 * @param buf the data to write
1517 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001518 *
1519 * OneNAND write out-of-band
1520 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001521static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1522 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001523{
1524 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001525 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301526 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001527 u_char *oobbuf;
1528 size_t len = ops->ooblen;
1529 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001530 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001531
1532 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001533
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001534 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001535
1536 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001537 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001538
Sergey Lapin3a38a552013-01-14 03:46:50 +00001539 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001540 oobsize = this->ecclayout->oobavail;
1541 else
1542 oobsize = mtd->oobsize;
1543
1544 column = to & (mtd->oobsize - 1);
1545
1546 if (unlikely(column >= oobsize)) {
1547 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001548 return -EINVAL;
1549 }
1550
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001551 /* For compatibility with NAND: Do not allow write past end of page */
1552 if (unlikely(column + len > oobsize)) {
1553 printk(KERN_ERR "onenand_write_oob_nolock: "
1554 "Attempt to write past end of page\n");
1555 return -EINVAL;
1556 }
1557
1558 /* Do not allow reads past end of device */
1559 if (unlikely(to >= mtd->size ||
1560 column + len > ((mtd->size >> this->page_shift) -
1561 (to >> this->page_shift)) * oobsize)) {
1562 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1563 return -EINVAL;
1564 }
1565
1566 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001567
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001568 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1569 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301570
Kyungmin Park79e90462007-09-10 17:13:49 +09001571 /* Loop until all data write */
1572 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001573 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001574
1575 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1576
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001577 /* We send data to spare ram with oobsize
1578 * to prevent byte access */
1579 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001580 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001581 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1582 else
1583 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001584 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001585
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001586 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301587 /* Set main area of DataRAM to 0xff*/
1588 memset(this->page_buf, 0xff, mtd->writesize);
1589 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1590 this->page_buf, 0, mtd->writesize);
1591 }
1592
1593 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001594
1595 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001596 if (ONENAND_IS_2PLANE(this)) {
1597 ONENAND_SET_BUFFERRAM1(this);
1598 onenand_update_bufferram(mtd, to + this->writesize, 0);
1599 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001600
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001601 ret = this->wait(mtd, FL_WRITING);
1602 if (ret) {
1603 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001604 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001605 }
1606
1607 ret = onenand_verify_oob(mtd, oobbuf, to);
1608 if (ret) {
1609 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1610 break;
1611 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001612
1613 written += thislen;
1614 if (written == len)
1615 break;
1616
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001617 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001618 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001619 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001620 }
1621
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001622 ops->oobretlen = written;
1623
1624 return ret;
1625}
1626
1627/**
1628 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1629 * @param mtd MTD device structure
1630 * @param to offset to write to
1631 * @param len number of bytes to write
1632 * @param retlen pointer to variable to store the number of written bytes
1633 * @param buf the data to write
1634 *
1635 * Write with ECC
1636 */
1637int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1638 size_t * retlen, const u_char * buf)
1639{
1640 struct mtd_oob_ops ops = {
1641 .len = len,
1642 .ooblen = 0,
1643 .datbuf = (u_char *) buf,
1644 .oobbuf = NULL,
1645 };
1646 int ret;
1647
1648 onenand_get_device(mtd, FL_WRITING);
1649 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001650 onenand_release_device(mtd);
1651
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001652 *retlen = ops.retlen;
1653 return ret;
1654}
Kyungmin Park79e90462007-09-10 17:13:49 +09001655
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001656/**
1657 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1658 * @param mtd MTD device structure
1659 * @param to offset to write to
1660 * @param ops oob operation description structure
1661 *
1662 * OneNAND write main and/or out-of-band
1663 */
1664int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1665 struct mtd_oob_ops *ops)
1666{
1667 int ret;
1668
1669 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001670 case MTD_OPS_PLACE_OOB:
1671 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001672 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001673 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001674 /* Not implemented yet */
1675 default:
1676 return -EINVAL;
1677 }
1678
1679 onenand_get_device(mtd, FL_WRITING);
1680 if (ops->datbuf)
1681 ret = onenand_write_ops_nolock(mtd, to, ops);
1682 else
1683 ret = onenand_write_oob_nolock(mtd, to, ops);
1684 onenand_release_device(mtd);
1685
1686 return ret;
1687
Kyungmin Park79e90462007-09-10 17:13:49 +09001688}
1689
Kyungmin Park396b0c42008-08-13 09:11:02 +09001690/**
1691 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1692 * @param mtd MTD device structure
1693 * @param ofs offset from device start
1694 * @param allowbbt 1, if its allowed to access the bbt area
1695 *
1696 * Check, if the block is bad, Either by reading the bad block table or
1697 * calling of the scan function.
1698 */
1699static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1700{
1701 struct onenand_chip *this = mtd->priv;
1702 struct bbm_info *bbm = this->bbm;
1703
1704 /* Return info from the table */
1705 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1706}
1707
1708
Kyungmin Park79e90462007-09-10 17:13:49 +09001709/**
1710 * onenand_erase - [MTD Interface] erase block(s)
1711 * @param mtd MTD device structure
1712 * @param instr erase instruction
1713 *
1714 * Erase one ore more blocks
1715 */
1716int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1717{
1718 struct onenand_chip *this = mtd->priv;
1719 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301720 loff_t addr = instr->addr;
1721 unsigned int len = instr->len;
1722 int ret = 0, i;
1723 struct mtd_erase_region_info *region = NULL;
1724 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001725
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301726 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1727 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001728
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301729 if (FLEXONENAND(this)) {
1730 /* Find the eraseregion of this address */
1731 i = flexonenand_region(mtd, addr);
1732 region = &mtd->eraseregions[i];
1733
1734 block_size = region->erasesize;
1735 region_end = region->offset
1736 + region->erasesize * region->numblocks;
1737
1738 /* Start address within region must align on block boundary.
1739 * Erase region's start offset is always block start address.
1740 */
1741 if (unlikely((addr - region->offset) & (block_size - 1))) {
1742 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1743 " Unaligned address\n");
1744 return -EINVAL;
1745 }
1746 } else {
1747 block_size = 1 << this->erase_shift;
1748
1749 /* Start address must align on block boundary */
1750 if (unlikely(addr & (block_size - 1))) {
1751 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1752 "Unaligned address\n");
1753 return -EINVAL;
1754 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001755 }
1756
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301757 /* Length must align on block boundary */
1758 if (unlikely(len & (block_size - 1))) {
Scott Wooddf83c472008-06-20 12:38:57 -05001759 MTDDEBUG (MTD_DEBUG_LEVEL0,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301760 "onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001761 return -EINVAL;
1762 }
1763
Kyungmin Park79e90462007-09-10 17:13:49 +09001764 /* Grab the lock and see if the device is available */
1765 onenand_get_device(mtd, FL_ERASING);
1766
1767 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001768 instr->state = MTD_ERASING;
1769
1770 while (len) {
1771
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001772 /* Check if we have a bad block, we do not erase bad blocks */
1773 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1774 printk(KERN_WARNING "onenand_erase: attempt to erase"
1775 " a bad block at addr 0x%08x\n",
1776 (unsigned int) addr);
1777 instr->state = MTD_ERASE_FAILED;
1778 goto erase_exit;
1779 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001780
1781 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1782
Kyungmin Park396b0c42008-08-13 09:11:02 +09001783 onenand_invalidate_bufferram(mtd, addr, block_size);
1784
Kyungmin Park79e90462007-09-10 17:13:49 +09001785 ret = this->wait(mtd, FL_ERASING);
1786 /* Check, if it is write protected */
1787 if (ret) {
1788 if (ret == -EPERM)
Scott Wooddf83c472008-06-20 12:38:57 -05001789 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001790 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001791 else
Scott Wooddf83c472008-06-20 12:38:57 -05001792 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001793 "Failed erase, block %d\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301794 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001795 instr->state = MTD_ERASE_FAILED;
1796 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001797
Kyungmin Park79e90462007-09-10 17:13:49 +09001798 goto erase_exit;
1799 }
1800
1801 len -= block_size;
1802 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301803
1804 if (addr == region_end) {
1805 if (!len)
1806 break;
1807 region++;
1808
1809 block_size = region->erasesize;
1810 region_end = region->offset
1811 + region->erasesize * region->numblocks;
1812
1813 if (len & (block_size - 1)) {
1814 /* This has been checked at MTD
1815 * partitioning level. */
1816 printk("onenand_erase: Unaligned address\n");
1817 goto erase_exit;
1818 }
1819 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001820 }
1821
1822 instr->state = MTD_ERASE_DONE;
1823
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001824erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001825
1826 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1827 /* Do call back function */
1828 if (!ret)
1829 mtd_erase_callback(instr);
1830
1831 /* Deselect and wake up anyone waiting on the device */
1832 onenand_release_device(mtd);
1833
1834 return ret;
1835}
1836
1837/**
1838 * onenand_sync - [MTD Interface] sync
1839 * @param mtd MTD device structure
1840 *
1841 * Sync is actually a wait for chip ready function
1842 */
1843void onenand_sync(struct mtd_info *mtd)
1844{
Scott Wooddf83c472008-06-20 12:38:57 -05001845 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001846
1847 /* Grab the lock and see if the device is available */
1848 onenand_get_device(mtd, FL_SYNCING);
1849
1850 /* Release it and go back */
1851 onenand_release_device(mtd);
1852}
1853
1854/**
1855 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1856 * @param mtd MTD device structure
1857 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001858 *
1859 * Check whether the block is bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001860 */
1861int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1862{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001863 int ret;
1864
1865 /* Check for invalid offset */
1866 if (ofs > mtd->size)
1867 return -EINVAL;
1868
1869 onenand_get_device(mtd, FL_READING);
1870 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1871 onenand_release_device(mtd);
1872 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001873}
1874
1875/**
Kyungmin Park87b49502008-11-13 15:14:33 +09001876 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1877 * @param mtd MTD device structure
1878 * @param ofs offset from device start
1879 *
1880 * This is the default implementation, which can be overridden by
1881 * a hardware specific driver.
1882 */
1883static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1884{
1885 struct onenand_chip *this = mtd->priv;
1886 struct bbm_info *bbm = this->bbm;
1887 u_char buf[2] = {0, 0};
1888 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001889 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park87b49502008-11-13 15:14:33 +09001890 .ooblen = 2,
1891 .oobbuf = buf,
1892 .ooboffs = 0,
1893 };
1894 int block;
1895
1896 /* Get block number */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301897 block = onenand_block(this, ofs);
Kyungmin Park87b49502008-11-13 15:14:33 +09001898 if (bbm->bbt)
1899 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1900
1901 /* We write two bytes, so we dont have to mess with 16 bit access */
1902 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1903 return onenand_write_oob_nolock(mtd, ofs, &ops);
1904}
1905
1906/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001907 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1908 * @param mtd MTD device structure
1909 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001910 *
1911 * Mark the block as bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001912 */
1913int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1914{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001915 int ret;
1916
1917 ret = onenand_block_isbad(mtd, ofs);
1918 if (ret) {
1919 /* If it was bad already, return success and do nothing */
1920 if (ret > 0)
1921 return 0;
1922 return ret;
1923 }
1924
Sergey Lapin3a38a552013-01-14 03:46:50 +00001925 ret = mtd_block_markbad(mtd, ofs);
Kyungmin Park396b0c42008-08-13 09:11:02 +09001926 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001927}
1928
1929/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001930 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1931 * @param mtd MTD device structure
1932 * @param ofs offset relative to mtd start
1933 * @param len number of bytes to lock or unlock
1934 * @param cmd lock or unlock command
Kyungmin Park79e90462007-09-10 17:13:49 +09001935 *
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001936 * Lock or unlock one or more blocks
Kyungmin Park79e90462007-09-10 17:13:49 +09001937 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001938static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park79e90462007-09-10 17:13:49 +09001939{
1940 struct onenand_chip *this = mtd->priv;
1941 int start, end, block, value, status;
1942
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301943 start = onenand_block(this, ofs);
1944 end = onenand_block(this, ofs + len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001945
1946 /* Continuous lock scheme */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001947 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park79e90462007-09-10 17:13:49 +09001948 /* Set start block address */
1949 this->write_word(start,
1950 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1951 /* Set end block address */
1952 this->write_word(end - 1,
1953 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1954 /* Write unlock command */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001955 this->command(mtd, cmd, 0, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001956
1957 /* There's no return value */
1958 this->wait(mtd, FL_UNLOCKING);
1959
1960 /* Sanity check */
1961 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1962 & ONENAND_CTRL_ONGO)
1963 continue;
1964
1965 /* Check lock status */
1966 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1967 if (!(status & ONENAND_WP_US))
1968 printk(KERN_ERR "wp status = 0x%x\n", status);
1969
1970 return 0;
1971 }
1972
1973 /* Block lock scheme */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301974 for (block = start; block < end; block++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001975 /* Set block address */
1976 value = onenand_block_address(this, block);
1977 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1978 /* Select DataRAM for DDP */
1979 value = onenand_bufferram_address(this, block);
1980 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1981
Kyungmin Park79e90462007-09-10 17:13:49 +09001982 /* Set start block address */
1983 this->write_word(block,
1984 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1985 /* Write unlock command */
1986 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1987
1988 /* There's no return value */
1989 this->wait(mtd, FL_UNLOCKING);
1990
1991 /* Sanity check */
1992 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1993 & ONENAND_CTRL_ONGO)
1994 continue;
1995
Kyungmin Park79e90462007-09-10 17:13:49 +09001996 /* Check lock status */
1997 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1998 if (!(status & ONENAND_WP_US))
1999 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2000 block, status);
2001 }
2002
2003 return 0;
2004}
2005
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002006#ifdef ONENAND_LINUX
Kyungmin Park79e90462007-09-10 17:13:49 +09002007/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002008 * onenand_lock - [MTD Interface] Lock block(s)
2009 * @param mtd MTD device structure
2010 * @param ofs offset relative to mtd start
2011 * @param len number of bytes to unlock
2012 *
2013 * Lock one or more blocks
2014 */
2015static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2016{
2017 int ret;
2018
2019 onenand_get_device(mtd, FL_LOCKING);
2020 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2021 onenand_release_device(mtd);
2022 return ret;
2023}
2024
2025/**
2026 * onenand_unlock - [MTD Interface] Unlock block(s)
2027 * @param mtd MTD device structure
2028 * @param ofs offset relative to mtd start
2029 * @param len number of bytes to unlock
2030 *
2031 * Unlock one or more blocks
2032 */
2033static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2034{
2035 int ret;
2036
2037 onenand_get_device(mtd, FL_LOCKING);
2038 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2039 onenand_release_device(mtd);
2040 return ret;
2041}
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002042#endif
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002043
2044/**
2045 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2046 * @param this onenand chip data structure
2047 *
2048 * Check lock status
2049 */
2050static int onenand_check_lock_status(struct onenand_chip *this)
2051{
2052 unsigned int value, block, status;
2053 unsigned int end;
2054
2055 end = this->chipsize >> this->erase_shift;
2056 for (block = 0; block < end; block++) {
2057 /* Set block address */
2058 value = onenand_block_address(this, block);
2059 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2060 /* Select DataRAM for DDP */
2061 value = onenand_bufferram_address(this, block);
2062 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2063 /* Set start block address */
2064 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2065
2066 /* Check lock status */
2067 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2068 if (!(status & ONENAND_WP_US)) {
2069 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2070 return 0;
2071 }
2072 }
2073
2074 return 1;
2075}
2076
2077/**
2078 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2079 * @param mtd MTD device structure
2080 *
2081 * Unlock all blocks
2082 */
2083static void onenand_unlock_all(struct mtd_info *mtd)
2084{
2085 struct onenand_chip *this = mtd->priv;
2086 loff_t ofs = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302087 size_t len = mtd->size;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002088
2089 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2090 /* Set start block address */
2091 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2092 /* Write unlock command */
2093 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2094
2095 /* There's no return value */
2096 this->wait(mtd, FL_LOCKING);
2097
2098 /* Sanity check */
2099 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2100 & ONENAND_CTRL_ONGO)
2101 continue;
2102
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002103 /* Check lock status */
2104 if (onenand_check_lock_status(this))
2105 return;
2106
2107 /* Workaround for all block unlock in DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302108 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002109 /* All blocks on another chip */
2110 ofs = this->chipsize >> 1;
2111 len = this->chipsize >> 1;
2112 }
2113 }
2114
2115 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2116}
2117
2118
2119/**
2120 * onenand_check_features - Check and set OneNAND features
2121 * @param mtd MTD data structure
2122 *
2123 * Check and set OneNAND features
2124 * - lock scheme
2125 * - two plane
2126 */
2127static void onenand_check_features(struct mtd_info *mtd)
2128{
2129 struct onenand_chip *this = mtd->priv;
2130 unsigned int density, process;
2131
2132 /* Lock scheme depends on density and process */
2133 density = onenand_get_density(this->device_id);
2134 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2135
2136 /* Lock scheme */
2137 switch (density) {
2138 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002139 if (ONENAND_IS_DDP(this))
2140 this->options |= ONENAND_HAS_2PLANE;
2141 else
2142 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002143
2144 case ONENAND_DEVICE_DENSITY_2Gb:
2145 /* 2Gb DDP don't have 2 plane */
2146 if (!ONENAND_IS_DDP(this))
2147 this->options |= ONENAND_HAS_2PLANE;
2148 this->options |= ONENAND_HAS_UNLOCK_ALL;
2149
2150 case ONENAND_DEVICE_DENSITY_1Gb:
2151 /* A-Die has all block unlock */
2152 if (process)
2153 this->options |= ONENAND_HAS_UNLOCK_ALL;
2154 break;
2155
2156 default:
2157 /* Some OneNAND has continuous lock scheme */
2158 if (!process)
2159 this->options |= ONENAND_HAS_CONT_LOCK;
2160 break;
2161 }
2162
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302163 if (ONENAND_IS_MLC(this))
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002164 this->options |= ONENAND_HAS_4KB_PAGE;
2165
2166 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302167 this->options &= ~ONENAND_HAS_2PLANE;
2168
2169 if (FLEXONENAND(this)) {
2170 this->options &= ~ONENAND_HAS_CONT_LOCK;
2171 this->options |= ONENAND_HAS_UNLOCK_ALL;
2172 }
2173
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002174 if (this->options & ONENAND_HAS_CONT_LOCK)
2175 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2176 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2177 printk(KERN_DEBUG "Chip support all block unlock\n");
2178 if (this->options & ONENAND_HAS_2PLANE)
2179 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002180 if (this->options & ONENAND_HAS_4KB_PAGE)
2181 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2182
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002183}
2184
2185/**
Kyungmin Park79e90462007-09-10 17:13:49 +09002186 * onenand_print_device_info - Print device ID
2187 * @param device device ID
2188 *
2189 * Print device ID
2190 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002191char *onenand_print_device_info(int device, int version)
Kyungmin Park79e90462007-09-10 17:13:49 +09002192{
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302193 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002194 char *dev_info = malloc(80);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002195 char *p = dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002196
2197 vcc = device & ONENAND_DEVICE_VCC_MASK;
2198 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2199 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302200 density = onenand_get_density(device);
2201 flexonenand = device & DEVICE_IS_FLEXONENAND;
2202 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park79e90462007-09-10 17:13:49 +09002203 demuxed ? "" : "Muxed ",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302204 flexonenand ? "Flex-" : "",
Kyungmin Park79e90462007-09-10 17:13:49 +09002205 ddp ? "(DDP)" : "",
2206 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002207
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002208 sprintf(p, "\nOneNAND version = 0x%04x", version);
2209 printk("%s\n", dev_info);
2210
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002211 return dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002212}
2213
2214static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra891d81b2010-10-11 21:48:03 +02002215 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002216 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002217};
2218
2219/**
2220 * onenand_check_maf - Check manufacturer ID
2221 * @param manuf manufacturer ID
2222 *
2223 * Check manufacturer ID
2224 */
2225static int onenand_check_maf(int manuf)
2226{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002227 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park79e90462007-09-10 17:13:49 +09002228 int i;
Marek Vasute3334742011-11-06 00:59:52 +01002229#ifdef ONENAND_DEBUG
2230 char *name;
2231#endif
Kyungmin Park79e90462007-09-10 17:13:49 +09002232
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302233 for (i = 0; i < size; i++)
Kyungmin Park79e90462007-09-10 17:13:49 +09002234 if (manuf == onenand_manuf_ids[i].id)
2235 break;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002236
Marek Vasute3334742011-11-06 00:59:52 +01002237#ifdef ONENAND_DEBUG
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002238 if (i < size)
2239 name = onenand_manuf_ids[i].name;
2240 else
2241 name = "Unknown";
Kyungmin Park79e90462007-09-10 17:13:49 +09002242
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002243 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park79e90462007-09-10 17:13:49 +09002244#endif
2245
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002246 return i == size;
Kyungmin Park79e90462007-09-10 17:13:49 +09002247}
2248
2249/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302250* flexonenand_get_boundary - Reads the SLC boundary
2251* @param onenand_info - onenand info structure
2252*
2253* Fill up boundary[] field in onenand_chip
2254**/
2255static int flexonenand_get_boundary(struct mtd_info *mtd)
2256{
2257 struct onenand_chip *this = mtd->priv;
2258 unsigned int die, bdry;
Marek Vasute3334742011-11-06 00:59:52 +01002259 int syscfg, locked;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302260
2261 /* Disable ECC */
2262 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2263 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2264
2265 for (die = 0; die < this->dies; die++) {
2266 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2267 this->wait(mtd, FL_SYNCING);
2268
2269 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002270 this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302271
2272 bdry = this->read_word(this->base + ONENAND_DATARAM);
2273 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2274 locked = 0;
2275 else
2276 locked = 1;
2277 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2278
2279 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002280 this->wait(mtd, FL_RESETING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302281
2282 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2283 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2284 }
2285
2286 /* Enable ECC */
2287 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2288 return 0;
2289}
2290
2291/**
2292 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2293 * boundary[], diesize[], mtd->size, mtd->erasesize,
2294 * mtd->eraseregions
2295 * @param mtd - MTD device structure
2296 */
2297static void flexonenand_get_size(struct mtd_info *mtd)
2298{
2299 struct onenand_chip *this = mtd->priv;
2300 int die, i, eraseshift, density;
2301 int blksperdie, maxbdry;
2302 loff_t ofs;
2303
2304 density = onenand_get_density(this->device_id);
2305 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2306 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2307 maxbdry = blksperdie - 1;
2308 eraseshift = this->erase_shift - 1;
2309
2310 mtd->numeraseregions = this->dies << 1;
2311
2312 /* This fills up the device boundary */
2313 flexonenand_get_boundary(mtd);
2314 die = 0;
2315 ofs = 0;
2316 i = -1;
2317 for (; die < this->dies; die++) {
2318 if (!die || this->boundary[die-1] != maxbdry) {
2319 i++;
2320 mtd->eraseregions[i].offset = ofs;
2321 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2322 mtd->eraseregions[i].numblocks =
2323 this->boundary[die] + 1;
2324 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2325 eraseshift++;
2326 } else {
2327 mtd->numeraseregions -= 1;
2328 mtd->eraseregions[i].numblocks +=
2329 this->boundary[die] + 1;
2330 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2331 }
2332 if (this->boundary[die] != maxbdry) {
2333 i++;
2334 mtd->eraseregions[i].offset = ofs;
2335 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2336 mtd->eraseregions[i].numblocks = maxbdry ^
2337 this->boundary[die];
2338 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2339 eraseshift--;
2340 } else
2341 mtd->numeraseregions -= 1;
2342 }
2343
2344 /* Expose MLC erase size except when all blocks are SLC */
2345 mtd->erasesize = 1 << this->erase_shift;
2346 if (mtd->numeraseregions == 1)
2347 mtd->erasesize >>= 1;
2348
2349 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2350 for (i = 0; i < mtd->numeraseregions; i++)
2351 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2352 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2353 mtd->eraseregions[i].erasesize,
2354 mtd->eraseregions[i].numblocks);
2355
2356 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2357 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2358 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2359 << (this->erase_shift - 1);
2360 mtd->size += this->diesize[die];
2361 }
2362}
2363
2364/**
2365 * flexonenand_check_blocks_erased - Check if blocks are erased
2366 * @param mtd_info - mtd info structure
2367 * @param start - first erase block to check
2368 * @param end - last erase block to check
2369 *
2370 * Converting an unerased block from MLC to SLC
2371 * causes byte values to change. Since both data and its ECC
2372 * have changed, reads on the block give uncorrectable error.
2373 * This might lead to the block being detected as bad.
2374 *
2375 * Avoid this by ensuring that the block to be converted is
2376 * erased.
2377 */
2378static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2379 int start, int end)
2380{
2381 struct onenand_chip *this = mtd->priv;
2382 int i, ret;
2383 int block;
2384 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002385 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302386 .ooboffs = 0,
2387 .ooblen = mtd->oobsize,
2388 .datbuf = NULL,
2389 .oobbuf = this->oob_buf,
2390 };
2391 loff_t addr;
2392
2393 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2394
2395 for (block = start; block <= end; block++) {
2396 addr = flexonenand_addr(this, block);
2397 if (onenand_block_isbad_nolock(mtd, addr, 0))
2398 continue;
2399
2400 /*
2401 * Since main area write results in ECC write to spare,
2402 * it is sufficient to check only ECC bytes for change.
2403 */
2404 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2405 if (ret)
2406 return ret;
2407
2408 for (i = 0; i < mtd->oobsize; i++)
2409 if (this->oob_buf[i] != 0xff)
2410 break;
2411
2412 if (i != mtd->oobsize) {
2413 printk(KERN_WARNING "Block %d not erased.\n", block);
2414 return 1;
2415 }
2416 }
2417
2418 return 0;
2419}
2420
2421/**
2422 * flexonenand_set_boundary - Writes the SLC boundary
2423 * @param mtd - mtd info structure
2424 */
2425int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2426 int boundary, int lock)
2427{
2428 struct onenand_chip *this = mtd->priv;
2429 int ret, density, blksperdie, old, new, thisboundary;
2430 loff_t addr;
2431
2432 if (die >= this->dies)
2433 return -EINVAL;
2434
2435 if (boundary == this->boundary[die])
2436 return 0;
2437
2438 density = onenand_get_density(this->device_id);
2439 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2440 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2441
2442 if (boundary >= blksperdie) {
2443 printk("flexonenand_set_boundary:"
2444 "Invalid boundary value. "
2445 "Boundary not changed.\n");
2446 return -EINVAL;
2447 }
2448
2449 /* Check if converting blocks are erased */
2450 old = this->boundary[die] + (die * this->density_mask);
2451 new = boundary + (die * this->density_mask);
2452 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2453 + 1, max(old, new));
2454 if (ret) {
2455 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2456 return ret;
2457 }
2458
2459 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2460 this->wait(mtd, FL_SYNCING);
2461
2462 /* Check is boundary is locked */
2463 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2464 ret = this->wait(mtd, FL_READING);
2465
2466 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2467 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2468 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2469 goto out;
2470 }
2471
2472 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2473 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2474
2475 boundary &= FLEXONENAND_PI_MASK;
2476 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2477
2478 addr = die ? this->diesize[0] : 0;
2479 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2480 ret = this->wait(mtd, FL_ERASING);
2481 if (ret) {
2482 printk("flexonenand_set_boundary:"
2483 "Failed PI erase for Die %d\n", die);
2484 goto out;
2485 }
2486
2487 this->write_word(boundary, this->base + ONENAND_DATARAM);
2488 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2489 ret = this->wait(mtd, FL_WRITING);
2490 if (ret) {
2491 printk("flexonenand_set_boundary:"
2492 "Failed PI write for Die %d\n", die);
2493 goto out;
2494 }
2495
2496 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2497 ret = this->wait(mtd, FL_WRITING);
2498out:
2499 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2500 this->wait(mtd, FL_RESETING);
2501 if (!ret)
2502 /* Recalculate device size on boundary change*/
2503 flexonenand_get_size(mtd);
2504
2505 return ret;
2506}
2507
2508/**
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002509 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park79e90462007-09-10 17:13:49 +09002510 * @param mtd MTD device structure
2511 *
2512 * OneNAND detection method:
2513 * Compare the the values from command with ones from register
2514 */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002515static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park79e90462007-09-10 17:13:49 +09002516{
2517 struct onenand_chip *this = mtd->priv;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002518 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002519 int syscfg;
2520
2521 /* Save system configuration 1 */
2522 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002523
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002524 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002525 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2526 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park79e90462007-09-10 17:13:49 +09002527
2528 /* Send the command for reading device ID from BootRAM */
2529 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2530
2531 /* Read manufacturer and device IDs from BootRAM */
2532 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2533 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2534
Kyungmin Park79e90462007-09-10 17:13:49 +09002535 /* Reset OneNAND to read default register values */
2536 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2537
Kyungmin Park396b0c42008-08-13 09:11:02 +09002538 /* Wait reset */
2539 this->wait(mtd, FL_RESETING);
Kyungmin Park79e90462007-09-10 17:13:49 +09002540
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002541 /* Restore system configuration 1 */
2542 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2543
2544 /* Check manufacturer ID */
2545 if (onenand_check_maf(bram_maf_id))
2546 return -ENXIO;
2547
Kyungmin Park79e90462007-09-10 17:13:49 +09002548 /* Read manufacturer and device IDs from Register */
2549 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2550 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2551
2552 /* Check OneNAND device */
2553 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2554 return -ENXIO;
Kyungmin Park05d23182008-03-17 08:54:06 +09002555
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002556 return 0;
2557}
2558
2559/**
2560 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2561 * @param mtd MTD device structure
2562 *
2563 * OneNAND detection method:
2564 * Compare the the values from command with ones from register
2565 */
2566int onenand_probe(struct mtd_info *mtd)
2567{
2568 struct onenand_chip *this = mtd->priv;
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002569 int dev_id, ver_id;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002570 int density;
2571 int ret;
2572
2573 ret = this->chip_probe(mtd);
2574 if (ret)
2575 return ret;
2576
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002577 /* Read device IDs from Register */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002578 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2579 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2580 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2581
Kyungmin Park79e90462007-09-10 17:13:49 +09002582 /* Flash device information */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002583 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park79e90462007-09-10 17:13:49 +09002584 this->device_id = dev_id;
Stefan Roese7c9aafe2008-11-11 10:29:09 +01002585 this->version_id = ver_id;
Kyungmin Park79e90462007-09-10 17:13:49 +09002586
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002587 /* Check OneNAND features */
2588 onenand_check_features(mtd);
2589
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002590 density = onenand_get_density(dev_id);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302591 if (FLEXONENAND(this)) {
2592 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2593 /* Maximum possible erase regions */
2594 mtd->numeraseregions = this->dies << 1;
2595 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2596 * (this->dies << 1));
2597 if (!mtd->eraseregions)
2598 return -ENOMEM;
2599 }
2600
2601 /*
2602 * For Flex-OneNAND, chipsize represents maximum possible device size.
2603 * mtd->size represents the actual device size.
2604 */
Kyungmin Park79e90462007-09-10 17:13:49 +09002605 this->chipsize = (16 << density) << 20;
2606
2607 /* OneNAND page size & block size */
2608 /* The data buffer size is equal to page size */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002609 mtd->writesize =
Kyungmin Park79e90462007-09-10 17:13:49 +09002610 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302611 /* We use the full BufferRAM */
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002612 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302613 mtd->writesize <<= 1;
2614
Kyungmin Park396b0c42008-08-13 09:11:02 +09002615 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park79e90462007-09-10 17:13:49 +09002616 /* Pagers per block is always 64 in OneNAND */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002617 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302618 /*
2619 * Flex-OneNAND SLC area has 64 pages per block.
2620 * Flex-OneNAND MLC area has 128 pages per block.
2621 * Expose MLC erase size to find erase_shift and page_mask.
2622 */
2623 if (FLEXONENAND(this))
2624 mtd->erasesize <<= 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002625
2626 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002627 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002628 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Park396b0c42008-08-13 09:11:02 +09002629 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302630 /* Set density mask. it is used for DDP */
2631 if (ONENAND_IS_DDP(this))
2632 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002633 /* It's real page size */
2634 this->writesize = mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002635
2636 /* REVIST: Multichip handling */
2637
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302638 if (FLEXONENAND(this))
2639 flexonenand_get_size(mtd);
2640 else
2641 mtd->size = this->chipsize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002642
Kyungmin Park396b0c42008-08-13 09:11:02 +09002643 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002644 mtd->_erase = onenand_erase;
2645 mtd->_read = onenand_read;
2646 mtd->_write = onenand_write;
2647 mtd->_read_oob = onenand_read_oob;
2648 mtd->_write_oob = onenand_write_oob;
2649 mtd->_sync = onenand_sync;
2650 mtd->_block_isbad = onenand_block_isbad;
2651 mtd->_block_markbad = onenand_block_markbad;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002652
Kyungmin Park79e90462007-09-10 17:13:49 +09002653 return 0;
2654}
2655
2656/**
2657 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2658 * @param mtd MTD device structure
2659 * @param maxchips Number of chips to scan for
2660 *
2661 * This fills out all the not initialized function pointers
2662 * with the defaults.
2663 * The flash ID is read and the mtd/chip structures are
2664 * filled with the appropriate values.
2665 */
2666int onenand_scan(struct mtd_info *mtd, int maxchips)
2667{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002668 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002669 struct onenand_chip *this = mtd->priv;
2670
2671 if (!this->read_word)
2672 this->read_word = onenand_readw;
2673 if (!this->write_word)
2674 this->write_word = onenand_writew;
2675
2676 if (!this->command)
2677 this->command = onenand_command;
2678 if (!this->wait)
2679 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002680 if (!this->bbt_wait)
2681 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002682
2683 if (!this->read_bufferram)
2684 this->read_bufferram = onenand_read_bufferram;
2685 if (!this->write_bufferram)
2686 this->write_bufferram = onenand_write_bufferram;
2687
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002688 if (!this->chip_probe)
2689 this->chip_probe = onenand_chip_probe;
2690
Kyungmin Park87b49502008-11-13 15:14:33 +09002691 if (!this->block_markbad)
2692 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002693 if (!this->scan_bbt)
2694 this->scan_bbt = onenand_default_bbt;
2695
Kyungmin Park79e90462007-09-10 17:13:49 +09002696 if (onenand_probe(mtd))
2697 return -ENXIO;
2698
2699 /* Set Sync. Burst Read after probing */
2700 if (this->mmcontrol) {
2701 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2702 this->read_bufferram = onenand_sync_read_bufferram;
2703 }
2704
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002705 /* Allocate buffers, if necessary */
2706 if (!this->page_buf) {
2707 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2708 if (!this->page_buf) {
2709 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2710 return -ENOMEM;
2711 }
2712 this->options |= ONENAND_PAGEBUF_ALLOC;
2713 }
2714 if (!this->oob_buf) {
2715 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2716 if (!this->oob_buf) {
2717 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2718 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2719 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2720 kfree(this->page_buf);
2721 }
2722 return -ENOMEM;
2723 }
2724 this->options |= ONENAND_OOBBUF_ALLOC;
2725 }
2726
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002727 this->state = FL_READY;
2728
2729 /*
2730 * Allow subpage writes up to oobsize.
2731 */
2732 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302733 case 128:
2734 this->ecclayout = &onenand_oob_128;
2735 mtd->subpage_sft = 0;
2736 break;
2737
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002738 case 64:
2739 this->ecclayout = &onenand_oob_64;
2740 mtd->subpage_sft = 2;
2741 break;
2742
2743 case 32:
2744 this->ecclayout = &onenand_oob_32;
2745 mtd->subpage_sft = 1;
2746 break;
2747
2748 default:
2749 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2750 mtd->oobsize);
2751 mtd->subpage_sft = 0;
2752 /* To prevent kernel oops */
2753 this->ecclayout = &onenand_oob_32;
2754 break;
2755 }
2756
2757 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2758
2759 /*
2760 * The number of bytes available for a client to place data into
2761 * the out of band area
2762 */
2763 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302764
2765 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002766 this->ecclayout->oobfree[i].length; i++)
2767 this->ecclayout->oobavail +=
2768 this->ecclayout->oobfree[i].length;
2769 mtd->oobavail = this->ecclayout->oobavail;
2770
2771 mtd->ecclayout = this->ecclayout;
2772
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002773 /* Unlock whole block */
2774 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002775
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002776 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002777}
2778
2779/**
2780 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2781 * @param mtd MTD device structure
2782 */
2783void onenand_release(struct mtd_info *mtd)
2784{
2785}