Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/shli/md
[linux-2.6-block.git] / drivers / mtd / onenand / onenand_base.c
CommitLineData
cd5f6346
KP
1/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
3cf60253
AKS
4 * Copyright © 2005-2009 Samsung Electronics
5 * Copyright © 2007 Nokia Corporation
6 *
cd5f6346
KP
7 * Kyungmin Park <kyungmin.park@samsung.com>
8 *
81280d58
AH
9 * Credits:
10 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
11 * auto-placement support, read-while load support, various fixes
81280d58 12 *
5988af23
RH
13 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
14 * Flex-OneNAND support
3cf60253
AKS
15 * Amul Kumar Saha <amul.saha at samsung.com>
16 * OTP support
5988af23 17 *
cd5f6346
KP
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
c90173f0 25#include <linux/moduleparam.h>
5a0e3ad6 26#include <linux/slab.h>
015953d7 27#include <linux/sched.h>
6c77fd64 28#include <linux/delay.h>
2c22120f 29#include <linux/interrupt.h>
015953d7 30#include <linux/jiffies.h>
cd5f6346
KP
31#include <linux/mtd/mtd.h>
32#include <linux/mtd/onenand.h>
33#include <linux/mtd/partitions.h>
34
35#include <asm/io.h>
36
72073027
MK
37/*
38 * Multiblock erase if number of blocks to erase is 2 or more.
39 * Maximum number of blocks for simultaneous erase is 64.
40 */
41#define MB_ERASE_MIN_BLK_COUNT 2
42#define MB_ERASE_MAX_BLK_COUNT 64
43
5988af23
RH
44/* Default Flex-OneNAND boundary and lock respectively */
45static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
46
c90173f0
AS
47module_param_array(flex_bdry, int, NULL, 0400);
48MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
49 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
50 "DIE_BDRY: SLC boundary of the die"
51 "LOCK: Locking information for SLC boundary"
52 " : 0->Set boundary in unlocked status"
53 " : 1->Set boundary in locked status");
54
3cf60253
AKS
55/* Default OneNAND/Flex-OneNAND OTP options*/
56static int otp;
57
58module_param(otp, int, 0400);
59MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
60 "Syntax : otp=LOCK_TYPE"
61 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
62 " : 0 -> Default (No Blocks Locked)"
63 " : 1 -> OTP Block lock"
64 " : 2 -> 1st Block lock"
65 " : 3 -> BOTH OTP Block and 1st Block lock");
66
99b17c08
RT
67/*
68 * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
69 * For now, we expose only 64 out of 80 ecc bytes
5988af23 70 */
a411679f
BB
71static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section,
72 struct mtd_oob_region *oobregion)
73{
74 if (section > 7)
75 return -ERANGE;
76
77 oobregion->offset = (section * 16) + 6;
78 oobregion->length = 10;
79
80 return 0;
81}
82
83static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 7)
87 return -ERANGE;
88
89 oobregion->offset = (section * 16) + 2;
90 oobregion->length = 4;
91
92 return 0;
93}
94
95static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = {
96 .ecc = flexonenand_ooblayout_ecc,
97 .free = flexonenand_ooblayout_free,
5988af23
RH
98};
99
99b17c08
RT
100/*
101 * onenand_oob_128 - oob info for OneNAND with 4KB page
102 *
103 * Based on specification:
104 * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
105 *
a411679f
BB
106 */
107static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section,
108 struct mtd_oob_region *oobregion)
109{
110 if (section > 7)
111 return -ERANGE;
112
113 oobregion->offset = (section * 16) + 7;
114 oobregion->length = 9;
115
116 return 0;
117}
118
119static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section,
120 struct mtd_oob_region *oobregion)
121{
122 if (section >= 8)
123 return -ERANGE;
124
125 /*
126 * free bytes are using the spare area fields marked as
127 * "Managed by internal ECC logic for Logical Sector Number area"
128 */
129 oobregion->offset = (section * 16) + 2;
130 oobregion->length = 3;
131
132 return 0;
133}
134
135static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = {
136 .ecc = onenand_ooblayout_128_ecc,
137 .free = onenand_ooblayout_128_free,
99b17c08
RT
138};
139
cd5f6346 140/**
a411679f 141 * onenand_oob_32_64 - oob info for large (2KB) page
cd5f6346 142 */
a411679f
BB
143static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section,
144 struct mtd_oob_region *oobregion)
145{
146 if (section > 3)
147 return -ERANGE;
148
149 oobregion->offset = (section * 16) + 8;
150 oobregion->length = 5;
151
152 return 0;
153}
154
155static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section,
156 struct mtd_oob_region *oobregion)
157{
158 int sections = (mtd->oobsize / 32) * 2;
159
160 if (section >= sections)
161 return -ERANGE;
162
163 if (section & 1) {
164 oobregion->offset = ((section - 1) * 16) + 14;
165 oobregion->length = 2;
166 } else {
167 oobregion->offset = (section * 16) + 2;
168 oobregion->length = 3;
d9777f1c 169 }
cd5f6346 170
a411679f
BB
171 return 0;
172}
173
174static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = {
175 .ecc = onenand_ooblayout_32_64_ecc,
176 .free = onenand_ooblayout_32_64_free,
cd5f6346
KP
177};
178
179static const unsigned char ffchars[] = {
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
184 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
185 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
186 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
5988af23
RH
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
193 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
194 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
195 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
cd5f6346
KP
196};
197
198/**
199 * onenand_readw - [OneNAND Interface] Read OneNAND register
200 * @param addr address to read
201 *
202 * Read OneNAND register
203 */
204static unsigned short onenand_readw(void __iomem *addr)
205{
206 return readw(addr);
207}
208
209/**
210 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
211 * @param value value to write
212 * @param addr address to write
213 *
214 * Write OneNAND register with value
215 */
216static void onenand_writew(unsigned short value, void __iomem *addr)
217{
218 writew(value, addr);
219}
220
221/**
222 * onenand_block_address - [DEFAULT] Get block address
83a36838 223 * @param this onenand chip data structure
cd5f6346
KP
224 * @param block the block
225 * @return translated block address if DDP, otherwise same
226 *
227 * Setup Start Address 1 Register (F100h)
228 */
83a36838 229static int onenand_block_address(struct onenand_chip *this, int block)
cd5f6346 230{
738d61f5
KP
231 /* Device Flash Core select, NAND Flash Block Address */
232 if (block & this->density_mask)
233 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
cd5f6346
KP
234
235 return block;
236}
237
238/**
239 * onenand_bufferram_address - [DEFAULT] Get bufferram address
83a36838 240 * @param this onenand chip data structure
cd5f6346
KP
241 * @param block the block
242 * @return set DBS value if DDP, otherwise 0
243 *
244 * Setup Start Address 2 Register (F101h) for DDP
245 */
83a36838 246static int onenand_bufferram_address(struct onenand_chip *this, int block)
cd5f6346 247{
738d61f5
KP
248 /* Device BufferRAM Select */
249 if (block & this->density_mask)
250 return ONENAND_DDP_CHIP1;
cd5f6346 251
738d61f5 252 return ONENAND_DDP_CHIP0;
cd5f6346
KP
253}
254
255/**
256 * onenand_page_address - [DEFAULT] Get page address
257 * @param page the page address
258 * @param sector the sector address
259 * @return combined page and sector address
260 *
261 * Setup Start Address 8 Register (F107h)
262 */
263static int onenand_page_address(int page, int sector)
264{
265 /* Flash Page Address, Flash Sector Address */
266 int fpa, fsa;
267
268 fpa = page & ONENAND_FPA_MASK;
269 fsa = sector & ONENAND_FSA_MASK;
270
271 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
272}
273
274/**
275 * onenand_buffer_address - [DEFAULT] Get buffer address
276 * @param dataram1 DataRAM index
277 * @param sectors the sector address
278 * @param count the number of sectors
279 * @return the start buffer value
280 *
281 * Setup Start Buffer Register (F200h)
282 */
283static int onenand_buffer_address(int dataram1, int sectors, int count)
284{
285 int bsa, bsc;
286
287 /* BufferRAM Sector Address */
288 bsa = sectors & ONENAND_BSA_MASK;
289
290 if (dataram1)
291 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
292 else
293 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
294
295 /* BufferRAM Sector Count */
296 bsc = count & ONENAND_BSC_MASK;
297
298 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
299}
300
5988af23
RH
301/**
302 * flexonenand_block- For given address return block number
303 * @param this - OneNAND device structure
304 * @param addr - Address for which block number is needed
305 */
306static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
307{
308 unsigned boundary, blk, die = 0;
309
310 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
311 die = 1;
312 addr -= this->diesize[0];
313 }
314
315 boundary = this->boundary[die];
316
317 blk = addr >> (this->erase_shift - 1);
318 if (blk > boundary)
319 blk = (blk + boundary + 1) >> 1;
320
321 blk += die ? this->density_mask : 0;
322 return blk;
323}
324
325inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
326{
327 if (!FLEXONENAND(this))
328 return addr >> this->erase_shift;
329 return flexonenand_block(this, addr);
330}
331
332/**
333 * flexonenand_addr - Return address of the block
334 * @this: OneNAND device structure
335 * @block: Block number on Flex-OneNAND
336 *
337 * Return address of the block
338 */
339static loff_t flexonenand_addr(struct onenand_chip *this, int block)
340{
341 loff_t ofs = 0;
342 int die = 0, boundary;
343
344 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
345 block -= this->density_mask;
346 die = 1;
347 ofs = this->diesize[0];
348 }
349
350 boundary = this->boundary[die];
351 ofs += (loff_t)block << (this->erase_shift - 1);
352 if (block > (boundary + 1))
353 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
354 return ofs;
355}
356
357loff_t onenand_addr(struct onenand_chip *this, int block)
358{
359 if (!FLEXONENAND(this))
360 return (loff_t)block << this->erase_shift;
361 return flexonenand_addr(this, block);
362}
363EXPORT_SYMBOL(onenand_addr);
364
e71f04fc
KP
365/**
366 * onenand_get_density - [DEFAULT] Get OneNAND density
367 * @param dev_id OneNAND device ID
368 *
369 * Get OneNAND density from device ID
370 */
371static inline int onenand_get_density(int dev_id)
372{
373 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
374 return (density & ONENAND_DEVICE_DENSITY_MASK);
375}
376
5988af23
RH
377/**
378 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
379 * @param mtd MTD device structure
380 * @param addr address whose erase region needs to be identified
381 */
382int flexonenand_region(struct mtd_info *mtd, loff_t addr)
383{
384 int i;
385
386 for (i = 0; i < mtd->numeraseregions; i++)
387 if (addr < mtd->eraseregions[i].offset)
388 break;
389 return i - 1;
390}
391EXPORT_SYMBOL(flexonenand_region);
392
cd5f6346
KP
393/**
394 * onenand_command - [DEFAULT] Send command to OneNAND device
395 * @param mtd MTD device structure
396 * @param cmd the command to be sent
397 * @param addr offset to read from or write to
398 * @param len number of bytes to read or write
399 *
400 * Send command to OneNAND device. This function is used for middle/large page
401 * devices (1KB/2KB Bytes per page)
402 */
403static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
404{
405 struct onenand_chip *this = mtd->priv;
b21b72cf 406 int value, block, page;
cd5f6346
KP
407
408 /* Address translation */
409 switch (cmd) {
410 case ONENAND_CMD_UNLOCK:
411 case ONENAND_CMD_LOCK:
412 case ONENAND_CMD_LOCK_TIGHT:
28b79ff9 413 case ONENAND_CMD_UNLOCK_ALL:
cd5f6346
KP
414 block = -1;
415 page = -1;
416 break;
417
5988af23
RH
418 case FLEXONENAND_CMD_PI_ACCESS:
419 /* addr contains die index */
420 block = addr * this->density_mask;
421 page = -1;
422 break;
423
cd5f6346 424 case ONENAND_CMD_ERASE:
72073027
MK
425 case ONENAND_CMD_MULTIBLOCK_ERASE:
426 case ONENAND_CMD_ERASE_VERIFY:
cd5f6346 427 case ONENAND_CMD_BUFFERRAM:
493c6460 428 case ONENAND_CMD_OTP_ACCESS:
5988af23 429 block = onenand_block(this, addr);
cd5f6346
KP
430 page = -1;
431 break;
432
5988af23
RH
433 case FLEXONENAND_CMD_READ_PI:
434 cmd = ONENAND_CMD_READ;
435 block = addr * this->density_mask;
436 page = 0;
437 break;
438
cd5f6346 439 default:
5988af23 440 block = onenand_block(this, addr);
42b0aab1
RHS
441 if (FLEXONENAND(this))
442 page = (int) (addr - onenand_addr(this, block))>>\
443 this->page_shift;
444 else
445 page = (int) (addr >> this->page_shift);
ee9745fc
KP
446 if (ONENAND_IS_2PLANE(this)) {
447 /* Make the even block number */
448 block &= ~1;
449 /* Is it the odd plane? */
450 if (addr & this->writesize)
451 block++;
452 page >>= 1;
453 }
cd5f6346
KP
454 page &= this->page_mask;
455 break;
456 }
457
458 /* NOTE: The setting order of the registers is very important! */
459 if (cmd == ONENAND_CMD_BUFFERRAM) {
460 /* Select DataRAM for DDP */
83a36838 461 value = onenand_bufferram_address(this, block);
cd5f6346
KP
462 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
463
8a8f632d 464 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
ee9745fc
KP
465 /* It is always BufferRAM0 */
466 ONENAND_SET_BUFFERRAM0(this);
467 else
468 /* Switch to the next data buffer */
469 ONENAND_SET_NEXT_BUFFERRAM(this);
cd5f6346
KP
470
471 return 0;
472 }
473
474 if (block != -1) {
475 /* Write 'DFS, FBA' of Flash */
83a36838 476 value = onenand_block_address(this, block);
cd5f6346 477 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
3cecf69e 478
b21b72cf
KP
479 /* Select DataRAM for DDP */
480 value = onenand_bufferram_address(this, block);
481 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
cd5f6346
KP
482 }
483
484 if (page != -1) {
60d84f97 485 /* Now we use page size operation */
5988af23 486 int sectors = 0, count = 0;
cd5f6346
KP
487 int dataram;
488
489 switch (cmd) {
5988af23 490 case FLEXONENAND_CMD_RECOVER_LSB:
cd5f6346
KP
491 case ONENAND_CMD_READ:
492 case ONENAND_CMD_READOOB:
8a8f632d 493 if (ONENAND_IS_4KB_PAGE(this))
5988af23
RH
494 /* It is always BufferRAM0 */
495 dataram = ONENAND_SET_BUFFERRAM0(this);
496 else
497 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
cd5f6346
KP
498 break;
499
500 default:
ee9745fc
KP
501 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
502 cmd = ONENAND_CMD_2X_PROG;
cd5f6346
KP
503 dataram = ONENAND_CURRENT_BUFFERRAM(this);
504 break;
505 }
506
507 /* Write 'FPA, FSA' of Flash */
508 value = onenand_page_address(page, sectors);
509 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
510
511 /* Write 'BSA, BSC' of DataRAM */
512 value = onenand_buffer_address(dataram, sectors, count);
513 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
cd5f6346
KP
514 }
515
516 /* Interrupt clear */
517 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
518
519 /* Write command */
520 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
521
522 return 0;
523}
524
5988af23
RH
525/**
526 * onenand_read_ecc - return ecc status
527 * @param this onenand chip structure
528 */
529static inline int onenand_read_ecc(struct onenand_chip *this)
530{
531 int ecc, i, result = 0;
532
6a88c47b 533 if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
5988af23
RH
534 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
535
536 for (i = 0; i < 4; i++) {
6a88c47b 537 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
5988af23
RH
538 if (likely(!ecc))
539 continue;
540 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
541 return ONENAND_ECC_2BIT_ALL;
542 else
543 result = ONENAND_ECC_1BIT_ALL;
544 }
545
546 return result;
547}
548
cd5f6346
KP
549/**
550 * onenand_wait - [DEFAULT] wait until the command is done
551 * @param mtd MTD device structure
552 * @param state state to select the max. timeout value
553 *
554 * Wait for command done. This applies to all OneNAND command
555 * Read can take up to 30us, erase up to 2ms and program up to 350us
556 * according to general OneNAND specs
557 */
558static int onenand_wait(struct mtd_info *mtd, int state)
559{
560 struct onenand_chip * this = mtd->priv;
561 unsigned long timeout;
562 unsigned int flags = ONENAND_INT_MASTER;
563 unsigned int interrupt = 0;
2fd32d4a 564 unsigned int ctrl;
cd5f6346
KP
565
566 /* The 20 msec is enough */
567 timeout = jiffies + msecs_to_jiffies(20);
568 while (time_before(jiffies, timeout)) {
569 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
570
571 if (interrupt & flags)
572 break;
573
72073027 574 if (state != FL_READING && state != FL_PREPARING_ERASE)
cd5f6346
KP
575 cond_resched();
576 }
577 /* To get correct interrupt status in timeout case */
578 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
579
580 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
581
83973b87
KP
582 /*
583 * In the Spec. it checks the controller status first
584 * However if you get the correct information in case of
585 * power off recovery (POR) test, it should read ECC status first
586 */
cd5f6346 587 if (interrupt & ONENAND_INT_READ) {
5988af23 588 int ecc = onenand_read_ecc(this);
f4f91ac3 589 if (ecc) {
b3c9f8bf 590 if (ecc & ONENAND_ECC_2BIT_ALL) {
297758f8
AKS
591 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
592 __func__, ecc);
f4f91ac3 593 mtd->ecc_stats.failed++;
30a7eb29 594 return -EBADMSG;
49dc08ee 595 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
297758f8
AKS
596 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
597 __func__, ecc);
f4f91ac3 598 mtd->ecc_stats.corrected++;
49dc08ee 599 }
cd5f6346 600 }
9d032801 601 } else if (state == FL_READING) {
297758f8
AKS
602 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
603 __func__, ctrl, interrupt);
9d032801 604 return -EIO;
cd5f6346
KP
605 }
606
72073027
MK
607 if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
608 printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
609 __func__, ctrl, interrupt);
610 return -EIO;
611 }
612
613 if (!(interrupt & ONENAND_INT_MASTER)) {
614 printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
615 __func__, ctrl, interrupt);
616 return -EIO;
617 }
618
83973b87
KP
619 /* If there's controller error, it's a real error */
620 if (ctrl & ONENAND_CTRL_ERROR) {
297758f8
AKS
621 printk(KERN_ERR "%s: controller error = 0x%04x\n",
622 __func__, ctrl);
83973b87 623 if (ctrl & ONENAND_CTRL_LOCK)
297758f8 624 printk(KERN_ERR "%s: it's locked error.\n", __func__);
83973b87
KP
625 return -EIO;
626 }
627
cd5f6346
KP
628 return 0;
629}
630
2c22120f
KP
631/*
632 * onenand_interrupt - [DEFAULT] onenand interrupt handler
633 * @param irq onenand interrupt number
634 * @param dev_id interrupt data
635 *
636 * complete the work
637 */
638static irqreturn_t onenand_interrupt(int irq, void *data)
639{
06efcad0 640 struct onenand_chip *this = data;
2c22120f
KP
641
642 /* To handle shared interrupt */
643 if (!this->complete.done)
644 complete(&this->complete);
645
646 return IRQ_HANDLED;
647}
648
649/*
650 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
651 * @param mtd MTD device structure
652 * @param state state to select the max. timeout value
653 *
654 * Wait for command done.
655 */
656static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
657{
658 struct onenand_chip *this = mtd->priv;
659
2c22120f
KP
660 wait_for_completion(&this->complete);
661
662 return onenand_wait(mtd, state);
663}
664
665/*
666 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
667 * @param mtd MTD device structure
668 * @param state state to select the max. timeout value
669 *
670 * Try interrupt based wait (It is used one-time)
671 */
672static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
673{
674 struct onenand_chip *this = mtd->priv;
675 unsigned long remain, timeout;
676
677 /* We use interrupt wait first */
678 this->wait = onenand_interrupt_wait;
679
2c22120f
KP
680 timeout = msecs_to_jiffies(100);
681 remain = wait_for_completion_timeout(&this->complete, timeout);
682 if (!remain) {
683 printk(KERN_INFO "OneNAND: There's no interrupt. "
684 "We use the normal wait\n");
685
686 /* Release the irq */
687 free_irq(this->irq, this);
c9ac5977 688
2c22120f
KP
689 this->wait = onenand_wait;
690 }
691
692 return onenand_wait(mtd, state);
693}
694
695/*
696 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
697 * @param mtd MTD device structure
698 *
699 * There's two method to wait onenand work
700 * 1. polling - read interrupt status register
701 * 2. interrupt - use the kernel interrupt method
702 */
703static void onenand_setup_wait(struct mtd_info *mtd)
704{
705 struct onenand_chip *this = mtd->priv;
706 int syscfg;
707
708 init_completion(&this->complete);
709
710 if (this->irq <= 0) {
711 this->wait = onenand_wait;
712 return;
713 }
714
715 if (request_irq(this->irq, &onenand_interrupt,
716 IRQF_SHARED, "onenand", this)) {
717 /* If we can't get irq, use the normal wait */
718 this->wait = onenand_wait;
719 return;
720 }
721
722 /* Enable interrupt */
723 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
724 syscfg |= ONENAND_SYS_CFG1_IOBE;
725 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
726
727 this->wait = onenand_try_interrupt_wait;
728}
729
cd5f6346
KP
730/**
731 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
732 * @param mtd MTD data structure
733 * @param area BufferRAM area
734 * @return offset given area
735 *
736 * Return BufferRAM offset given area
737 */
738static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
739{
740 struct onenand_chip *this = mtd->priv;
741
742 if (ONENAND_CURRENT_BUFFERRAM(this)) {
ee9745fc 743 /* Note: the 'this->writesize' is a real page size */
cd5f6346 744 if (area == ONENAND_DATARAM)
ee9745fc 745 return this->writesize;
cd5f6346
KP
746 if (area == ONENAND_SPARERAM)
747 return mtd->oobsize;
748 }
749
750 return 0;
751}
752
753/**
754 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
755 * @param mtd MTD data structure
756 * @param area BufferRAM area
757 * @param buffer the databuffer to put/get data
758 * @param offset offset to read from or write to
759 * @param count number of bytes to read/write
760 *
761 * Read the BufferRAM area
762 */
763static int onenand_read_bufferram(struct mtd_info *mtd, int area,
764 unsigned char *buffer, int offset, size_t count)
765{
766 struct onenand_chip *this = mtd->priv;
767 void __iomem *bufferram;
768
769 bufferram = this->base + area;
770
771 bufferram += onenand_bufferram_offset(mtd, area);
772
9c01f87d
KP
773 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
774 unsigned short word;
775
776 /* Align with word(16-bit) size */
777 count--;
778
779 /* Read word and save byte */
780 word = this->read_word(bufferram + offset + count);
781 buffer[count] = (word & 0xff);
782 }
783
cd5f6346
KP
784 memcpy(buffer, bufferram + offset, count);
785
786 return 0;
787}
788
52b0eea7
KP
789/**
790 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
791 * @param mtd MTD data structure
792 * @param area BufferRAM area
793 * @param buffer the databuffer to put/get data
794 * @param offset offset to read from or write to
795 * @param count number of bytes to read/write
796 *
797 * Read the BufferRAM area with Sync. Burst Mode
798 */
799static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
800 unsigned char *buffer, int offset, size_t count)
801{
802 struct onenand_chip *this = mtd->priv;
803 void __iomem *bufferram;
804
805 bufferram = this->base + area;
806
807 bufferram += onenand_bufferram_offset(mtd, area);
808
809 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
810
9c01f87d
KP
811 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
812 unsigned short word;
813
814 /* Align with word(16-bit) size */
815 count--;
816
817 /* Read word and save byte */
818 word = this->read_word(bufferram + offset + count);
819 buffer[count] = (word & 0xff);
820 }
821
52b0eea7
KP
822 memcpy(buffer, bufferram + offset, count);
823
824 this->mmcontrol(mtd, 0);
825
826 return 0;
827}
828
cd5f6346
KP
829/**
830 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
831 * @param mtd MTD data structure
832 * @param area BufferRAM area
833 * @param buffer the databuffer to put/get data
834 * @param offset offset to read from or write to
835 * @param count number of bytes to read/write
836 *
837 * Write the BufferRAM area
838 */
839static int onenand_write_bufferram(struct mtd_info *mtd, int area,
840 const unsigned char *buffer, int offset, size_t count)
841{
842 struct onenand_chip *this = mtd->priv;
843 void __iomem *bufferram;
844
845 bufferram = this->base + area;
846
847 bufferram += onenand_bufferram_offset(mtd, area);
848
9c01f87d
KP
849 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
850 unsigned short word;
851 int byte_offset;
852
853 /* Align with word(16-bit) size */
854 count--;
855
856 /* Calculate byte access offset */
857 byte_offset = offset + count;
858
859 /* Read word and save byte */
860 word = this->read_word(bufferram + byte_offset);
861 word = (word & ~0xff) | buffer[count];
862 this->write_word(word, bufferram + byte_offset);
863 }
864
cd5f6346
KP
865 memcpy(bufferram + offset, buffer, count);
866
867 return 0;
868}
869
ee9745fc
KP
870/**
871 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
872 * @param mtd MTD data structure
873 * @param addr address to check
874 * @return blockpage address
875 *
876 * Get blockpage address at 2x program mode
877 */
878static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
879{
880 struct onenand_chip *this = mtd->priv;
881 int blockpage, block, page;
882
883 /* Calculate the even block number */
884 block = (int) (addr >> this->erase_shift) & ~1;
885 /* Is it the odd plane? */
886 if (addr & this->writesize)
887 block++;
888 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
889 blockpage = (block << 7) | page;
890
891 return blockpage;
892}
893
cd5f6346
KP
894/**
895 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
896 * @param mtd MTD data structure
897 * @param addr address to check
d5c5e78a 898 * @return 1 if there are valid data, otherwise 0
cd5f6346
KP
899 *
900 * Check bufferram if there is data we required
901 */
902static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
903{
904 struct onenand_chip *this = mtd->priv;
cde36b37 905 int blockpage, found = 0;
abf3c0f2 906 unsigned int i;
d5c5e78a 907
ee9745fc
KP
908 if (ONENAND_IS_2PLANE(this))
909 blockpage = onenand_get_2x_blockpage(mtd, addr);
910 else
911 blockpage = (int) (addr >> this->page_shift);
cd5f6346 912
abf3c0f2 913 /* Is there valid data? */
cd5f6346 914 i = ONENAND_CURRENT_BUFFERRAM(this);
abf3c0f2 915 if (this->bufferram[i].blockpage == blockpage)
cde36b37
AH
916 found = 1;
917 else {
918 /* Check another BufferRAM */
919 i = ONENAND_NEXT_BUFFERRAM(this);
920 if (this->bufferram[i].blockpage == blockpage) {
921 ONENAND_SET_NEXT_BUFFERRAM(this);
922 found = 1;
923 }
924 }
cd5f6346 925
cde36b37
AH
926 if (found && ONENAND_IS_DDP(this)) {
927 /* Select DataRAM for DDP */
5988af23 928 int block = onenand_block(this, addr);
cde36b37
AH
929 int value = onenand_bufferram_address(this, block);
930 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
abf3c0f2 931 }
cd5f6346 932
cde36b37 933 return found;
cd5f6346
KP
934}
935
936/**
937 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
938 * @param mtd MTD data structure
939 * @param addr address to update
940 * @param valid valid flag
941 *
942 * Update BufferRAM information
943 */
abf3c0f2 944static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
cd5f6346
KP
945 int valid)
946{
947 struct onenand_chip *this = mtd->priv;
abf3c0f2
KP
948 int blockpage;
949 unsigned int i;
d5c5e78a 950
ee9745fc
KP
951 if (ONENAND_IS_2PLANE(this))
952 blockpage = onenand_get_2x_blockpage(mtd, addr);
953 else
954 blockpage = (int) (addr >> this->page_shift);
cd5f6346 955
abf3c0f2
KP
956 /* Invalidate another BufferRAM */
957 i = ONENAND_NEXT_BUFFERRAM(this);
5b4246f1 958 if (this->bufferram[i].blockpage == blockpage)
abf3c0f2 959 this->bufferram[i].blockpage = -1;
cd5f6346
KP
960
961 /* Update BufferRAM */
962 i = ONENAND_CURRENT_BUFFERRAM(this);
abf3c0f2
KP
963 if (valid)
964 this->bufferram[i].blockpage = blockpage;
965 else
966 this->bufferram[i].blockpage = -1;
cd5f6346
KP
967}
968
480b9dfb
AH
969/**
970 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
971 * @param mtd MTD data structure
972 * @param addr start address to invalidate
973 * @param len length to invalidate
974 *
975 * Invalidate BufferRAM information
976 */
977static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
978 unsigned int len)
979{
980 struct onenand_chip *this = mtd->priv;
981 int i;
982 loff_t end_addr = addr + len;
983
984 /* Invalidate BufferRAM */
985 for (i = 0; i < MAX_BUFFERRAM; i++) {
986 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
987 if (buf_addr >= addr && buf_addr < end_addr)
988 this->bufferram[i].blockpage = -1;
989 }
990}
991
cd5f6346
KP
992/**
993 * onenand_get_device - [GENERIC] Get chip for selected access
994 * @param mtd MTD device structure
995 * @param new_state the state which is requested
996 *
997 * Get the device and lock it for exclusive access
998 */
a41371eb 999static int onenand_get_device(struct mtd_info *mtd, int new_state)
cd5f6346
KP
1000{
1001 struct onenand_chip *this = mtd->priv;
1002 DECLARE_WAITQUEUE(wait, current);
1003
1004 /*
1005 * Grab the lock and see if the device is available
1006 */
1007 while (1) {
1008 spin_lock(&this->chip_lock);
1009 if (this->state == FL_READY) {
1010 this->state = new_state;
1011 spin_unlock(&this->chip_lock);
cf24dc85
AH
1012 if (new_state != FL_PM_SUSPENDED && this->enable)
1013 this->enable(mtd);
cd5f6346
KP
1014 break;
1015 }
a41371eb
KP
1016 if (new_state == FL_PM_SUSPENDED) {
1017 spin_unlock(&this->chip_lock);
1018 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
1019 }
cd5f6346
KP
1020 set_current_state(TASK_UNINTERRUPTIBLE);
1021 add_wait_queue(&this->wq, &wait);
1022 spin_unlock(&this->chip_lock);
1023 schedule();
1024 remove_wait_queue(&this->wq, &wait);
1025 }
a41371eb
KP
1026
1027 return 0;
cd5f6346
KP
1028}
1029
1030/**
1031 * onenand_release_device - [GENERIC] release chip
1032 * @param mtd MTD device structure
1033 *
1034 * Deselect, release chip lock and wake up anyone waiting on the device
1035 */
1036static void onenand_release_device(struct mtd_info *mtd)
1037{
1038 struct onenand_chip *this = mtd->priv;
1039
cf24dc85
AH
1040 if (this->state != FL_PM_SUSPENDED && this->disable)
1041 this->disable(mtd);
cd5f6346
KP
1042 /* Release the chip */
1043 spin_lock(&this->chip_lock);
1044 this->state = FL_READY;
1045 wake_up(&this->wq);
1046 spin_unlock(&this->chip_lock);
1047}
1048
1049/**
7854d3f7 1050 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
d15057b7
KP
1051 * @param mtd MTD device structure
1052 * @param buf destination address
1053 * @param column oob offset to read from
1054 * @param thislen oob length to read
1055 */
1056static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
1057 int thislen)
1058{
1059 struct onenand_chip *this = mtd->priv;
d30aae6d
BB
1060 int ret;
1061
1062 this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
1063 mtd->oobsize);
1064 ret = mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
1065 column, thislen);
1066 if (ret)
1067 return ret;
d15057b7 1068
d15057b7
KP
1069 return 0;
1070}
1071
5988af23
RH
1072/**
1073 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1074 * @param mtd MTD device structure
1075 * @param addr address to recover
1076 * @param status return value from onenand_wait / onenand_bbt_wait
1077 *
1078 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1079 * lower page address and MSB page has higher page address in paired pages.
1080 * If power off occurs during MSB page program, the paired LSB page data can
1081 * become corrupt. LSB page recovery read is a way to read LSB page though page
1082 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1083 * read after power up, issue LSB page recovery read.
1084 */
1085static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1086{
1087 struct onenand_chip *this = mtd->priv;
1088 int i;
1089
1090 /* Recovery is only for Flex-OneNAND */
1091 if (!FLEXONENAND(this))
1092 return status;
1093
1094 /* check if we failed due to uncorrectable error */
d57f4054 1095 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
5988af23
RH
1096 return status;
1097
1098 /* check if address lies in MLC region */
1099 i = flexonenand_region(mtd, addr);
1100 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1101 return status;
1102
1103 /* We are attempting to reread, so decrement stats.failed
1104 * which was incremented by onenand_wait due to read failure
1105 */
297758f8
AKS
1106 printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1107 __func__);
5988af23
RH
1108 mtd->ecc_stats.failed--;
1109
1110 /* Issue the LSB page recovery command */
1111 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1112 return this->wait(mtd, FL_READING);
1113}
1114
1115/**
1116 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1117 * @param mtd MTD device structure
1118 * @param from offset to read from
1119 * @param ops: oob operation description structure
1120 *
1121 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1122 * So, read-while-load is not present.
1123 */
1124static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1125 struct mtd_oob_ops *ops)
1126{
1127 struct onenand_chip *this = mtd->priv;
1128 struct mtd_ecc_stats stats;
1129 size_t len = ops->len;
1130 size_t ooblen = ops->ooblen;
1131 u_char *buf = ops->datbuf;
1132 u_char *oobbuf = ops->oobbuf;
1133 int read = 0, column, thislen;
1134 int oobread = 0, oobcolumn, thisooblen, oobsize;
1135 int ret = 0;
1136 int writesize = this->writesize;
1137
0a32a102
BN
1138 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1139 (int)len);
5988af23 1140
29f1058a 1141 oobsize = mtd_oobavail(mtd, ops);
5988af23
RH
1142 oobcolumn = from & (mtd->oobsize - 1);
1143
1144 /* Do not allow reads past end of device */
1145 if (from + len > mtd->size) {
297758f8
AKS
1146 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1147 __func__);
5988af23
RH
1148 ops->retlen = 0;
1149 ops->oobretlen = 0;
1150 return -EINVAL;
1151 }
1152
1153 stats = mtd->ecc_stats;
1154
1155 while (read < len) {
1156 cond_resched();
1157
1158 thislen = min_t(int, writesize, len - read);
1159
1160 column = from & (writesize - 1);
1161 if (column + thislen > writesize)
1162 thislen = writesize - column;
1163
1164 if (!onenand_check_bufferram(mtd, from)) {
1165 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1166
1167 ret = this->wait(mtd, FL_READING);
1168 if (unlikely(ret))
1169 ret = onenand_recover_lsb(mtd, from, ret);
1170 onenand_update_bufferram(mtd, from, !ret);
d57f4054 1171 if (mtd_is_eccerr(ret))
5988af23 1172 ret = 0;
b085058f
AH
1173 if (ret)
1174 break;
5988af23
RH
1175 }
1176
1177 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1178 if (oobbuf) {
1179 thisooblen = oobsize - oobcolumn;
1180 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1181
0612b9dd 1182 if (ops->mode == MTD_OPS_AUTO_OOB)
5988af23
RH
1183 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1184 else
1185 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1186 oobread += thisooblen;
1187 oobbuf += thisooblen;
1188 oobcolumn = 0;
1189 }
1190
1191 read += thislen;
1192 if (read == len)
1193 break;
1194
1195 from += thislen;
1196 buf += thislen;
1197 }
1198
1199 /*
1200 * Return success, if no ECC failures, else -EBADMSG
1201 * fs driver will take care of that, because
1202 * retlen == desired len and result == -EBADMSG
1203 */
1204 ops->retlen = read;
1205 ops->oobretlen = oobread;
1206
1207 if (ret)
1208 return ret;
1209
1210 if (mtd->ecc_stats.failed - stats.failed)
1211 return -EBADMSG;
1212
edbc4540
MD
1213 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1214 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
5988af23
RH
1215}
1216
d15057b7 1217/**
49dc08ee 1218 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
cd5f6346
KP
1219 * @param mtd MTD device structure
1220 * @param from offset to read from
d15057b7 1221 * @param ops: oob operation description structure
cd5f6346 1222 *
d15057b7
KP
1223 * OneNAND read main and/or out-of-band data
1224 */
49dc08ee 1225static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
d15057b7 1226 struct mtd_oob_ops *ops)
cd5f6346
KP
1227{
1228 struct onenand_chip *this = mtd->priv;
f4f91ac3 1229 struct mtd_ecc_stats stats;
d15057b7
KP
1230 size_t len = ops->len;
1231 size_t ooblen = ops->ooblen;
1232 u_char *buf = ops->datbuf;
1233 u_char *oobbuf = ops->oobbuf;
1234 int read = 0, column, thislen;
1235 int oobread = 0, oobcolumn, thisooblen, oobsize;
0fc2ccea 1236 int ret = 0, boundary = 0;
ee9745fc 1237 int writesize = this->writesize;
cd5f6346 1238
0a32a102
BN
1239 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1240 (int)len);
d15057b7 1241
29f1058a 1242 oobsize = mtd_oobavail(mtd, ops);
d15057b7 1243 oobcolumn = from & (mtd->oobsize - 1);
cd5f6346
KP
1244
1245 /* Do not allow reads past end of device */
1246 if ((from + len) > mtd->size) {
297758f8
AKS
1247 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1248 __func__);
d15057b7
KP
1249 ops->retlen = 0;
1250 ops->oobretlen = 0;
cd5f6346
KP
1251 return -EINVAL;
1252 }
1253
f4f91ac3 1254 stats = mtd->ecc_stats;
61a7e198 1255
a8de85d5
AH
1256 /* Read-while-load method */
1257
1258 /* Do first load to bufferRAM */
1259 if (read < len) {
1260 if (!onenand_check_bufferram(mtd, from)) {
ee9745fc 1261 this->command(mtd, ONENAND_CMD_READ, from, writesize);
a8de85d5
AH
1262 ret = this->wait(mtd, FL_READING);
1263 onenand_update_bufferram(mtd, from, !ret);
d57f4054 1264 if (mtd_is_eccerr(ret))
5f4d47d5 1265 ret = 0;
a8de85d5
AH
1266 }
1267 }
1268
ee9745fc
KP
1269 thislen = min_t(int, writesize, len - read);
1270 column = from & (writesize - 1);
1271 if (column + thislen > writesize)
1272 thislen = writesize - column;
a8de85d5
AH
1273
1274 while (!ret) {
1275 /* If there is more to load then start next load */
1276 from += thislen;
1277 if (read + thislen < len) {
ee9745fc 1278 this->command(mtd, ONENAND_CMD_READ, from, writesize);
0fc2ccea
AH
1279 /*
1280 * Chip boundary handling in DDP
1281 * Now we issued chip 1 read and pointed chip 1
492e1501 1282 * bufferram so we have to point chip 0 bufferram.
0fc2ccea 1283 */
738d61f5
KP
1284 if (ONENAND_IS_DDP(this) &&
1285 unlikely(from == (this->chipsize >> 1))) {
1286 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
0fc2ccea
AH
1287 boundary = 1;
1288 } else
1289 boundary = 0;
a8de85d5
AH
1290 ONENAND_SET_PREV_BUFFERRAM(this);
1291 }
1292 /* While load is going, read from last bufferRAM */
1293 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
d15057b7
KP
1294
1295 /* Read oob area if needed */
1296 if (oobbuf) {
1297 thisooblen = oobsize - oobcolumn;
1298 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1299
0612b9dd 1300 if (ops->mode == MTD_OPS_AUTO_OOB)
d15057b7
KP
1301 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1302 else
1303 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1304 oobread += thisooblen;
1305 oobbuf += thisooblen;
1306 oobcolumn = 0;
1307 }
1308
a8de85d5
AH
1309 /* See if we are done */
1310 read += thislen;
1311 if (read == len)
1312 break;
1313 /* Set up for next read from bufferRAM */
0fc2ccea 1314 if (unlikely(boundary))
738d61f5 1315 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
a8de85d5
AH
1316 ONENAND_SET_NEXT_BUFFERRAM(this);
1317 buf += thislen;
ee9745fc 1318 thislen = min_t(int, writesize, len - read);
a8de85d5
AH
1319 column = 0;
1320 cond_resched();
1321 /* Now wait for load */
1322 ret = this->wait(mtd, FL_READING);
1323 onenand_update_bufferram(mtd, from, !ret);
d57f4054 1324 if (mtd_is_eccerr(ret))
5f4d47d5 1325 ret = 0;
a8de85d5 1326 }
cd5f6346 1327
cd5f6346
KP
1328 /*
1329 * Return success, if no ECC failures, else -EBADMSG
1330 * fs driver will take care of that, because
1331 * retlen == desired len and result == -EBADMSG
1332 */
d15057b7
KP
1333 ops->retlen = read;
1334 ops->oobretlen = oobread;
f4f91ac3 1335
a8de85d5
AH
1336 if (ret)
1337 return ret;
1338
5f4d47d5
AH
1339 if (mtd->ecc_stats.failed - stats.failed)
1340 return -EBADMSG;
1341
edbc4540
MD
1342 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1343 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
cd5f6346
KP
1344}
1345
cd5f6346 1346/**
49dc08ee 1347 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
cd5f6346
KP
1348 * @param mtd MTD device structure
1349 * @param from offset to read from
d15057b7 1350 * @param ops: oob operation description structure
cd5f6346
KP
1351 *
1352 * OneNAND read out-of-band data from the spare area
1353 */
49dc08ee 1354static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
12f77c9e 1355 struct mtd_oob_ops *ops)
cd5f6346
KP
1356{
1357 struct onenand_chip *this = mtd->priv;
5f4d47d5 1358 struct mtd_ecc_stats stats;
a5e7c7b4 1359 int read = 0, thislen, column, oobsize;
12f77c9e 1360 size_t len = ops->ooblen;
905c6bcd 1361 unsigned int mode = ops->mode;
12f77c9e 1362 u_char *buf = ops->oobbuf;
5988af23 1363 int ret = 0, readcmd;
cd5f6346 1364
12f77c9e
KP
1365 from += ops->ooboffs;
1366
0a32a102
BN
1367 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1368 (int)len);
cd5f6346
KP
1369
1370 /* Initialize return length value */
12f77c9e 1371 ops->oobretlen = 0;
cd5f6346 1372
0612b9dd 1373 if (mode == MTD_OPS_AUTO_OOB)
f5b8aa78 1374 oobsize = mtd->oobavail;
a5e7c7b4
AH
1375 else
1376 oobsize = mtd->oobsize;
1377
1378 column = from & (mtd->oobsize - 1);
1379
1380 if (unlikely(column >= oobsize)) {
297758f8
AKS
1381 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1382 __func__);
a5e7c7b4
AH
1383 return -EINVAL;
1384 }
1385
5f4d47d5
AH
1386 stats = mtd->ecc_stats;
1387
8a8f632d 1388 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
5988af23 1389
cd5f6346 1390 while (read < len) {
61a7e198
AB
1391 cond_resched();
1392
a5e7c7b4 1393 thislen = oobsize - column;
cd5f6346
KP
1394 thislen = min_t(int, thislen, len);
1395
5988af23 1396 this->command(mtd, readcmd, from, mtd->oobsize);
cd5f6346
KP
1397
1398 onenand_update_bufferram(mtd, from, 0);
1399
1400 ret = this->wait(mtd, FL_READING);
5988af23
RH
1401 if (unlikely(ret))
1402 ret = onenand_recover_lsb(mtd, from, ret);
1403
d57f4054 1404 if (ret && !mtd_is_eccerr(ret)) {
297758f8
AKS
1405 printk(KERN_ERR "%s: read failed = 0x%x\n",
1406 __func__, ret);
5f4d47d5
AH
1407 break;
1408 }
cd5f6346 1409
0612b9dd 1410 if (mode == MTD_OPS_AUTO_OOB)
a5e7c7b4
AH
1411 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1412 else
1413 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
cd5f6346
KP
1414
1415 read += thislen;
1416
1417 if (read == len)
1418 break;
1419
cd5f6346
KP
1420 buf += thislen;
1421
1422 /* Read more? */
1423 if (read < len) {
1424 /* Page size */
28318776 1425 from += mtd->writesize;
cd5f6346
KP
1426 column = 0;
1427 }
1428 }
1429
12f77c9e 1430 ops->oobretlen = read;
5f4d47d5
AH
1431
1432 if (ret)
1433 return ret;
1434
1435 if (mtd->ecc_stats.failed - stats.failed)
1436 return -EBADMSG;
1437
1438 return 0;
cd5f6346
KP
1439}
1440
d15057b7
KP
1441/**
1442 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
e3da8067
KP
1443 * @param mtd: MTD device structure
1444 * @param from: offset to read from
1445 * @param ops: oob operation description structure
d15057b7
KP
1446
1447 * Read main and/or out-of-band
8593fbc6
TG
1448 */
1449static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1450 struct mtd_oob_ops *ops)
1451{
5988af23 1452 struct onenand_chip *this = mtd->priv;
49dc08ee
AB
1453 int ret;
1454
4f4fad27 1455 switch (ops->mode) {
0612b9dd
BN
1456 case MTD_OPS_PLACE_OOB:
1457 case MTD_OPS_AUTO_OOB:
a5e7c7b4 1458 break;
0612b9dd 1459 case MTD_OPS_RAW:
4f4fad27 1460 /* Not implemented yet */
a5e7c7b4
AH
1461 default:
1462 return -EINVAL;
1463 }
d15057b7 1464
49dc08ee 1465 onenand_get_device(mtd, FL_READING);
d15057b7 1466 if (ops->datbuf)
8a8f632d 1467 ret = ONENAND_IS_4KB_PAGE(this) ?
5988af23
RH
1468 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1469 onenand_read_ops_nolock(mtd, from, ops);
49dc08ee
AB
1470 else
1471 ret = onenand_read_oob_nolock(mtd, from, ops);
1472 onenand_release_device(mtd);
d15057b7 1473
49dc08ee 1474 return ret;
8593fbc6
TG
1475}
1476
211ac75f
KP
1477/**
1478 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1479 * @param mtd MTD device structure
1480 * @param state state to select the max. timeout value
1481 *
1482 * Wait for command done.
1483 */
1484static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1485{
1486 struct onenand_chip *this = mtd->priv;
1487 unsigned long timeout;
e0c1a921 1488 unsigned int interrupt, ctrl, ecc, addr1, addr8;
211ac75f
KP
1489
1490 /* The 20 msec is enough */
1491 timeout = jiffies + msecs_to_jiffies(20);
1492 while (time_before(jiffies, timeout)) {
1493 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1494 if (interrupt & ONENAND_INT_MASTER)
1495 break;
1496 }
1497 /* To get correct interrupt status in timeout case */
1498 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1499 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
e0c1a921
AH
1500 addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
1501 addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
211ac75f 1502
211ac75f 1503 if (interrupt & ONENAND_INT_READ) {
e0c1a921 1504 ecc = onenand_read_ecc(this);
83973b87 1505 if (ecc & ONENAND_ECC_2BIT_ALL) {
e0c1a921
AH
1506 printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
1507 "intr 0x%04x addr1 %#x addr8 %#x\n",
1508 __func__, ecc, ctrl, interrupt, addr1, addr8);
5988af23 1509 return ONENAND_BBT_READ_ECC_ERROR;
83973b87 1510 }
211ac75f 1511 } else {
e0c1a921
AH
1512 printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
1513 "intr 0x%04x addr1 %#x addr8 %#x\n",
1514 __func__, ctrl, interrupt, addr1, addr8);
211ac75f
KP
1515 return ONENAND_BBT_READ_FATAL_ERROR;
1516 }
1517
83973b87
KP
1518 /* Initial bad block case: 0x2400 or 0x0400 */
1519 if (ctrl & ONENAND_CTRL_ERROR) {
e0c1a921
AH
1520 printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1521 "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
83973b87
KP
1522 return ONENAND_BBT_READ_ERROR;
1523 }
1524
211ac75f
KP
1525 return 0;
1526}
1527
1528/**
1529 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1530 * @param mtd MTD device structure
1531 * @param from offset to read from
e3da8067 1532 * @param ops oob operation description structure
211ac75f
KP
1533 *
1534 * OneNAND read out-of-band data from the spare area for bbt scan
1535 */
1536int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1537 struct mtd_oob_ops *ops)
1538{
1539 struct onenand_chip *this = mtd->priv;
1540 int read = 0, thislen, column;
5988af23 1541 int ret = 0, readcmd;
211ac75f
KP
1542 size_t len = ops->ooblen;
1543 u_char *buf = ops->oobbuf;
1544
0a32a102
BN
1545 pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
1546 len);
211ac75f
KP
1547
1548 /* Initialize return value */
1549 ops->oobretlen = 0;
1550
1551 /* Do not allow reads past end of device */
1552 if (unlikely((from + len) > mtd->size)) {
297758f8
AKS
1553 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1554 __func__);
211ac75f
KP
1555 return ONENAND_BBT_READ_FATAL_ERROR;
1556 }
1557
1558 /* Grab the lock and see if the device is available */
1559 onenand_get_device(mtd, FL_READING);
1560
1561 column = from & (mtd->oobsize - 1);
1562
8a8f632d 1563 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
5988af23 1564
211ac75f
KP
1565 while (read < len) {
1566 cond_resched();
1567
1568 thislen = mtd->oobsize - column;
1569 thislen = min_t(int, thislen, len);
1570
5988af23 1571 this->command(mtd, readcmd, from, mtd->oobsize);
211ac75f
KP
1572
1573 onenand_update_bufferram(mtd, from, 0);
1574
31bb999e 1575 ret = this->bbt_wait(mtd, FL_READING);
5988af23
RH
1576 if (unlikely(ret))
1577 ret = onenand_recover_lsb(mtd, from, ret);
1578
211ac75f
KP
1579 if (ret)
1580 break;
1581
1582 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1583 read += thislen;
1584 if (read == len)
1585 break;
1586
1587 buf += thislen;
1588
1589 /* Read more? */
1590 if (read < len) {
1591 /* Update Page size */
ee9745fc 1592 from += this->writesize;
211ac75f
KP
1593 column = 0;
1594 }
1595 }
1596
1597 /* Deselect and wake up anyone waiting on the device */
1598 onenand_release_device(mtd);
1599
1600 ops->oobretlen = read;
1601 return ret;
1602}
1603
cd5f6346 1604#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
8e6ec690
KP
1605/**
1606 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1607 * @param mtd MTD device structure
1608 * @param buf the databuffer to verify
1609 * @param to offset to read from
8e6ec690 1610 */
a5e7c7b4 1611static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
8e6ec690
KP
1612{
1613 struct onenand_chip *this = mtd->priv;
69d79186 1614 u_char *oob_buf = this->oob_buf;
5988af23
RH
1615 int status, i, readcmd;
1616
8a8f632d 1617 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
8e6ec690 1618
5988af23 1619 this->command(mtd, readcmd, to, mtd->oobsize);
8e6ec690
KP
1620 onenand_update_bufferram(mtd, to, 0);
1621 status = this->wait(mtd, FL_READING);
1622 if (status)
1623 return status;
1624
69d79186 1625 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
91014e9b 1626 for (i = 0; i < mtd->oobsize; i++)
69d79186 1627 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
8e6ec690
KP
1628 return -EBADMSG;
1629
1630 return 0;
1631}
1632
cd5f6346 1633/**
8b29c0b6
AH
1634 * onenand_verify - [GENERIC] verify the chip contents after a write
1635 * @param mtd MTD device structure
1636 * @param buf the databuffer to verify
1637 * @param addr offset to read from
1638 * @param len number of bytes to read and compare
cd5f6346 1639 */
8b29c0b6 1640static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
cd5f6346
KP
1641{
1642 struct onenand_chip *this = mtd->priv;
cd5f6346 1643 int ret = 0;
8b29c0b6 1644 int thislen, column;
cd5f6346 1645
e6da8568
RT
1646 column = addr & (this->writesize - 1);
1647
8b29c0b6 1648 while (len != 0) {
e6da8568 1649 thislen = min_t(int, this->writesize - column, len);
60d84f97 1650
ee9745fc 1651 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
cd5f6346 1652
8b29c0b6
AH
1653 onenand_update_bufferram(mtd, addr, 0);
1654
1655 ret = this->wait(mtd, FL_READING);
1656 if (ret)
1657 return ret;
cd5f6346 1658
8b29c0b6 1659 onenand_update_bufferram(mtd, addr, 1);
cd5f6346 1660
3328dc31 1661 this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
cd5f6346 1662
e6da8568 1663 if (memcmp(buf, this->verify_buf + column, thislen))
8b29c0b6
AH
1664 return -EBADMSG;
1665
1666 len -= thislen;
1667 buf += thislen;
1668 addr += thislen;
e6da8568 1669 column = 0;
8b29c0b6 1670 }
d5c5e78a 1671
cd5f6346
KP
1672 return 0;
1673}
1674#else
8b29c0b6 1675#define onenand_verify(...) (0)
8e6ec690 1676#define onenand_verify_oob(...) (0)
cd5f6346
KP
1677#endif
1678
60d84f97 1679#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
cd5f6346 1680
6c77fd64
RP
1681static void onenand_panic_wait(struct mtd_info *mtd)
1682{
1683 struct onenand_chip *this = mtd->priv;
1684 unsigned int interrupt;
1685 int i;
1686
1687 for (i = 0; i < 2000; i++) {
1688 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1689 if (interrupt & ONENAND_INT_MASTER)
1690 break;
1691 udelay(10);
1692 }
1693}
1694
1695/**
1696 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1697 * @param mtd MTD device structure
1698 * @param to offset to write to
1699 * @param len number of bytes to write
1700 * @param retlen pointer to variable to store the number of written bytes
1701 * @param buf the data to write
1702 *
1703 * Write with ECC
1704 */
1705static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1706 size_t *retlen, const u_char *buf)
1707{
1708 struct onenand_chip *this = mtd->priv;
1709 int column, subpage;
1710 int written = 0;
6c77fd64
RP
1711
1712 if (this->state == FL_PM_SUSPENDED)
1713 return -EBUSY;
1714
1715 /* Wait for any existing operation to clear */
1716 onenand_panic_wait(mtd);
1717
0a32a102
BN
1718 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1719 (int)len);
6c77fd64 1720
6c77fd64 1721 /* Reject writes, which are not page aligned */
b73d7e43 1722 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
297758f8
AKS
1723 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1724 __func__);
6c77fd64
RP
1725 return -EINVAL;
1726 }
1727
1728 column = to & (mtd->writesize - 1);
1729
1730 /* Loop until all data write */
1731 while (written < len) {
1732 int thislen = min_t(int, mtd->writesize - column, len - written);
1733 u_char *wbuf = (u_char *) buf;
1734
1735 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1736
1737 /* Partial page write */
1738 subpage = thislen < mtd->writesize;
1739 if (subpage) {
1740 memset(this->page_buf, 0xff, mtd->writesize);
1741 memcpy(this->page_buf + column, buf, thislen);
1742 wbuf = this->page_buf;
1743 }
1744
1745 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1746 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1747
1748 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1749
1750 onenand_panic_wait(mtd);
1751
1752 /* In partial page write we don't update bufferram */
7f2a7ce1 1753 onenand_update_bufferram(mtd, to, !subpage);
6c77fd64
RP
1754 if (ONENAND_IS_2PLANE(this)) {
1755 ONENAND_SET_BUFFERRAM1(this);
7f2a7ce1 1756 onenand_update_bufferram(mtd, to + this->writesize, !subpage);
6c77fd64
RP
1757 }
1758
1759 written += thislen;
1760
1761 if (written == len)
1762 break;
1763
1764 column = 0;
1765 to += thislen;
1766 buf += thislen;
1767 }
1768
1769 *retlen = written;
7f2a7ce1 1770 return 0;
6c77fd64
RP
1771}
1772
cd5f6346 1773/**
7854d3f7 1774 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
d15057b7
KP
1775 * @param mtd MTD device structure
1776 * @param oob_buf oob buffer
1777 * @param buf source address
1778 * @param column oob offset to write to
1779 * @param thislen oob length to write
1780 */
1781static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1782 const u_char *buf, int column, int thislen)
1783{
d30aae6d 1784 return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen);
d15057b7
KP
1785}
1786
1787/**
49dc08ee 1788 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
cd5f6346
KP
1789 * @param mtd MTD device structure
1790 * @param to offset to write to
d15057b7 1791 * @param ops oob operation description structure
cd5f6346 1792 *
d15057b7 1793 * Write main and/or oob with ECC
cd5f6346 1794 */
49dc08ee 1795static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
d15057b7 1796 struct mtd_oob_ops *ops)
cd5f6346
KP
1797{
1798 struct onenand_chip *this = mtd->priv;
9ce96908
KP
1799 int written = 0, column, thislen = 0, subpage = 0;
1800 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
d15057b7
KP
1801 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1802 size_t len = ops->len;
1803 size_t ooblen = ops->ooblen;
1804 const u_char *buf = ops->datbuf;
1805 const u_char *oob = ops->oobbuf;
1806 u_char *oobbuf;
ac80dac0 1807 int ret = 0, cmd;
cd5f6346 1808
0a32a102
BN
1809 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1810 (int)len);
cd5f6346
KP
1811
1812 /* Initialize retlen, in case of early exit */
d15057b7
KP
1813 ops->retlen = 0;
1814 ops->oobretlen = 0;
cd5f6346 1815
cd5f6346 1816 /* Reject writes, which are not page aligned */
b73d7e43 1817 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
297758f8
AKS
1818 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1819 __func__);
cd5f6346
KP
1820 return -EINVAL;
1821 }
1822
9ce96908
KP
1823 /* Check zero length */
1824 if (!len)
1825 return 0;
29f1058a 1826 oobsize = mtd_oobavail(mtd, ops);
d15057b7
KP
1827 oobcolumn = to & (mtd->oobsize - 1);
1828
60d84f97 1829 column = to & (mtd->writesize - 1);
60d84f97 1830
cd5f6346 1831 /* Loop until all data write */
9ce96908
KP
1832 while (1) {
1833 if (written < len) {
1834 u_char *wbuf = (u_char *) buf;
60d84f97 1835
9ce96908
KP
1836 thislen = min_t(int, mtd->writesize - column, len - written);
1837 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
d15057b7 1838
9ce96908 1839 cond_resched();
61a7e198 1840
9ce96908 1841 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
60d84f97 1842
9ce96908
KP
1843 /* Partial page write */
1844 subpage = thislen < mtd->writesize;
1845 if (subpage) {
1846 memset(this->page_buf, 0xff, mtd->writesize);
1847 memcpy(this->page_buf + column, buf, thislen);
1848 wbuf = this->page_buf;
1849 }
cd5f6346 1850
9ce96908 1851 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
d15057b7 1852
9ce96908
KP
1853 if (oob) {
1854 oobbuf = this->oob_buf;
d15057b7 1855
9ce96908
KP
1856 /* We send data to spare ram with oobsize
1857 * to prevent byte access */
1858 memset(oobbuf, 0xff, mtd->oobsize);
0612b9dd 1859 if (ops->mode == MTD_OPS_AUTO_OOB)
9ce96908
KP
1860 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1861 else
1862 memcpy(oobbuf + oobcolumn, oob, thisooblen);
d15057b7 1863
9ce96908
KP
1864 oobwritten += thisooblen;
1865 oob += thisooblen;
1866 oobcolumn = 0;
1867 } else
1868 oobbuf = (u_char *) ffchars;
1869
1870 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
d15057b7 1871 } else
9ce96908 1872 ONENAND_SET_NEXT_BUFFERRAM(this);
d15057b7 1873
9ce96908 1874 /*
492e1501
MK
1875 * 2 PLANE, MLC, and Flex-OneNAND do not support
1876 * write-while-program feature.
9ce96908 1877 */
6a88c47b 1878 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
9ce96908
KP
1879 ONENAND_SET_PREV_BUFFERRAM(this);
1880
1881 ret = this->wait(mtd, FL_WRITING);
1882
1883 /* In partial page write we don't update bufferram */
1884 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1885 if (ret) {
1886 written -= prevlen;
297758f8
AKS
1887 printk(KERN_ERR "%s: write failed %d\n",
1888 __func__, ret);
9ce96908
KP
1889 break;
1890 }
cd5f6346 1891
9ce96908
KP
1892 if (written == len) {
1893 /* Only check verify write turn on */
1894 ret = onenand_verify(mtd, buf - len, to - len, len);
1895 if (ret)
297758f8
AKS
1896 printk(KERN_ERR "%s: verify failed %d\n",
1897 __func__, ret);
9ce96908
KP
1898 break;
1899 }
cd5f6346 1900
9ce96908
KP
1901 ONENAND_SET_NEXT_BUFFERRAM(this);
1902 }
81f38e11 1903
ac80dac0
RT
1904 this->ongoing = 0;
1905 cmd = ONENAND_CMD_PROG;
1906
1907 /* Exclude 1st OTP and OTP blocks for cache program feature */
1908 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1909 likely(onenand_block(this, to) != 0) &&
1910 ONENAND_IS_4KB_PAGE(this) &&
1911 ((written + thislen) < len)) {
1912 cmd = ONENAND_CMD_2X_CACHE_PROG;
1913 this->ongoing = 1;
1914 }
1915
1916 this->command(mtd, cmd, to, mtd->writesize);
9ce96908
KP
1917
1918 /*
1919 * 2 PLANE, MLC, and Flex-OneNAND wait here
1920 */
6a88c47b 1921 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
9ce96908 1922 ret = this->wait(mtd, FL_WRITING);
cd5f6346 1923
9ce96908
KP
1924 /* In partial page write we don't update bufferram */
1925 onenand_update_bufferram(mtd, to, !ret && !subpage);
1926 if (ret) {
297758f8
AKS
1927 printk(KERN_ERR "%s: write failed %d\n",
1928 __func__, ret);
9ce96908
KP
1929 break;
1930 }
cd5f6346 1931
9ce96908
KP
1932 /* Only check verify write turn on */
1933 ret = onenand_verify(mtd, buf, to, thislen);
1934 if (ret) {
297758f8
AKS
1935 printk(KERN_ERR "%s: verify failed %d\n",
1936 __func__, ret);
9ce96908
KP
1937 break;
1938 }
cd5f6346 1939
9ce96908 1940 written += thislen;
81f38e11 1941
9ce96908
KP
1942 if (written == len)
1943 break;
1944
1945 } else
1946 written += thislen;
cd5f6346 1947
60d84f97 1948 column = 0;
9ce96908
KP
1949 prev_subpage = subpage;
1950 prev = to;
1951 prevlen = thislen;
cd5f6346
KP
1952 to += thislen;
1953 buf += thislen;
9ce96908 1954 first = 0;
cd5f6346
KP
1955 }
1956
9ce96908
KP
1957 /* In error case, clear all bufferrams */
1958 if (written != len)
1959 onenand_invalidate_bufferram(mtd, 0, -1);
1960
d15057b7 1961 ops->retlen = written;
9ce96908 1962 ops->oobretlen = oobwritten;
d5c5e78a 1963
cd5f6346
KP
1964 return ret;
1965}
1966
a5e7c7b4 1967
cd5f6346 1968/**
7854d3f7 1969 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
cd5f6346
KP
1970 * @param mtd MTD device structure
1971 * @param to offset to write to
1972 * @param len number of bytes to write
1973 * @param retlen pointer to variable to store the number of written bytes
1974 * @param buf the data to write
a5e7c7b4 1975 * @param mode operation mode
cd5f6346
KP
1976 *
1977 * OneNAND write out-of-band
1978 */
49dc08ee
AB
1979static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1980 struct mtd_oob_ops *ops)
cd5f6346
KP
1981{
1982 struct onenand_chip *this = mtd->priv;
a5e7c7b4 1983 int column, ret = 0, oobsize;
5988af23 1984 int written = 0, oobcmd;
91014e9b 1985 u_char *oobbuf;
12f77c9e
KP
1986 size_t len = ops->ooblen;
1987 const u_char *buf = ops->oobbuf;
905c6bcd 1988 unsigned int mode = ops->mode;
12f77c9e
KP
1989
1990 to += ops->ooboffs;
cd5f6346 1991
0a32a102
BN
1992 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1993 (int)len);
cd5f6346
KP
1994
1995 /* Initialize retlen, in case of early exit */
12f77c9e 1996 ops->oobretlen = 0;
cd5f6346 1997
0612b9dd 1998 if (mode == MTD_OPS_AUTO_OOB)
f5b8aa78 1999 oobsize = mtd->oobavail;
a5e7c7b4
AH
2000 else
2001 oobsize = mtd->oobsize;
2002
2003 column = to & (mtd->oobsize - 1);
2004
2005 if (unlikely(column >= oobsize)) {
297758f8
AKS
2006 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2007 __func__);
a5e7c7b4
AH
2008 return -EINVAL;
2009 }
2010
52e4200a 2011 /* For compatibility with NAND: Do not allow write past end of page */
91014e9b 2012 if (unlikely(column + len > oobsize)) {
297758f8
AKS
2013 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2014 __func__);
52e4200a
AH
2015 return -EINVAL;
2016 }
2017
470bc844 2018 oobbuf = this->oob_buf;
91014e9b 2019
8a8f632d 2020 oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
5988af23 2021
cd5f6346
KP
2022 /* Loop until all data write */
2023 while (written < len) {
a5e7c7b4 2024 int thislen = min_t(int, oobsize, len - written);
cd5f6346 2025
61a7e198
AB
2026 cond_resched();
2027
cd5f6346
KP
2028 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2029
34c10609
KP
2030 /* We send data to spare ram with oobsize
2031 * to prevent byte access */
91014e9b 2032 memset(oobbuf, 0xff, mtd->oobsize);
0612b9dd 2033 if (mode == MTD_OPS_AUTO_OOB)
91014e9b 2034 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
a5e7c7b4 2035 else
91014e9b
KP
2036 memcpy(oobbuf + column, buf, thislen);
2037 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
cd5f6346 2038
8a8f632d 2039 if (ONENAND_IS_4KB_PAGE(this)) {
5988af23
RH
2040 /* Set main area of DataRAM to 0xff*/
2041 memset(this->page_buf, 0xff, mtd->writesize);
2042 this->write_bufferram(mtd, ONENAND_DATARAM,
2043 this->page_buf, 0, mtd->writesize);
2044 }
2045
2046 this->command(mtd, oobcmd, to, mtd->oobsize);
cd5f6346
KP
2047
2048 onenand_update_bufferram(mtd, to, 0);
ee9745fc
KP
2049 if (ONENAND_IS_2PLANE(this)) {
2050 ONENAND_SET_BUFFERRAM1(this);
2051 onenand_update_bufferram(mtd, to + this->writesize, 0);
2052 }
cd5f6346 2053
8e6ec690
KP
2054 ret = this->wait(mtd, FL_WRITING);
2055 if (ret) {
297758f8 2056 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
5b4246f1 2057 break;
8e6ec690
KP
2058 }
2059
91014e9b 2060 ret = onenand_verify_oob(mtd, oobbuf, to);
8e6ec690 2061 if (ret) {
297758f8
AKS
2062 printk(KERN_ERR "%s: verify failed %d\n",
2063 __func__, ret);
5b4246f1 2064 break;
8e6ec690 2065 }
cd5f6346
KP
2066
2067 written += thislen;
cd5f6346
KP
2068 if (written == len)
2069 break;
2070
a5e7c7b4 2071 to += mtd->writesize;
cd5f6346 2072 buf += thislen;
a5e7c7b4 2073 column = 0;
cd5f6346
KP
2074 }
2075
12f77c9e 2076 ops->oobretlen = written;
d5c5e78a 2077
8e6ec690 2078 return ret;
cd5f6346
KP
2079}
2080
8593fbc6
TG
2081/**
2082 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
e3da8067
KP
2083 * @param mtd: MTD device structure
2084 * @param to: offset to write
2085 * @param ops: oob operation description structure
8593fbc6
TG
2086 */
2087static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2088 struct mtd_oob_ops *ops)
2089{
49dc08ee
AB
2090 int ret;
2091
4f4fad27 2092 switch (ops->mode) {
0612b9dd
BN
2093 case MTD_OPS_PLACE_OOB:
2094 case MTD_OPS_AUTO_OOB:
a5e7c7b4 2095 break;
0612b9dd 2096 case MTD_OPS_RAW:
4f4fad27 2097 /* Not implemented yet */
a5e7c7b4
AH
2098 default:
2099 return -EINVAL;
2100 }
d15057b7 2101
49dc08ee 2102 onenand_get_device(mtd, FL_WRITING);
d15057b7 2103 if (ops->datbuf)
49dc08ee
AB
2104 ret = onenand_write_ops_nolock(mtd, to, ops);
2105 else
2106 ret = onenand_write_oob_nolock(mtd, to, ops);
2107 onenand_release_device(mtd);
d15057b7 2108
49dc08ee 2109 return ret;
8593fbc6
TG
2110}
2111
cdc00130 2112/**
49dc08ee 2113 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
cdc00130
KP
2114 * @param mtd MTD device structure
2115 * @param ofs offset from device start
cdc00130
KP
2116 * @param allowbbt 1, if its allowed to access the bbt area
2117 *
2118 * Check, if the block is bad. Either by reading the bad block table or
2119 * calling of the scan function.
2120 */
49dc08ee 2121static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
cdc00130
KP
2122{
2123 struct onenand_chip *this = mtd->priv;
2124 struct bbm_info *bbm = this->bbm;
2125
2126 /* Return info from the table */
2127 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2128}
2129
72073027
MK
2130
2131static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
2132 struct erase_info *instr)
2133{
2134 struct onenand_chip *this = mtd->priv;
2135 loff_t addr = instr->addr;
2136 int len = instr->len;
2137 unsigned int block_size = (1 << this->erase_shift);
2138 int ret = 0;
2139
2140 while (len) {
2141 this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
2142 ret = this->wait(mtd, FL_VERIFYING_ERASE);
2143 if (ret) {
2144 printk(KERN_ERR "%s: Failed verify, block %d\n",
2145 __func__, onenand_block(this, addr));
2146 instr->state = MTD_ERASE_FAILED;
2147 instr->fail_addr = addr;
2148 return -1;
2149 }
2150 len -= block_size;
2151 addr += block_size;
2152 }
2153 return 0;
2154}
2155
2156/**
7854d3f7 2157 * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
72073027
MK
2158 * @param mtd MTD device structure
2159 * @param instr erase instruction
2160 * @param region erase region
2161 *
2162 * Erase one or more blocks up to 64 block at a time
2163 */
2164static int onenand_multiblock_erase(struct mtd_info *mtd,
2165 struct erase_info *instr,
2166 unsigned int block_size)
2167{
2168 struct onenand_chip *this = mtd->priv;
2169 loff_t addr = instr->addr;
2170 int len = instr->len;
2171 int eb_count = 0;
2172 int ret = 0;
2173 int bdry_block = 0;
2174
2175 instr->state = MTD_ERASING;
2176
2177 if (ONENAND_IS_DDP(this)) {
2178 loff_t bdry_addr = this->chipsize >> 1;
2179 if (addr < bdry_addr && (addr + len) > bdry_addr)
2180 bdry_block = bdry_addr >> this->erase_shift;
2181 }
2182
2183 /* Pre-check bbs */
2184 while (len) {
2185 /* Check if we have a bad block, we do not erase bad blocks */
2186 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2187 printk(KERN_WARNING "%s: attempt to erase a bad block "
2188 "at addr 0x%012llx\n",
2189 __func__, (unsigned long long) addr);
2190 instr->state = MTD_ERASE_FAILED;
2191 return -EIO;
2192 }
2193 len -= block_size;
2194 addr += block_size;
2195 }
2196
2197 len = instr->len;
2198 addr = instr->addr;
2199
2200 /* loop over 64 eb batches */
2201 while (len) {
2202 struct erase_info verify_instr = *instr;
2203 int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
2204
2205 verify_instr.addr = addr;
2206 verify_instr.len = 0;
2207
2208 /* do not cross chip boundary */
2209 if (bdry_block) {
2210 int this_block = (addr >> this->erase_shift);
2211
2212 if (this_block < bdry_block) {
2213 max_eb_count = min(max_eb_count,
2214 (bdry_block - this_block));
2215 }
2216 }
2217
2218 eb_count = 0;
2219
2220 while (len > block_size && eb_count < (max_eb_count - 1)) {
2221 this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
2222 addr, block_size);
2223 onenand_invalidate_bufferram(mtd, addr, block_size);
2224
2225 ret = this->wait(mtd, FL_PREPARING_ERASE);
2226 if (ret) {
2227 printk(KERN_ERR "%s: Failed multiblock erase, "
2228 "block %d\n", __func__,
2229 onenand_block(this, addr));
2230 instr->state = MTD_ERASE_FAILED;
2231 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2232 return -EIO;
2233 }
2234
2235 len -= block_size;
2236 addr += block_size;
2237 eb_count++;
2238 }
2239
2240 /* last block of 64-eb series */
2241 cond_resched();
2242 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2243 onenand_invalidate_bufferram(mtd, addr, block_size);
2244
2245 ret = this->wait(mtd, FL_ERASING);
2246 /* Check if it is write protected */
2247 if (ret) {
2248 printk(KERN_ERR "%s: Failed erase, block %d\n",
2249 __func__, onenand_block(this, addr));
2250 instr->state = MTD_ERASE_FAILED;
2251 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2252 return -EIO;
2253 }
2254
2255 len -= block_size;
2256 addr += block_size;
2257 eb_count++;
2258
2259 /* verify */
2260 verify_instr.len = eb_count * block_size;
2261 if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
2262 instr->state = verify_instr.state;
2263 instr->fail_addr = verify_instr.fail_addr;
2264 return -EIO;
2265 }
2266
2267 }
2268 return 0;
2269}
2270
2271
cd5f6346 2272/**
7854d3f7 2273 * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
cd5f6346
KP
2274 * @param mtd MTD device structure
2275 * @param instr erase instruction
73885aea
MK
2276 * @param region erase region
2277 * @param block_size erase block size
cd5f6346 2278 *
73885aea 2279 * Erase one or more blocks one block at a time
cd5f6346 2280 */
73885aea
MK
2281static int onenand_block_by_block_erase(struct mtd_info *mtd,
2282 struct erase_info *instr,
2283 struct mtd_erase_region_info *region,
2284 unsigned int block_size)
cd5f6346
KP
2285{
2286 struct onenand_chip *this = mtd->priv;
5988af23 2287 loff_t addr = instr->addr;
73885aea 2288 int len = instr->len;
5988af23 2289 loff_t region_end = 0;
73885aea 2290 int ret = 0;
cd5f6346 2291
73885aea
MK
2292 if (region) {
2293 /* region is set for Flex-OneNAND */
5988af23 2294 region_end = region->offset + region->erasesize * region->numblocks;
cd5f6346
KP
2295 }
2296
cd5f6346
KP
2297 instr->state = MTD_ERASING;
2298
73885aea 2299 /* Loop through the blocks */
cd5f6346 2300 while (len) {
61a7e198 2301 cond_resched();
cd5f6346 2302
cdc00130 2303 /* Check if we have a bad block, we do not erase bad blocks */
49dc08ee 2304 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
297758f8
AKS
2305 printk(KERN_WARNING "%s: attempt to erase a bad block "
2306 "at addr 0x%012llx\n",
2307 __func__, (unsigned long long) addr);
cdc00130 2308 instr->state = MTD_ERASE_FAILED;
73885aea 2309 return -EIO;
cdc00130 2310 }
cd5f6346
KP
2311
2312 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2313
480b9dfb
AH
2314 onenand_invalidate_bufferram(mtd, addr, block_size);
2315
cd5f6346
KP
2316 ret = this->wait(mtd, FL_ERASING);
2317 /* Check, if it is write protected */
2318 if (ret) {
297758f8
AKS
2319 printk(KERN_ERR "%s: Failed erase, block %d\n",
2320 __func__, onenand_block(this, addr));
cd5f6346
KP
2321 instr->state = MTD_ERASE_FAILED;
2322 instr->fail_addr = addr;
73885aea 2323 return -EIO;
cd5f6346
KP
2324 }
2325
2326 len -= block_size;
2327 addr += block_size;
5988af23 2328
eff3bba6 2329 if (region && addr == region_end) {
5988af23
RH
2330 if (!len)
2331 break;
2332 region++;
2333
2334 block_size = region->erasesize;
2335 region_end = region->offset + region->erasesize * region->numblocks;
2336
2337 if (len & (block_size - 1)) {
2338 /* FIXME: This should be handled at MTD partitioning level. */
297758f8
AKS
2339 printk(KERN_ERR "%s: Unaligned address\n",
2340 __func__);
73885aea 2341 return -EIO;
5988af23
RH
2342 }
2343 }
73885aea
MK
2344 }
2345 return 0;
2346}
5988af23 2347
73885aea
MK
2348/**
2349 * onenand_erase - [MTD Interface] erase block(s)
2350 * @param mtd MTD device structure
2351 * @param instr erase instruction
2352 *
2353 * Erase one or more blocks
2354 */
2355static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2356{
2357 struct onenand_chip *this = mtd->priv;
2358 unsigned int block_size;
2359 loff_t addr = instr->addr;
2360 loff_t len = instr->len;
2361 int ret = 0;
2362 struct mtd_erase_region_info *region = NULL;
2363 loff_t region_offset = 0;
2364
289c0522 2365 pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
0a32a102
BN
2366 (unsigned long long)instr->addr,
2367 (unsigned long long)instr->len);
73885aea 2368
73885aea
MK
2369 if (FLEXONENAND(this)) {
2370 /* Find the eraseregion of this address */
2371 int i = flexonenand_region(mtd, addr);
cd5f6346 2372
73885aea
MK
2373 region = &mtd->eraseregions[i];
2374 block_size = region->erasesize;
cd5f6346 2375
73885aea
MK
2376 /* Start address within region must align on block boundary.
2377 * Erase region's start offset is always block start address.
2378 */
2379 region_offset = region->offset;
2380 } else
2381 block_size = 1 << this->erase_shift;
2382
2383 /* Start address must align on block boundary */
2384 if (unlikely((addr - region_offset) & (block_size - 1))) {
2385 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2386 return -EINVAL;
2387 }
2388
2389 /* Length must align on block boundary */
2390 if (unlikely(len & (block_size - 1))) {
2391 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2392 return -EINVAL;
2393 }
2394
73885aea
MK
2395 /* Grab the lock and see if the device is available */
2396 onenand_get_device(mtd, FL_ERASING);
2397
d983c54e
KP
2398 if (ONENAND_IS_4KB_PAGE(this) || region ||
2399 instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
72073027
MK
2400 /* region is set for Flex-OneNAND (no mb erase) */
2401 ret = onenand_block_by_block_erase(mtd, instr,
2402 region, block_size);
2403 } else {
2404 ret = onenand_multiblock_erase(mtd, instr, block_size);
2405 }
cd5f6346
KP
2406
2407 /* Deselect and wake up anyone waiting on the device */
2408 onenand_release_device(mtd);
2409
3cd3a86b 2410 /* Do call back function */
73885aea
MK
2411 if (!ret) {
2412 instr->state = MTD_ERASE_DONE;
3cd3a86b 2413 mtd_erase_callback(instr);
73885aea 2414 }
3cd3a86b 2415
cd5f6346
KP
2416 return ret;
2417}
2418
2419/**
2420 * onenand_sync - [MTD Interface] sync
2421 * @param mtd MTD device structure
2422 *
2423 * Sync is actually a wait for chip ready function
2424 */
2425static void onenand_sync(struct mtd_info *mtd)
2426{
289c0522 2427 pr_debug("%s: called\n", __func__);
cd5f6346
KP
2428
2429 /* Grab the lock and see if the device is available */
2430 onenand_get_device(mtd, FL_SYNCING);
2431
2432 /* Release it and go back */
2433 onenand_release_device(mtd);
2434}
2435
2436/**
2437 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2438 * @param mtd MTD device structure
2439 * @param ofs offset relative to mtd start
cdc00130
KP
2440 *
2441 * Check whether the block is bad
cd5f6346
KP
2442 */
2443static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2444{
49dc08ee
AB
2445 int ret;
2446
49dc08ee
AB
2447 onenand_get_device(mtd, FL_READING);
2448 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2449 onenand_release_device(mtd);
2450 return ret;
cdc00130
KP
2451}
2452
2453/**
2454 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2455 * @param mtd MTD device structure
2456 * @param ofs offset from device start
2457 *
2458 * This is the default implementation, which can be overridden by
2459 * a hardware specific driver.
2460 */
2461static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2462{
2463 struct onenand_chip *this = mtd->priv;
2464 struct bbm_info *bbm = this->bbm;
2465 u_char buf[2] = {0, 0};
12f77c9e 2466 struct mtd_oob_ops ops = {
0612b9dd 2467 .mode = MTD_OPS_PLACE_OOB,
12f77c9e
KP
2468 .ooblen = 2,
2469 .oobbuf = buf,
2470 .ooboffs = 0,
2471 };
cdc00130
KP
2472 int block;
2473
2474 /* Get block number */
5988af23 2475 block = onenand_block(this, ofs);
cdc00130
KP
2476 if (bbm->bbt)
2477 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2478
492e1501 2479 /* We write two bytes, so we don't have to mess with 16-bit access */
cdc00130 2480 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
5988af23
RH
2481 /* FIXME : What to do when marking SLC block in partition
2482 * with MLC erasesize? For now, it is not advisable to
2483 * create partitions containing both SLC and MLC regions.
2484 */
2485 return onenand_write_oob_nolock(mtd, ofs, &ops);
cd5f6346
KP
2486}
2487
2488/**
2489 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2490 * @param mtd MTD device structure
2491 * @param ofs offset relative to mtd start
cdc00130
KP
2492 *
2493 * Mark the block as bad
cd5f6346
KP
2494 */
2495static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2496{
5e64c29e 2497 struct onenand_chip *this = mtd->priv;
cdc00130
KP
2498 int ret;
2499
2500 ret = onenand_block_isbad(mtd, ofs);
2501 if (ret) {
2502 /* If it was bad already, return success and do nothing */
2503 if (ret > 0)
2504 return 0;
2505 return ret;
2506 }
2507
49dc08ee 2508 onenand_get_device(mtd, FL_WRITING);
5e64c29e 2509 ret = this->block_markbad(mtd, ofs);
49dc08ee
AB
2510 onenand_release_device(mtd);
2511 return ret;
cd5f6346
KP
2512}
2513
2514/**
08f782b6 2515 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
cd5f6346
KP
2516 * @param mtd MTD device structure
2517 * @param ofs offset relative to mtd start
08f782b6 2518 * @param len number of bytes to lock or unlock
e3da8067 2519 * @param cmd lock or unlock command
cd5f6346 2520 *
08f782b6 2521 * Lock or unlock one or more blocks
cd5f6346 2522 */
08f782b6 2523static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
cd5f6346
KP
2524{
2525 struct onenand_chip *this = mtd->priv;
2526 int start, end, block, value, status;
08f782b6 2527 int wp_status_mask;
cd5f6346 2528
5988af23
RH
2529 start = onenand_block(this, ofs);
2530 end = onenand_block(this, ofs + len) - 1;
cd5f6346 2531
08f782b6
KP
2532 if (cmd == ONENAND_CMD_LOCK)
2533 wp_status_mask = ONENAND_WP_LS;
2534 else
2535 wp_status_mask = ONENAND_WP_US;
2536
cd5f6346 2537 /* Continuous lock scheme */
28b79ff9 2538 if (this->options & ONENAND_HAS_CONT_LOCK) {
cd5f6346
KP
2539 /* Set start block address */
2540 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2541 /* Set end block address */
5988af23 2542 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
08f782b6
KP
2543 /* Write lock command */
2544 this->command(mtd, cmd, 0, 0);
cd5f6346
KP
2545
2546 /* There's no return value */
08f782b6 2547 this->wait(mtd, FL_LOCKING);
cd5f6346
KP
2548
2549 /* Sanity check */
2550 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2551 & ONENAND_CTRL_ONGO)
2552 continue;
2553
2554 /* Check lock status */
2555 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
08f782b6 2556 if (!(status & wp_status_mask))
297758f8
AKS
2557 printk(KERN_ERR "%s: wp status = 0x%x\n",
2558 __func__, status);
cd5f6346
KP
2559
2560 return 0;
2561 }
2562
2563 /* Block lock scheme */
5988af23 2564 for (block = start; block < end + 1; block++) {
20ba89a3
KP
2565 /* Set block address */
2566 value = onenand_block_address(this, block);
2567 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2568 /* Select DataRAM for DDP */
2569 value = onenand_bufferram_address(this, block);
2570 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
cd5f6346
KP
2571 /* Set start block address */
2572 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
08f782b6
KP
2573 /* Write lock command */
2574 this->command(mtd, cmd, 0, 0);
cd5f6346
KP
2575
2576 /* There's no return value */
08f782b6 2577 this->wait(mtd, FL_LOCKING);
cd5f6346
KP
2578
2579 /* Sanity check */
2580 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2581 & ONENAND_CTRL_ONGO)
2582 continue;
2583
cd5f6346
KP
2584 /* Check lock status */
2585 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
08f782b6 2586 if (!(status & wp_status_mask))
297758f8
AKS
2587 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2588 __func__, block, status);
cd5f6346 2589 }
d5c5e78a 2590
cd5f6346
KP
2591 return 0;
2592}
2593
08f782b6
KP
2594/**
2595 * onenand_lock - [MTD Interface] Lock block(s)
2596 * @param mtd MTD device structure
2597 * @param ofs offset relative to mtd start
2598 * @param len number of bytes to unlock
2599 *
2600 * Lock one or more blocks
2601 */
69423d99 2602static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
08f782b6 2603{
34627f0e
AH
2604 int ret;
2605
2606 onenand_get_device(mtd, FL_LOCKING);
2607 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2608 onenand_release_device(mtd);
2609 return ret;
08f782b6
KP
2610}
2611
08f782b6
KP
2612/**
2613 * onenand_unlock - [MTD Interface] Unlock block(s)
2614 * @param mtd MTD device structure
2615 * @param ofs offset relative to mtd start
2616 * @param len number of bytes to unlock
2617 *
2618 * Unlock one or more blocks
2619 */
69423d99 2620static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
08f782b6 2621{
34627f0e
AH
2622 int ret;
2623
2624 onenand_get_device(mtd, FL_LOCKING);
2625 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2626 onenand_release_device(mtd);
2627 return ret;
08f782b6
KP
2628}
2629
28b79ff9
KP
2630/**
2631 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2632 * @param this onenand chip data structure
2633 *
2634 * Check lock status
2635 */
66a10506 2636static int onenand_check_lock_status(struct onenand_chip *this)
28b79ff9
KP
2637{
2638 unsigned int value, block, status;
2639 unsigned int end;
2640
2641 end = this->chipsize >> this->erase_shift;
2642 for (block = 0; block < end; block++) {
2643 /* Set block address */
2644 value = onenand_block_address(this, block);
2645 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2646 /* Select DataRAM for DDP */
2647 value = onenand_bufferram_address(this, block);
2648 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2649 /* Set start block address */
2650 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2651
2652 /* Check lock status */
2653 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
66a10506 2654 if (!(status & ONENAND_WP_US)) {
297758f8
AKS
2655 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2656 __func__, block, status);
66a10506
KP
2657 return 0;
2658 }
28b79ff9 2659 }
66a10506
KP
2660
2661 return 1;
28b79ff9
KP
2662}
2663
2664/**
2665 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2666 * @param mtd MTD device structure
2667 *
2668 * Unlock all blocks
2669 */
66a10506 2670static void onenand_unlock_all(struct mtd_info *mtd)
28b79ff9
KP
2671{
2672 struct onenand_chip *this = mtd->priv;
66a10506 2673 loff_t ofs = 0;
5988af23 2674 loff_t len = mtd->size;
28b79ff9
KP
2675
2676 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
10b7a2bd
KP
2677 /* Set start block address */
2678 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
28b79ff9
KP
2679 /* Write unlock command */
2680 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2681
2682 /* There's no return value */
08f782b6 2683 this->wait(mtd, FL_LOCKING);
28b79ff9
KP
2684
2685 /* Sanity check */
2686 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2687 & ONENAND_CTRL_ONGO)
2688 continue;
2689
31bb999e
KP
2690 /* Don't check lock status */
2691 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2692 return;
2693
66a10506
KP
2694 /* Check lock status */
2695 if (onenand_check_lock_status(this))
2696 return;
2697
28b79ff9 2698 /* Workaround for all block unlock in DDP */
5988af23 2699 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
66a10506
KP
2700 /* All blocks on another chip */
2701 ofs = this->chipsize >> 1;
2702 len = this->chipsize >> 1;
28b79ff9 2703 }
28b79ff9
KP
2704 }
2705
66a10506 2706 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
28b79ff9
KP
2707}
2708
493c6460
KP
2709#ifdef CONFIG_MTD_ONENAND_OTP
2710
3cf60253
AKS
2711/**
2712 * onenand_otp_command - Send OTP specific command to OneNAND device
2713 * @param mtd MTD device structure
2714 * @param cmd the command to be sent
2715 * @param addr offset to read from or write to
2716 * @param len number of bytes to read or write
2717 */
2718static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2719 size_t len)
2720{
2721 struct onenand_chip *this = mtd->priv;
2722 int value, block, page;
2723
2724 /* Address translation */
2725 switch (cmd) {
2726 case ONENAND_CMD_OTP_ACCESS:
2727 block = (int) (addr >> this->erase_shift);
2728 page = -1;
2729 break;
2730
2731 default:
2732 block = (int) (addr >> this->erase_shift);
2733 page = (int) (addr >> this->page_shift);
2734
2735 if (ONENAND_IS_2PLANE(this)) {
2736 /* Make the even block number */
2737 block &= ~1;
2738 /* Is it the odd plane? */
2739 if (addr & this->writesize)
2740 block++;
2741 page >>= 1;
2742 }
2743 page &= this->page_mask;
2744 break;
2745 }
2746
2747 if (block != -1) {
2748 /* Write 'DFS, FBA' of Flash */
2749 value = onenand_block_address(this, block);
2750 this->write_word(value, this->base +
2751 ONENAND_REG_START_ADDRESS1);
2752 }
2753
2754 if (page != -1) {
2755 /* Now we use page size operation */
2756 int sectors = 4, count = 4;
2757 int dataram;
2758
2759 switch (cmd) {
2760 default:
2761 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2762 cmd = ONENAND_CMD_2X_PROG;
2763 dataram = ONENAND_CURRENT_BUFFERRAM(this);
2764 break;
2765 }
2766
2767 /* Write 'FPA, FSA' of Flash */
2768 value = onenand_page_address(page, sectors);
2769 this->write_word(value, this->base +
2770 ONENAND_REG_START_ADDRESS8);
2771
2772 /* Write 'BSA, BSC' of DataRAM */
2773 value = onenand_buffer_address(dataram, sectors, count);
2774 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2775 }
2776
2777 /* Interrupt clear */
2778 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2779
2780 /* Write command */
2781 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2782
2783 return 0;
2784}
2785
2786/**
7854d3f7 2787 * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
3cf60253
AKS
2788 * @param mtd MTD device structure
2789 * @param to offset to write to
2790 * @param len number of bytes to write
2791 * @param retlen pointer to variable to store the number of written bytes
2792 * @param buf the data to write
2793 *
2794 * OneNAND write out-of-band only for OTP
2795 */
2796static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2797 struct mtd_oob_ops *ops)
2798{
2799 struct onenand_chip *this = mtd->priv;
2800 int column, ret = 0, oobsize;
2801 int written = 0;
2802 u_char *oobbuf;
2803 size_t len = ops->ooblen;
2804 const u_char *buf = ops->oobbuf;
2805 int block, value, status;
2806
2807 to += ops->ooboffs;
2808
2809 /* Initialize retlen, in case of early exit */
2810 ops->oobretlen = 0;
2811
2812 oobsize = mtd->oobsize;
2813
2814 column = to & (mtd->oobsize - 1);
2815
2816 oobbuf = this->oob_buf;
2817
2818 /* Loop until all data write */
2819 while (written < len) {
2820 int thislen = min_t(int, oobsize, len - written);
2821
2822 cond_resched();
2823
2824 block = (int) (to >> this->erase_shift);
2825 /*
2826 * Write 'DFS, FBA' of Flash
2827 * Add: F100h DQ=DFS, FBA
2828 */
2829
2830 value = onenand_block_address(this, block);
2831 this->write_word(value, this->base +
2832 ONENAND_REG_START_ADDRESS1);
2833
2834 /*
2835 * Select DataRAM for DDP
2836 * Add: F101h DQ=DBS
2837 */
2838
2839 value = onenand_bufferram_address(this, block);
2840 this->write_word(value, this->base +
2841 ONENAND_REG_START_ADDRESS2);
2842 ONENAND_SET_NEXT_BUFFERRAM(this);
2843
2844 /*
2845 * Enter OTP access mode
2846 */
2847 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2848 this->wait(mtd, FL_OTPING);
2849
2850 /* We send data to spare ram with oobsize
2851 * to prevent byte access */
2852 memcpy(oobbuf + column, buf, thislen);
2853
2854 /*
2855 * Write Data into DataRAM
2856 * Add: 8th Word
2857 * in sector0/spare/page0
2858 * DQ=XXFCh
2859 */
2860 this->write_bufferram(mtd, ONENAND_SPARERAM,
2861 oobbuf, 0, mtd->oobsize);
2862
2863 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2864 onenand_update_bufferram(mtd, to, 0);
2865 if (ONENAND_IS_2PLANE(this)) {
2866 ONENAND_SET_BUFFERRAM1(this);
2867 onenand_update_bufferram(mtd, to + this->writesize, 0);
2868 }
2869
2870 ret = this->wait(mtd, FL_WRITING);
2871 if (ret) {
2872 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2873 break;
2874 }
2875
2876 /* Exit OTP access mode */
2877 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2878 this->wait(mtd, FL_RESETING);
2879
2880 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2881 status &= 0x60;
2882
2883 if (status == 0x60) {
2884 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2885 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2886 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2887 } else if (status == 0x20) {
2888 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2889 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2890 printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
2891 } else if (status == 0x40) {
2892 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2893 printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
2894 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2895 } else {
2896 printk(KERN_DEBUG "Reboot to check\n");
2897 }
2898
2899 written += thislen;
2900 if (written == len)
2901 break;
2902
2903 to += mtd->writesize;
2904 buf += thislen;
2905 column = 0;
2906 }
2907
2908 ops->oobretlen = written;
2909
2910 return ret;
2911}
2912
492e1501 2913/* Internal OTP operation */
493c6460
KP
2914typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2915 size_t *retlen, u_char *buf);
2916
2917/**
2918 * do_otp_read - [DEFAULT] Read OTP block area
2919 * @param mtd MTD device structure
2920 * @param from The offset to read
2921 * @param len number of bytes to read
2922 * @param retlen pointer to variable to store the number of readbytes
2923 * @param buf the databuffer to put/get data
2924 *
2925 * Read OTP block area.
2926 */
2927static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2928 size_t *retlen, u_char *buf)
2929{
2930 struct onenand_chip *this = mtd->priv;
49dc08ee
AB
2931 struct mtd_oob_ops ops = {
2932 .len = len,
2933 .ooblen = 0,
2934 .datbuf = buf,
2935 .oobbuf = NULL,
2936 };
493c6460
KP
2937 int ret;
2938
2939 /* Enter OTP access mode */
2940 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2941 this->wait(mtd, FL_OTPING);
2942
8a8f632d 2943 ret = ONENAND_IS_4KB_PAGE(this) ?
5988af23
RH
2944 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2945 onenand_read_ops_nolock(mtd, from, &ops);
493c6460
KP
2946
2947 /* Exit OTP access mode */
2948 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2949 this->wait(mtd, FL_RESETING);
2950
2951 return ret;
2952}
2953
2954/**
2955 * do_otp_write - [DEFAULT] Write OTP block area
2956 * @param mtd MTD device structure
49dc08ee 2957 * @param to The offset to write
493c6460
KP
2958 * @param len number of bytes to write
2959 * @param retlen pointer to variable to store the number of write bytes
2960 * @param buf the databuffer to put/get data
2961 *
2962 * Write OTP block area.
2963 */
49dc08ee 2964static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
493c6460
KP
2965 size_t *retlen, u_char *buf)
2966{
2967 struct onenand_chip *this = mtd->priv;
2968 unsigned char *pbuf = buf;
2969 int ret;
49dc08ee 2970 struct mtd_oob_ops ops;
493c6460
KP
2971
2972 /* Force buffer page aligned */
28318776 2973 if (len < mtd->writesize) {
493c6460 2974 memcpy(this->page_buf, buf, len);
28318776 2975 memset(this->page_buf + len, 0xff, mtd->writesize - len);
493c6460 2976 pbuf = this->page_buf;
28318776 2977 len = mtd->writesize;
493c6460
KP
2978 }
2979
2980 /* Enter OTP access mode */
2981 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2982 this->wait(mtd, FL_OTPING);
2983
49dc08ee
AB
2984 ops.len = len;
2985 ops.ooblen = 0;
1437085c 2986 ops.datbuf = pbuf;
49dc08ee
AB
2987 ops.oobbuf = NULL;
2988 ret = onenand_write_ops_nolock(mtd, to, &ops);
2989 *retlen = ops.retlen;
493c6460
KP
2990
2991 /* Exit OTP access mode */
2992 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2993 this->wait(mtd, FL_RESETING);
2994
2995 return ret;
2996}
2997
2998/**
2999 * do_otp_lock - [DEFAULT] Lock OTP block area
3000 * @param mtd MTD device structure
3001 * @param from The offset to lock
3002 * @param len number of bytes to lock
3003 * @param retlen pointer to variable to store the number of lock bytes
3004 * @param buf the databuffer to put/get data
3005 *
3006 * Lock OTP block area.
3007 */
3008static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
3009 size_t *retlen, u_char *buf)
3010{
3011 struct onenand_chip *this = mtd->priv;
5988af23 3012 struct mtd_oob_ops ops;
493c6460
KP
3013 int ret;
3014
5988af23 3015 if (FLEXONENAND(this)) {
3cf60253
AKS
3016
3017 /* Enter OTP access mode */
3018 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
3019 this->wait(mtd, FL_OTPING);
5988af23
RH
3020 /*
3021 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3022 * main area of page 49.
3023 */
3024 ops.len = mtd->writesize;
3025 ops.ooblen = 0;
3026 ops.datbuf = buf;
3027 ops.oobbuf = NULL;
3028 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
3029 *retlen = ops.retlen;
3cf60253
AKS
3030
3031 /* Exit OTP access mode */
3032 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3033 this->wait(mtd, FL_RESETING);
5988af23 3034 } else {
0612b9dd 3035 ops.mode = MTD_OPS_PLACE_OOB;
5988af23
RH
3036 ops.ooblen = len;
3037 ops.oobbuf = buf;
3038 ops.ooboffs = 0;
3cf60253 3039 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
5988af23
RH
3040 *retlen = ops.oobretlen;
3041 }
493c6460 3042
493c6460
KP
3043 return ret;
3044}
3045
3046/**
3047 * onenand_otp_walk - [DEFAULT] Handle OTP operation
3048 * @param mtd MTD device structure
3049 * @param from The offset to read/write
3050 * @param len number of bytes to read/write
3051 * @param retlen pointer to variable to store the number of read bytes
3052 * @param buf the databuffer to put/get data
3053 * @param action do given action
3054 * @param mode specify user and factory
3055 *
3056 * Handle OTP operation.
3057 */
3058static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
3059 size_t *retlen, u_char *buf,
3060 otp_op_t action, int mode)
3061{
3062 struct onenand_chip *this = mtd->priv;
3063 int otp_pages;
3064 int density;
3065 int ret = 0;
3066
3067 *retlen = 0;
3068
e71f04fc 3069 density = onenand_get_density(this->device_id);
493c6460
KP
3070 if (density < ONENAND_DEVICE_DENSITY_512Mb)
3071 otp_pages = 20;
3072 else
3cf60253 3073 otp_pages = 50;
493c6460
KP
3074
3075 if (mode == MTD_OTP_FACTORY) {
28318776 3076 from += mtd->writesize * otp_pages;
3cf60253 3077 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
493c6460
KP
3078 }
3079
3080 /* Check User/Factory boundary */
3cf60253 3081 if (mode == MTD_OTP_USER) {
0a032a4d 3082 if (mtd->writesize * otp_pages < from + len)
3cf60253
AKS
3083 return 0;
3084 } else {
0a032a4d 3085 if (mtd->writesize * otp_pages < len)
3cf60253
AKS
3086 return 0;
3087 }
493c6460 3088
49dc08ee 3089 onenand_get_device(mtd, FL_OTPING);
493c6460
KP
3090 while (len > 0 && otp_pages > 0) {
3091 if (!action) { /* OTP Info functions */
3092 struct otp_info *otpinfo;
3093
3094 len -= sizeof(struct otp_info);
49dc08ee
AB
3095 if (len <= 0) {
3096 ret = -ENOSPC;
3097 break;
3098 }
493c6460
KP
3099
3100 otpinfo = (struct otp_info *) buf;
3101 otpinfo->start = from;
28318776 3102 otpinfo->length = mtd->writesize;
493c6460
KP
3103 otpinfo->locked = 0;
3104
28318776 3105 from += mtd->writesize;
493c6460
KP
3106 buf += sizeof(struct otp_info);
3107 *retlen += sizeof(struct otp_info);
3108 } else {
3109 size_t tmp_retlen;
493c6460
KP
3110
3111 ret = action(mtd, from, len, &tmp_retlen, buf);
c3cb77f8
DC
3112 if (ret)
3113 break;
493c6460 3114
3cf60253
AKS
3115 buf += tmp_retlen;
3116 len -= tmp_retlen;
3117 *retlen += tmp_retlen;
493c6460 3118
493c6460
KP
3119 }
3120 otp_pages--;
3121 }
49dc08ee 3122 onenand_release_device(mtd);
493c6460 3123
49dc08ee 3124 return ret;
493c6460
KP
3125}
3126
3127/**
3128 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3129 * @param mtd MTD device structure
493c6460 3130 * @param len number of bytes to read
4b78fc42
CR
3131 * @param retlen pointer to variable to store the number of read bytes
3132 * @param buf the databuffer to put/get data
493c6460
KP
3133 *
3134 * Read factory OTP info.
3135 */
4b78fc42
CR
3136static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
3137 size_t *retlen, struct otp_info *buf)
493c6460 3138{
4b78fc42
CR
3139 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3140 MTD_OTP_FACTORY);
493c6460
KP
3141}
3142
3143/**
3144 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3145 * @param mtd MTD device structure
3146 * @param from The offset to read
3147 * @param len number of bytes to read
3148 * @param retlen pointer to variable to store the number of read bytes
3149 * @param buf the databuffer to put/get data
3150 *
3151 * Read factory OTP area.
3152 */
3153static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3154 size_t len, size_t *retlen, u_char *buf)
3155{
3156 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3157}
3158
3159/**
3160 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3161 * @param mtd MTD device structure
4b78fc42 3162 * @param retlen pointer to variable to store the number of read bytes
493c6460 3163 * @param len number of bytes to read
4b78fc42 3164 * @param buf the databuffer to put/get data
493c6460
KP
3165 *
3166 * Read user OTP info.
3167 */
4b78fc42
CR
3168static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len,
3169 size_t *retlen, struct otp_info *buf)
493c6460 3170{
4b78fc42
CR
3171 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3172 MTD_OTP_USER);
493c6460
KP
3173}
3174
3175/**
3176 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3177 * @param mtd MTD device structure
3178 * @param from The offset to read
3179 * @param len number of bytes to read
3180 * @param retlen pointer to variable to store the number of read bytes
3181 * @param buf the databuffer to put/get data
3182 *
3183 * Read user OTP area.
3184 */
3185static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3186 size_t len, size_t *retlen, u_char *buf)
3187{
3188 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3189}
3190
3191/**
3192 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3193 * @param mtd MTD device structure
3194 * @param from The offset to write
3195 * @param len number of bytes to write
3196 * @param retlen pointer to variable to store the number of write bytes
3197 * @param buf the databuffer to put/get data
3198 *
3199 * Write user OTP area.
3200 */
3201static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3202 size_t len, size_t *retlen, u_char *buf)
3203{
3204 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3205}
3206
3207/**
3208 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3209 * @param mtd MTD device structure
3210 * @param from The offset to lock
3211 * @param len number of bytes to unlock
3212 *
3213 * Write lock mark on spare area in page 0 in OTP block
3214 */
3215static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3216 size_t len)
3217{
69d79186 3218 struct onenand_chip *this = mtd->priv;
5988af23 3219 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
493c6460
KP
3220 size_t retlen;
3221 int ret;
3cf60253 3222 unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
493c6460 3223
5988af23
RH
3224 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3225 : mtd->oobsize);
493c6460
KP
3226 /*
3227 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3228 * We write 16 bytes spare area instead of 2 bytes.
5988af23
RH
3229 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3230 * main area of page 49.
493c6460 3231 */
5988af23 3232
493c6460 3233 from = 0;
5988af23 3234 len = FLEXONENAND(this) ? mtd->writesize : 16;
493c6460 3235
3cf60253
AKS
3236 /*
3237 * Note: OTP lock operation
3238 * OTP block : 0xXXFC XX 1111 1100
3239 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3240 * Both : 0xXXF0 (If chip support) XX 1111 0000
3241 */
3242 if (FLEXONENAND(this))
3243 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3244
3245 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3246 if (otp == 1)
3247 buf[otp_lock_offset] = 0xFC;
3248 else if (otp == 2)
3249 buf[otp_lock_offset] = 0xF3;
3250 else if (otp == 3)
3251 buf[otp_lock_offset] = 0xF0;
3252 else if (otp != 0)
3253 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3254
5988af23 3255 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
493c6460
KP
3256
3257 return ret ? : retlen;
3258}
3cf60253 3259
493c6460
KP
3260#endif /* CONFIG_MTD_ONENAND_OTP */
3261
28b79ff9 3262/**
75384b0d 3263 * onenand_check_features - Check and set OneNAND features
28b79ff9
KP
3264 * @param mtd MTD data structure
3265 *
75384b0d
KP
3266 * Check and set OneNAND features
3267 * - lock scheme
ee9745fc 3268 * - two plane
28b79ff9 3269 */
75384b0d 3270static void onenand_check_features(struct mtd_info *mtd)
28b79ff9
KP
3271{
3272 struct onenand_chip *this = mtd->priv;
edb44b9b 3273 unsigned int density, process, numbufs;
28b79ff9
KP
3274
3275 /* Lock scheme depends on density and process */
e71f04fc 3276 density = onenand_get_density(this->device_id);
28b79ff9 3277 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
edb44b9b 3278 numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
28b79ff9
KP
3279
3280 /* Lock scheme */
ee9745fc
KP
3281 switch (density) {
3282 case ONENAND_DEVICE_DENSITY_4Gb:
6a88c47b
KP
3283 if (ONENAND_IS_DDP(this))
3284 this->options |= ONENAND_HAS_2PLANE;
ac80dac0 3285 else if (numbufs == 1) {
6a88c47b 3286 this->options |= ONENAND_HAS_4KB_PAGE;
ac80dac0 3287 this->options |= ONENAND_HAS_CACHE_PROGRAM;
e1c10243
KP
3288 /*
3289 * There are two different 4KiB pagesize chips
3290 * and no way to detect it by H/W config values.
3291 *
3292 * To detect the correct NOP for each chips,
3293 * It should check the version ID as workaround.
3294 *
3295 * Now it has as following
3296 * KFM4G16Q4M has NOP 4 with version ID 0x0131
3297 * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
3298 */
3299 if ((this->version_id & 0xf) == 0xe)
3300 this->options |= ONENAND_HAS_NOP_1;
ac80dac0 3301 }
ee9745fc
KP
3302
3303 case ONENAND_DEVICE_DENSITY_2Gb:
492e1501 3304 /* 2Gb DDP does not have 2 plane */
ee9745fc
KP
3305 if (!ONENAND_IS_DDP(this))
3306 this->options |= ONENAND_HAS_2PLANE;
3307 this->options |= ONENAND_HAS_UNLOCK_ALL;
3308
3309 case ONENAND_DEVICE_DENSITY_1Gb:
28b79ff9 3310 /* A-Die has all block unlock */
ee9745fc 3311 if (process)
28b79ff9 3312 this->options |= ONENAND_HAS_UNLOCK_ALL;
ee9745fc
KP
3313 break;
3314
3315 default:
3316 /* Some OneNAND has continuous lock scheme */
3317 if (!process)
28b79ff9 3318 this->options |= ONENAND_HAS_CONT_LOCK;
ee9745fc 3319 break;
28b79ff9 3320 }
ee9745fc 3321
8a8f632d
KP
3322 /* The MLC has 4KiB pagesize. */
3323 if (ONENAND_IS_MLC(this))
3324 this->options |= ONENAND_HAS_4KB_PAGE;
3325
3326 if (ONENAND_IS_4KB_PAGE(this))
5988af23
RH
3327 this->options &= ~ONENAND_HAS_2PLANE;
3328
3329 if (FLEXONENAND(this)) {
3330 this->options &= ~ONENAND_HAS_CONT_LOCK;
3331 this->options |= ONENAND_HAS_UNLOCK_ALL;
3332 }
3333
ee9745fc
KP
3334 if (this->options & ONENAND_HAS_CONT_LOCK)
3335 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3336 if (this->options & ONENAND_HAS_UNLOCK_ALL)
3337 printk(KERN_DEBUG "Chip support all block unlock\n");
3338 if (this->options & ONENAND_HAS_2PLANE)
3339 printk(KERN_DEBUG "Chip has 2 plane\n");
6a88c47b
KP
3340 if (this->options & ONENAND_HAS_4KB_PAGE)
3341 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
ac80dac0
RT
3342 if (this->options & ONENAND_HAS_CACHE_PROGRAM)
3343 printk(KERN_DEBUG "Chip has cache program feature\n");
28b79ff9
KP
3344}
3345
cd5f6346 3346/**
e3da8067 3347 * onenand_print_device_info - Print device & version ID
cd5f6346 3348 * @param device device ID
e3da8067 3349 * @param version version ID
cd5f6346 3350 *
e3da8067 3351 * Print device & version ID
cd5f6346 3352 */
28b79ff9 3353static void onenand_print_device_info(int device, int version)
cd5f6346 3354{
5988af23 3355 int vcc, demuxed, ddp, density, flexonenand;
cd5f6346
KP
3356
3357 vcc = device & ONENAND_DEVICE_VCC_MASK;
3358 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3359 ddp = device & ONENAND_DEVICE_IS_DDP;
e71f04fc 3360 density = onenand_get_density(device);
5988af23
RH
3361 flexonenand = device & DEVICE_IS_FLEXONENAND;
3362 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3363 demuxed ? "" : "Muxed ",
3364 flexonenand ? "Flex-" : "",
cd5f6346
KP
3365 ddp ? "(DDP)" : "",
3366 (16 << density),
3367 vcc ? "2.65/3.3" : "1.8",
3368 device);
49dc08ee 3369 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
cd5f6346
KP
3370}
3371
3372static const struct onenand_manufacturers onenand_manuf_ids[] = {
3373 {ONENAND_MFR_SAMSUNG, "Samsung"},
ee8f3768 3374 {ONENAND_MFR_NUMONYX, "Numonyx"},
cd5f6346
KP
3375};
3376
3377/**
3378 * onenand_check_maf - Check manufacturer ID
3379 * @param manuf manufacturer ID
3380 *
3381 * Check manufacturer ID
3382 */
3383static int onenand_check_maf(int manuf)
3384{
37b1cc39
KP
3385 int size = ARRAY_SIZE(onenand_manuf_ids);
3386 char *name;
cd5f6346
KP
3387 int i;
3388
37b1cc39 3389 for (i = 0; i < size; i++)
cd5f6346
KP
3390 if (manuf == onenand_manuf_ids[i].id)
3391 break;
cd5f6346 3392
37b1cc39
KP
3393 if (i < size)
3394 name = onenand_manuf_ids[i].name;
3395 else
3396 name = "Unknown";
3397
3398 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
cd5f6346 3399
37b1cc39 3400 return (i == size);
cd5f6346
KP
3401}
3402
5988af23
RH
3403/**
3404* flexonenand_get_boundary - Reads the SLC boundary
3405* @param onenand_info - onenand info structure
3406**/
3407static int flexonenand_get_boundary(struct mtd_info *mtd)
3408{
3409 struct onenand_chip *this = mtd->priv;
3410 unsigned die, bdry;
6b7368c2 3411 int syscfg, locked;
5988af23
RH
3412
3413 /* Disable ECC */
3414 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3415 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3416
3417 for (die = 0; die < this->dies; die++) {
3418 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3419 this->wait(mtd, FL_SYNCING);
3420
3421 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
6b7368c2 3422 this->wait(mtd, FL_READING);
5988af23
RH
3423
3424 bdry = this->read_word(this->base + ONENAND_DATARAM);
3425 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3426 locked = 0;
3427 else
3428 locked = 1;
3429 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3430
3431 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
6b7368c2 3432 this->wait(mtd, FL_RESETING);
5988af23
RH
3433
3434 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3435 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3436 }
3437
3438 /* Enable ECC */
3439 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3440 return 0;
3441}
3442
3443/**
3444 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3445 * boundary[], diesize[], mtd->size, mtd->erasesize
3446 * @param mtd - MTD device structure
3447 */
3448static void flexonenand_get_size(struct mtd_info *mtd)
3449{
3450 struct onenand_chip *this = mtd->priv;
3451 int die, i, eraseshift, density;
3452 int blksperdie, maxbdry;
3453 loff_t ofs;
3454
3455 density = onenand_get_density(this->device_id);
3456 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3457 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3458 maxbdry = blksperdie - 1;
3459 eraseshift = this->erase_shift - 1;
3460
3461 mtd->numeraseregions = this->dies << 1;
3462
3463 /* This fills up the device boundary */
3464 flexonenand_get_boundary(mtd);
3465 die = ofs = 0;
3466 i = -1;
3467 for (; die < this->dies; die++) {
3468 if (!die || this->boundary[die-1] != maxbdry) {
3469 i++;
3470 mtd->eraseregions[i].offset = ofs;
3471 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3472 mtd->eraseregions[i].numblocks =
3473 this->boundary[die] + 1;
3474 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3475 eraseshift++;
3476 } else {
3477 mtd->numeraseregions -= 1;
3478 mtd->eraseregions[i].numblocks +=
3479 this->boundary[die] + 1;
3480 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3481 }
3482 if (this->boundary[die] != maxbdry) {
3483 i++;
3484 mtd->eraseregions[i].offset = ofs;
3485 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3486 mtd->eraseregions[i].numblocks = maxbdry ^
3487 this->boundary[die];
3488 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3489 eraseshift--;
3490 } else
3491 mtd->numeraseregions -= 1;
3492 }
3493
3494 /* Expose MLC erase size except when all blocks are SLC */
3495 mtd->erasesize = 1 << this->erase_shift;
3496 if (mtd->numeraseregions == 1)
3497 mtd->erasesize >>= 1;
3498
3499 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3500 for (i = 0; i < mtd->numeraseregions; i++)
3501 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3502 " numblocks: %04u]\n",
3503 (unsigned int) mtd->eraseregions[i].offset,
3504 mtd->eraseregions[i].erasesize,
3505 mtd->eraseregions[i].numblocks);
3506
3507 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3508 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3509 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3510 << (this->erase_shift - 1);
3511 mtd->size += this->diesize[die];
3512 }
3513}
3514
3515/**
3516 * flexonenand_check_blocks_erased - Check if blocks are erased
3517 * @param mtd_info - mtd info structure
3518 * @param start - first erase block to check
3519 * @param end - last erase block to check
3520 *
3521 * Converting an unerased block from MLC to SLC
3522 * causes byte values to change. Since both data and its ECC
3523 * have changed, reads on the block give uncorrectable error.
3524 * This might lead to the block being detected as bad.
3525 *
3526 * Avoid this by ensuring that the block to be converted is
3527 * erased.
3528 */
3529static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3530{
3531 struct onenand_chip *this = mtd->priv;
3532 int i, ret;
3533 int block;
3534 struct mtd_oob_ops ops = {
0612b9dd 3535 .mode = MTD_OPS_PLACE_OOB,
5988af23
RH
3536 .ooboffs = 0,
3537 .ooblen = mtd->oobsize,
3538 .datbuf = NULL,
3539 .oobbuf = this->oob_buf,
3540 };
3541 loff_t addr;
3542
3543 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3544
3545 for (block = start; block <= end; block++) {
3546 addr = flexonenand_addr(this, block);
3547 if (onenand_block_isbad_nolock(mtd, addr, 0))
3548 continue;
3549
3550 /*
3551 * Since main area write results in ECC write to spare,
3552 * it is sufficient to check only ECC bytes for change.
3553 */
3554 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3555 if (ret)
3556 return ret;
3557
3558 for (i = 0; i < mtd->oobsize; i++)
3559 if (this->oob_buf[i] != 0xff)
3560 break;
3561
3562 if (i != mtd->oobsize) {
297758f8
AKS
3563 printk(KERN_WARNING "%s: Block %d not erased.\n",
3564 __func__, block);
5988af23
RH
3565 return 1;
3566 }
3567 }
3568
3569 return 0;
3570}
3571
3572/**
3573 * flexonenand_set_boundary - Writes the SLC boundary
3574 * @param mtd - mtd info structure
3575 */
0131950e 3576static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
5988af23
RH
3577 int boundary, int lock)
3578{
3579 struct onenand_chip *this = mtd->priv;
3580 int ret, density, blksperdie, old, new, thisboundary;
3581 loff_t addr;
3582
3583 /* Change only once for SDP Flex-OneNAND */
3584 if (die && (!ONENAND_IS_DDP(this)))
3585 return 0;
3586
3587 /* boundary value of -1 indicates no required change */
3588 if (boundary < 0 || boundary == this->boundary[die])
3589 return 0;
3590
3591 density = onenand_get_density(this->device_id);
3592 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3593 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3594
3595 if (boundary >= blksperdie) {
297758f8
AKS
3596 printk(KERN_ERR "%s: Invalid boundary value. "
3597 "Boundary not changed.\n", __func__);
5988af23
RH
3598 return -EINVAL;
3599 }
3600
3601 /* Check if converting blocks are erased */
3602 old = this->boundary[die] + (die * this->density_mask);
3603 new = boundary + (die * this->density_mask);
3604 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3605 if (ret) {
297758f8
AKS
3606 printk(KERN_ERR "%s: Please erase blocks "
3607 "before boundary change\n", __func__);
5988af23
RH
3608 return ret;
3609 }
3610
3611 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3612 this->wait(mtd, FL_SYNCING);
3613
3614 /* Check is boundary is locked */
3615 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
6b7368c2 3616 this->wait(mtd, FL_READING);
5988af23
RH
3617
3618 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3619 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
297758f8 3620 printk(KERN_ERR "%s: boundary locked\n", __func__);
5988af23
RH
3621 ret = 1;
3622 goto out;
3623 }
3624
297758f8 3625 printk(KERN_INFO "Changing die %d boundary: %d%s\n",
5988af23
RH
3626 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3627
3628 addr = die ? this->diesize[0] : 0;
3629
3630 boundary &= FLEXONENAND_PI_MASK;
3631 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3632
3633 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3634 ret = this->wait(mtd, FL_ERASING);
3635 if (ret) {
f369c7ec
MK
3636 printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
3637 __func__, die);
5988af23
RH
3638 goto out;
3639 }
3640
3641 this->write_word(boundary, this->base + ONENAND_DATARAM);
3642 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3643 ret = this->wait(mtd, FL_WRITING);
3644 if (ret) {
297758f8
AKS
3645 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3646 __func__, die);
5988af23
RH
3647 goto out;
3648 }
3649
3650 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3651 ret = this->wait(mtd, FL_WRITING);
3652out:
3653 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3654 this->wait(mtd, FL_RESETING);
3655 if (!ret)
3656 /* Recalculate device size on boundary change*/
3657 flexonenand_get_size(mtd);
3658
3659 return ret;
3660}
3661
cd5f6346 3662/**
ad0d363b 3663 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
cd5f6346
KP
3664 * @param mtd MTD device structure
3665 *
3666 * OneNAND detection method:
59c51591 3667 * Compare the values from command with ones from register
cd5f6346 3668 */
ad0d363b 3669static int onenand_chip_probe(struct mtd_info *mtd)
cd5f6346
KP
3670{
3671 struct onenand_chip *this = mtd->priv;
ad0d363b 3672 int bram_maf_id, bram_dev_id, maf_id, dev_id;
47e777e0
KP
3673 int syscfg;
3674
3675 /* Save system configuration 1 */
3676 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3677 /* Clear Sync. Burst Read mode to read BootRAM */
ee8f3768 3678 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
cd5f6346
KP
3679
3680 /* Send the command for reading device ID from BootRAM */
3681 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3682
3683 /* Read manufacturer and device IDs from BootRAM */
3684 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3685 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3686
47e777e0
KP
3687 /* Reset OneNAND to read default register values */
3688 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3689 /* Wait reset */
3690 this->wait(mtd, FL_RESETING);
3691
3692 /* Restore system configuration 1 */
3693 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3694
cd5f6346
KP
3695 /* Check manufacturer ID */
3696 if (onenand_check_maf(bram_maf_id))
3697 return -ENXIO;
3698
cd5f6346
KP
3699 /* Read manufacturer and device IDs from Register */
3700 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3701 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3702
3703 /* Check OneNAND device */
3704 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3705 return -ENXIO;
3706
ad0d363b
KP
3707 return 0;
3708}
3709
3710/**
3711 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3712 * @param mtd MTD device structure
3713 */
3714static int onenand_probe(struct mtd_info *mtd)
3715{
3716 struct onenand_chip *this = mtd->priv;
6b7368c2 3717 int dev_id, ver_id;
ad0d363b
KP
3718 int density;
3719 int ret;
3720
3721 ret = this->chip_probe(mtd);
3722 if (ret)
3723 return ret;
3724
6b7368c2 3725 /* Device and version IDs from Register */
ad0d363b
KP
3726 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3727 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3728 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3729
cd5f6346 3730 /* Flash device information */
28b79ff9 3731 onenand_print_device_info(dev_id, ver_id);
cd5f6346 3732 this->device_id = dev_id;
28b79ff9 3733 this->version_id = ver_id;
cd5f6346 3734
c37cb56f
KP
3735 /* Check OneNAND features */
3736 onenand_check_features(mtd);
3737
e71f04fc 3738 density = onenand_get_density(dev_id);
5988af23
RH
3739 if (FLEXONENAND(this)) {
3740 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3741 /* Maximum possible erase regions */
3742 mtd->numeraseregions = this->dies << 1;
3743 mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
3744 * (this->dies << 1), GFP_KERNEL);
3745 if (!mtd->eraseregions)
3746 return -ENOMEM;
3747 }
3748
3749 /*
3750 * For Flex-OneNAND, chipsize represents maximum possible device size.
3751 * mtd->size represents the actual device size.
3752 */
cd5f6346
KP
3753 this->chipsize = (16 << density) << 20;
3754
3755 /* OneNAND page size & block size */
3756 /* The data buffer size is equal to page size */
28318776 3757 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
5988af23 3758 /* We use the full BufferRAM */
8a8f632d 3759 if (ONENAND_IS_4KB_PAGE(this))
5988af23
RH
3760 mtd->writesize <<= 1;
3761
28318776 3762 mtd->oobsize = mtd->writesize >> 5;
9bfbc9b2 3763 /* Pages per a block are always 64 in OneNAND */
28318776 3764 mtd->erasesize = mtd->writesize << 6;
5988af23
RH
3765 /*
3766 * Flex-OneNAND SLC area has 64 pages per block.
3767 * Flex-OneNAND MLC area has 128 pages per block.
3768 * Expose MLC erase size to find erase_shift and page_mask.
3769 */
3770 if (FLEXONENAND(this))
3771 mtd->erasesize <<= 1;
cd5f6346
KP
3772
3773 this->erase_shift = ffs(mtd->erasesize) - 1;
28318776 3774 this->page_shift = ffs(mtd->writesize) - 1;
9bfbc9b2 3775 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
5988af23
RH
3776 /* Set density mask. it is used for DDP */
3777 if (ONENAND_IS_DDP(this))
3778 this->density_mask = this->chipsize >> (this->erase_shift + 1);
ee9745fc
KP
3779 /* It's real page size */
3780 this->writesize = mtd->writesize;
cd5f6346 3781
492e1501 3782 /* REVISIT: Multichip handling */
cd5f6346 3783
5988af23
RH
3784 if (FLEXONENAND(this))
3785 flexonenand_get_size(mtd);
3786 else
3787 mtd->size = this->chipsize;
cd5f6346 3788
ee9745fc
KP
3789 /*
3790 * We emulate the 4KiB page and 256KiB erase block size
3791 * But oobsize is still 64 bytes.
3792 * It is only valid if you turn on 2X program support,
3793 * Otherwise it will be ignored by compiler.
3794 */
3795 if (ONENAND_IS_2PLANE(this)) {
3796 mtd->writesize <<= 1;
3797 mtd->erasesize <<= 1;
3798 }
3799
cd5f6346
KP
3800 return 0;
3801}
3802
a41371eb
KP
3803/**
3804 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3805 * @param mtd MTD device structure
3806 */
3807static int onenand_suspend(struct mtd_info *mtd)
3808{
3809 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3810}
3811
3812/**
3813 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3814 * @param mtd MTD device structure
3815 */
3816static void onenand_resume(struct mtd_info *mtd)
3817{
3818 struct onenand_chip *this = mtd->priv;
3819
3820 if (this->state == FL_PM_SUSPENDED)
3821 onenand_release_device(mtd);
3822 else
297758f8
AKS
3823 printk(KERN_ERR "%s: resume() called for the chip which is not "
3824 "in suspended state\n", __func__);
a41371eb
KP
3825}
3826
cd5f6346
KP
3827/**
3828 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3829 * @param mtd MTD device structure
3830 * @param maxchips Number of chips to scan for
3831 *
3832 * This fills out all the not initialized function pointers
3833 * with the defaults.
3834 * The flash ID is read and the mtd/chip structures are
3835 * filled with the appropriate values.
3836 */
3837int onenand_scan(struct mtd_info *mtd, int maxchips)
3838{
5988af23 3839 int i, ret;
cd5f6346
KP
3840 struct onenand_chip *this = mtd->priv;
3841
3842 if (!this->read_word)
3843 this->read_word = onenand_readw;
3844 if (!this->write_word)
3845 this->write_word = onenand_writew;
3846
3847 if (!this->command)
3848 this->command = onenand_command;
3849 if (!this->wait)
2c22120f 3850 onenand_setup_wait(mtd);
31bb999e
KP
3851 if (!this->bbt_wait)
3852 this->bbt_wait = onenand_bbt_wait;
3853 if (!this->unlock_all)
3854 this->unlock_all = onenand_unlock_all;
cd5f6346 3855
ad0d363b
KP
3856 if (!this->chip_probe)
3857 this->chip_probe = onenand_chip_probe;
3858
cd5f6346
KP
3859 if (!this->read_bufferram)
3860 this->read_bufferram = onenand_read_bufferram;
3861 if (!this->write_bufferram)
3862 this->write_bufferram = onenand_write_bufferram;
3863
cdc00130
KP
3864 if (!this->block_markbad)
3865 this->block_markbad = onenand_default_block_markbad;
3866 if (!this->scan_bbt)
3867 this->scan_bbt = onenand_default_bbt;
3868
cd5f6346
KP
3869 if (onenand_probe(mtd))
3870 return -ENXIO;
3871
52b0eea7
KP
3872 /* Set Sync. Burst Read after probing */
3873 if (this->mmcontrol) {
3874 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3875 this->read_bufferram = onenand_sync_read_bufferram;
3876 }
3877
532a37cf
KP
3878 /* Allocate buffers, if necessary */
3879 if (!this->page_buf) {
470bc844 3880 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
e4eec195 3881 if (!this->page_buf)
532a37cf 3882 return -ENOMEM;
4a8ce0b0
KP
3883#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3884 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3885 if (!this->verify_buf) {
3886 kfree(this->page_buf);
3887 return -ENOMEM;
3888 }
3889#endif
532a37cf
KP
3890 this->options |= ONENAND_PAGEBUF_ALLOC;
3891 }
470bc844
KP
3892 if (!this->oob_buf) {
3893 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3894 if (!this->oob_buf) {
470bc844
KP
3895 if (this->options & ONENAND_PAGEBUF_ALLOC) {
3896 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3897 kfree(this->page_buf);
3898 }
3899 return -ENOMEM;
3900 }
3901 this->options |= ONENAND_OOBBUF_ALLOC;
3902 }
532a37cf 3903
cd5f6346
KP
3904 this->state = FL_READY;
3905 init_waitqueue_head(&this->wq);
3906 spin_lock_init(&this->chip_lock);
3907
60d84f97
KP
3908 /*
3909 * Allow subpage writes up to oobsize.
3910 */
cd5f6346 3911 switch (mtd->oobsize) {
5988af23 3912 case 128:
99b17c08 3913 if (FLEXONENAND(this)) {
a411679f 3914 mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops);
99b17c08
RT
3915 mtd->subpage_sft = 0;
3916 } else {
a411679f 3917 mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops);
99b17c08
RT
3918 mtd->subpage_sft = 2;
3919 }
e1c10243
KP
3920 if (ONENAND_IS_NOP_1(this))
3921 mtd->subpage_sft = 0;
5988af23 3922 break;
cd5f6346 3923 case 64:
a411679f 3924 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
60d84f97 3925 mtd->subpage_sft = 2;
cd5f6346
KP
3926 break;
3927
3928 case 32:
a411679f 3929 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
60d84f97 3930 mtd->subpage_sft = 1;
cd5f6346
KP
3931 break;
3932
3933 default:
297758f8
AKS
3934 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
3935 __func__, mtd->oobsize);
60d84f97 3936 mtd->subpage_sft = 0;
cd5f6346 3937 /* To prevent kernel oops */
a411679f 3938 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
cd5f6346
KP
3939 break;
3940 }
3941
60d84f97 3942 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
a5e7c7b4
AH
3943
3944 /*
3945 * The number of bytes available for a client to place data into
3946 * the out of band area
3947 */
d30aae6d
BB
3948 ret = mtd_ooblayout_count_freebytes(mtd);
3949 if (ret < 0)
3950 ret = 0;
3951
3952 mtd->oobavail = ret;
a5e7c7b4 3953
6a918bad 3954 mtd->ecc_strength = 1;
d5c5e78a 3955
cd5f6346 3956 /* Fill in remaining MTD driver data */
c7626802 3957 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
5fa43394 3958 mtd->flags = MTD_CAP_NANDFLASH;
3c3c10bb
AB
3959 mtd->_erase = onenand_erase;
3960 mtd->_point = NULL;
3961 mtd->_unpoint = NULL;
3c3c10bb
AB
3962 mtd->_read_oob = onenand_read_oob;
3963 mtd->_write_oob = onenand_write_oob;
3964 mtd->_panic_write = onenand_panic_write;
493c6460 3965#ifdef CONFIG_MTD_ONENAND_OTP
3c3c10bb
AB
3966 mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
3967 mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
3968 mtd->_get_user_prot_info = onenand_get_user_prot_info;
3969 mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
3970 mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
3971 mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
493c6460 3972#endif
3c3c10bb
AB
3973 mtd->_sync = onenand_sync;
3974 mtd->_lock = onenand_lock;
3975 mtd->_unlock = onenand_unlock;
3976 mtd->_suspend = onenand_suspend;
3977 mtd->_resume = onenand_resume;
3978 mtd->_block_isbad = onenand_block_isbad;
3979 mtd->_block_markbad = onenand_block_markbad;
cd5f6346 3980 mtd->owner = THIS_MODULE;
25dcd297 3981 mtd->writebufsize = mtd->writesize;
cd5f6346
KP
3982
3983 /* Unlock whole block */
b3dcfd35
RT
3984 if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
3985 this->unlock_all(mtd);
cd5f6346 3986
5988af23
RH
3987 ret = this->scan_bbt(mtd);
3988 if ((!FLEXONENAND(this)) || ret)
3989 return ret;
3990
3991 /* Change Flex-OneNAND boundaries if required */
3992 for (i = 0; i < MAX_DIES; i++)
3993 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3994 flex_bdry[(2 * i) + 1]);
3995
3996 return 0;
cd5f6346
KP
3997}
3998
3999/**
4000 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
4001 * @param mtd MTD device structure
4002 */
4003void onenand_release(struct mtd_info *mtd)
4004{
532a37cf
KP
4005 struct onenand_chip *this = mtd->priv;
4006
cd5f6346 4007 /* Deregister partitions */
711a632d 4008 mtd_device_unregister(mtd);
532a37cf
KP
4009
4010 /* Free bad block table memory, if allocated */
f00b0046
AH
4011 if (this->bbm) {
4012 struct bbm_info *bbm = this->bbm;
4013 kfree(bbm->bbt);
532a37cf 4014 kfree(this->bbm);
f00b0046 4015 }
470bc844 4016 /* Buffers allocated by onenand_scan */
4a8ce0b0 4017 if (this->options & ONENAND_PAGEBUF_ALLOC) {
532a37cf 4018 kfree(this->page_buf);
4a8ce0b0
KP
4019#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4020 kfree(this->verify_buf);
4021#endif
4022 }
470bc844
KP
4023 if (this->options & ONENAND_OOBBUF_ALLOC)
4024 kfree(this->oob_buf);
5988af23 4025 kfree(mtd->eraseregions);
cd5f6346
KP
4026}
4027
4028EXPORT_SYMBOL_GPL(onenand_scan);
4029EXPORT_SYMBOL_GPL(onenand_release);
4030
4031MODULE_LICENSE("GPL");
4032MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4033MODULE_DESCRIPTION("Generic OneNAND flash driver code");