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