]> err.no Git - linux-2.6/blob - drivers/mtd/onenand/onenand_base.c
[MTD] OneNAND: Select correct chip's bufferRAM for DDP
[linux-2.6] / drivers / mtd / onenand / onenand_base.c
1 /*
2  *  linux/drivers/mtd/onenand/onenand_base.c
3  *
4  *  Copyright (C) 2005-2007 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/onenand.h>
20 #include <linux/mtd/partitions.h>
21
22 #include <asm/io.h>
23
24 /**
25  * onenand_oob_64 - oob info for large (2KB) page
26  */
27 static struct nand_ecclayout onenand_oob_64 = {
28         .eccbytes       = 20,
29         .eccpos         = {
30                 8, 9, 10, 11, 12,
31                 24, 25, 26, 27, 28,
32                 40, 41, 42, 43, 44,
33                 56, 57, 58, 59, 60,
34                 },
35         .oobfree        = {
36                 {2, 3}, {14, 2}, {18, 3}, {30, 2},
37                 {34, 3}, {46, 2}, {50, 3}, {62, 2}
38         }
39 };
40
41 /**
42  * onenand_oob_32 - oob info for middle (1KB) page
43  */
44 static struct nand_ecclayout onenand_oob_32 = {
45         .eccbytes       = 10,
46         .eccpos         = {
47                 8, 9, 10, 11, 12,
48                 24, 25, 26, 27, 28,
49                 },
50         .oobfree        = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
51 };
52
53 static const unsigned char ffchars[] = {
54         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
56         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
58         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
60         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
62 };
63
64 /**
65  * onenand_readw - [OneNAND Interface] Read OneNAND register
66  * @param addr          address to read
67  *
68  * Read OneNAND register
69  */
70 static unsigned short onenand_readw(void __iomem *addr)
71 {
72         return readw(addr);
73 }
74
75 /**
76  * onenand_writew - [OneNAND Interface] Write OneNAND register with value
77  * @param value         value to write
78  * @param addr          address to write
79  *
80  * Write OneNAND register with value
81  */
82 static void onenand_writew(unsigned short value, void __iomem *addr)
83 {
84         writew(value, addr);
85 }
86
87 /**
88  * onenand_block_address - [DEFAULT] Get block address
89  * @param this          onenand chip data structure
90  * @param block         the block
91  * @return              translated block address if DDP, otherwise same
92  *
93  * Setup Start Address 1 Register (F100h)
94  */
95 static int onenand_block_address(struct onenand_chip *this, int block)
96 {
97         /* Device Flash Core select, NAND Flash Block Address */
98         if (block & this->density_mask)
99                 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
100
101         return block;
102 }
103
104 /**
105  * onenand_bufferram_address - [DEFAULT] Get bufferram address
106  * @param this          onenand chip data structure
107  * @param block         the block
108  * @return              set DBS value if DDP, otherwise 0
109  *
110  * Setup Start Address 2 Register (F101h) for DDP
111  */
112 static int onenand_bufferram_address(struct onenand_chip *this, int block)
113 {
114         /* Device BufferRAM Select */
115         if (block & this->density_mask)
116                 return ONENAND_DDP_CHIP1;
117
118         return ONENAND_DDP_CHIP0;
119 }
120
121 /**
122  * onenand_page_address - [DEFAULT] Get page address
123  * @param page          the page address
124  * @param sector        the sector address
125  * @return              combined page and sector address
126  *
127  * Setup Start Address 8 Register (F107h)
128  */
129 static int onenand_page_address(int page, int sector)
130 {
131         /* Flash Page Address, Flash Sector Address */
132         int fpa, fsa;
133
134         fpa = page & ONENAND_FPA_MASK;
135         fsa = sector & ONENAND_FSA_MASK;
136
137         return ((fpa << ONENAND_FPA_SHIFT) | fsa);
138 }
139
140 /**
141  * onenand_buffer_address - [DEFAULT] Get buffer address
142  * @param dataram1      DataRAM index
143  * @param sectors       the sector address
144  * @param count         the number of sectors
145  * @return              the start buffer value
146  *
147  * Setup Start Buffer Register (F200h)
148  */
149 static int onenand_buffer_address(int dataram1, int sectors, int count)
150 {
151         int bsa, bsc;
152
153         /* BufferRAM Sector Address */
154         bsa = sectors & ONENAND_BSA_MASK;
155
156         if (dataram1)
157                 bsa |= ONENAND_BSA_DATARAM1;    /* DataRAM1 */
158         else
159                 bsa |= ONENAND_BSA_DATARAM0;    /* DataRAM0 */
160
161         /* BufferRAM Sector Count */
162         bsc = count & ONENAND_BSC_MASK;
163
164         return ((bsa << ONENAND_BSA_SHIFT) | bsc);
165 }
166
167 /**
168  * onenand_command - [DEFAULT] Send command to OneNAND device
169  * @param mtd           MTD device structure
170  * @param cmd           the command to be sent
171  * @param addr          offset to read from or write to
172  * @param len           number of bytes to read or write
173  *
174  * Send command to OneNAND device. This function is used for middle/large page
175  * devices (1KB/2KB Bytes per page)
176  */
177 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
178 {
179         struct onenand_chip *this = mtd->priv;
180         int value, readcmd = 0, block_cmd = 0;
181         int block, page;
182
183         /* Address translation */
184         switch (cmd) {
185         case ONENAND_CMD_UNLOCK:
186         case ONENAND_CMD_LOCK:
187         case ONENAND_CMD_LOCK_TIGHT:
188         case ONENAND_CMD_UNLOCK_ALL:
189                 block = -1;
190                 page = -1;
191                 break;
192
193         case ONENAND_CMD_ERASE:
194         case ONENAND_CMD_BUFFERRAM:
195         case ONENAND_CMD_OTP_ACCESS:
196                 block_cmd = 1;
197                 block = (int) (addr >> this->erase_shift);
198                 page = -1;
199                 break;
200
201         default:
202                 block = (int) (addr >> this->erase_shift);
203                 page = (int) (addr >> this->page_shift);
204                 page &= this->page_mask;
205                 break;
206         }
207
208         /* NOTE: The setting order of the registers is very important! */
209         if (cmd == ONENAND_CMD_BUFFERRAM) {
210                 /* Select DataRAM for DDP */
211                 value = onenand_bufferram_address(this, block);
212                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
213
214                 /* Switch to the next data buffer */
215                 ONENAND_SET_NEXT_BUFFERRAM(this);
216
217                 return 0;
218         }
219
220         if (block != -1) {
221                 /* Write 'DFS, FBA' of Flash */
222                 value = onenand_block_address(this, block);
223                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
224
225                 if (block_cmd) {
226                         /* Select DataRAM for DDP */
227                         value = onenand_bufferram_address(this, block);
228                         this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
229                 }
230         }
231
232         if (page != -1) {
233                 /* Now we use page size operation */
234                 int sectors = 4, count = 4;
235                 int dataram;
236
237                 switch (cmd) {
238                 case ONENAND_CMD_READ:
239                 case ONENAND_CMD_READOOB:
240                         dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
241                         readcmd = 1;
242                         break;
243
244                 default:
245                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
246                         break;
247                 }
248
249                 /* Write 'FPA, FSA' of Flash */
250                 value = onenand_page_address(page, sectors);
251                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
252
253                 /* Write 'BSA, BSC' of DataRAM */
254                 value = onenand_buffer_address(dataram, sectors, count);
255                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
256
257                 if (readcmd) {
258                         /* Select DataRAM for DDP */
259                         value = onenand_bufferram_address(this, block);
260                         this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
261                 }
262         }
263
264         /* Interrupt clear */
265         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
266
267         /* Write command */
268         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
269
270         return 0;
271 }
272
273 /**
274  * onenand_wait - [DEFAULT] wait until the command is done
275  * @param mtd           MTD device structure
276  * @param state         state to select the max. timeout value
277  *
278  * Wait for command done. This applies to all OneNAND command
279  * Read can take up to 30us, erase up to 2ms and program up to 350us
280  * according to general OneNAND specs
281  */
282 static int onenand_wait(struct mtd_info *mtd, int state)
283 {
284         struct onenand_chip * this = mtd->priv;
285         unsigned long timeout;
286         unsigned int flags = ONENAND_INT_MASTER;
287         unsigned int interrupt = 0;
288         unsigned int ctrl;
289
290         /* The 20 msec is enough */
291         timeout = jiffies + msecs_to_jiffies(20);
292         while (time_before(jiffies, timeout)) {
293                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
294
295                 if (interrupt & flags)
296                         break;
297
298                 if (state != FL_READING)
299                         cond_resched();
300         }
301         /* To get correct interrupt status in timeout case */
302         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
303
304         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
305
306         if (ctrl & ONENAND_CTRL_ERROR) {
307                 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
308                 if (ctrl & ONENAND_CTRL_LOCK)
309                         printk(KERN_ERR "onenand_wait: it's locked error.\n");
310                 return ctrl;
311         }
312
313         if (interrupt & ONENAND_INT_READ) {
314                 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
315                 if (ecc) {
316                         printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
317                         if (ecc & ONENAND_ECC_2BIT_ALL) {
318                                 mtd->ecc_stats.failed++;
319                                 return ecc;
320                         } else if (ecc & ONENAND_ECC_1BIT_ALL)
321                                 mtd->ecc_stats.corrected++;
322                 }
323         } else if (state == FL_READING) {
324                 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
325                 return -EIO;
326         }
327
328         return 0;
329 }
330
331 /*
332  * onenand_interrupt - [DEFAULT] onenand interrupt handler
333  * @param irq           onenand interrupt number
334  * @param dev_id        interrupt data
335  *
336  * complete the work
337  */
338 static irqreturn_t onenand_interrupt(int irq, void *data)
339 {
340         struct onenand_chip *this = (struct onenand_chip *) data;
341
342         /* To handle shared interrupt */
343         if (!this->complete.done)
344                 complete(&this->complete);
345
346         return IRQ_HANDLED;
347 }
348
349 /*
350  * onenand_interrupt_wait - [DEFAULT] wait until the command is done
351  * @param mtd           MTD device structure
352  * @param state         state to select the max. timeout value
353  *
354  * Wait for command done.
355  */
356 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
357 {
358         struct onenand_chip *this = mtd->priv;
359
360         wait_for_completion(&this->complete);
361
362         return onenand_wait(mtd, state);
363 }
364
365 /*
366  * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
367  * @param mtd           MTD device structure
368  * @param state         state to select the max. timeout value
369  *
370  * Try interrupt based wait (It is used one-time)
371  */
372 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
373 {
374         struct onenand_chip *this = mtd->priv;
375         unsigned long remain, timeout;
376
377         /* We use interrupt wait first */
378         this->wait = onenand_interrupt_wait;
379
380         timeout = msecs_to_jiffies(100);
381         remain = wait_for_completion_timeout(&this->complete, timeout);
382         if (!remain) {
383                 printk(KERN_INFO "OneNAND: There's no interrupt. "
384                                 "We use the normal wait\n");
385
386                 /* Release the irq */
387                 free_irq(this->irq, this);
388
389                 this->wait = onenand_wait;
390         }
391
392         return onenand_wait(mtd, state);
393 }
394
395 /*
396  * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
397  * @param mtd           MTD device structure
398  *
399  * There's two method to wait onenand work
400  * 1. polling - read interrupt status register
401  * 2. interrupt - use the kernel interrupt method
402  */
403 static void onenand_setup_wait(struct mtd_info *mtd)
404 {
405         struct onenand_chip *this = mtd->priv;
406         int syscfg;
407
408         init_completion(&this->complete);
409
410         if (this->irq <= 0) {
411                 this->wait = onenand_wait;
412                 return;
413         }
414
415         if (request_irq(this->irq, &onenand_interrupt,
416                                 IRQF_SHARED, "onenand", this)) {
417                 /* If we can't get irq, use the normal wait */
418                 this->wait = onenand_wait;
419                 return;
420         }
421
422         /* Enable interrupt */
423         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
424         syscfg |= ONENAND_SYS_CFG1_IOBE;
425         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
426
427         this->wait = onenand_try_interrupt_wait;
428 }
429
430 /**
431  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
432  * @param mtd           MTD data structure
433  * @param area          BufferRAM area
434  * @return              offset given area
435  *
436  * Return BufferRAM offset given area
437  */
438 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
439 {
440         struct onenand_chip *this = mtd->priv;
441
442         if (ONENAND_CURRENT_BUFFERRAM(this)) {
443                 if (area == ONENAND_DATARAM)
444                         return mtd->writesize;
445                 if (area == ONENAND_SPARERAM)
446                         return mtd->oobsize;
447         }
448
449         return 0;
450 }
451
452 /**
453  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
454  * @param mtd           MTD data structure
455  * @param area          BufferRAM area
456  * @param buffer        the databuffer to put/get data
457  * @param offset        offset to read from or write to
458  * @param count         number of bytes to read/write
459  *
460  * Read the BufferRAM area
461  */
462 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
463                 unsigned char *buffer, int offset, size_t count)
464 {
465         struct onenand_chip *this = mtd->priv;
466         void __iomem *bufferram;
467
468         bufferram = this->base + area;
469
470         bufferram += onenand_bufferram_offset(mtd, area);
471
472         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
473                 unsigned short word;
474
475                 /* Align with word(16-bit) size */
476                 count--;
477
478                 /* Read word and save byte */
479                 word = this->read_word(bufferram + offset + count);
480                 buffer[count] = (word & 0xff);
481         }
482
483         memcpy(buffer, bufferram + offset, count);
484
485         return 0;
486 }
487
488 /**
489  * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
490  * @param mtd           MTD data structure
491  * @param area          BufferRAM area
492  * @param buffer        the databuffer to put/get data
493  * @param offset        offset to read from or write to
494  * @param count         number of bytes to read/write
495  *
496  * Read the BufferRAM area with Sync. Burst Mode
497  */
498 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
499                 unsigned char *buffer, int offset, size_t count)
500 {
501         struct onenand_chip *this = mtd->priv;
502         void __iomem *bufferram;
503
504         bufferram = this->base + area;
505
506         bufferram += onenand_bufferram_offset(mtd, area);
507
508         this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
509
510         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
511                 unsigned short word;
512
513                 /* Align with word(16-bit) size */
514                 count--;
515
516                 /* Read word and save byte */
517                 word = this->read_word(bufferram + offset + count);
518                 buffer[count] = (word & 0xff);
519         }
520
521         memcpy(buffer, bufferram + offset, count);
522
523         this->mmcontrol(mtd, 0);
524
525         return 0;
526 }
527
528 /**
529  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
530  * @param mtd           MTD data structure
531  * @param area          BufferRAM area
532  * @param buffer        the databuffer to put/get data
533  * @param offset        offset to read from or write to
534  * @param count         number of bytes to read/write
535  *
536  * Write the BufferRAM area
537  */
538 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
539                 const unsigned char *buffer, int offset, size_t count)
540 {
541         struct onenand_chip *this = mtd->priv;
542         void __iomem *bufferram;
543
544         bufferram = this->base + area;
545
546         bufferram += onenand_bufferram_offset(mtd, area);
547
548         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
549                 unsigned short word;
550                 int byte_offset;
551
552                 /* Align with word(16-bit) size */
553                 count--;
554
555                 /* Calculate byte access offset */
556                 byte_offset = offset + count;
557
558                 /* Read word and save byte */
559                 word = this->read_word(bufferram + byte_offset);
560                 word = (word & ~0xff) | buffer[count];
561                 this->write_word(word, bufferram + byte_offset);
562         }
563
564         memcpy(bufferram + offset, buffer, count);
565
566         return 0;
567 }
568
569 /**
570  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
571  * @param mtd           MTD data structure
572  * @param addr          address to check
573  * @return              1 if there are valid data, otherwise 0
574  *
575  * Check bufferram if there is data we required
576  */
577 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
578 {
579         struct onenand_chip *this = mtd->priv;
580         int blockpage, found = 0;
581         unsigned int i;
582
583         blockpage = (int) (addr >> this->page_shift);
584
585         /* Is there valid data? */
586         i = ONENAND_CURRENT_BUFFERRAM(this);
587         if (this->bufferram[i].blockpage == blockpage)
588                 found = 1;
589         else {
590                 /* Check another BufferRAM */
591                 i = ONENAND_NEXT_BUFFERRAM(this);
592                 if (this->bufferram[i].blockpage == blockpage) {
593                         ONENAND_SET_NEXT_BUFFERRAM(this);
594                         found = 1;
595                 }
596         }
597
598         if (found && ONENAND_IS_DDP(this)) {
599                 /* Select DataRAM for DDP */
600                 int block = (int) (addr >> this->erase_shift);
601                 int value = onenand_bufferram_address(this, block);
602                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
603         }
604
605         return found;
606 }
607
608 /**
609  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
610  * @param mtd           MTD data structure
611  * @param addr          address to update
612  * @param valid         valid flag
613  *
614  * Update BufferRAM information
615  */
616 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
617                 int valid)
618 {
619         struct onenand_chip *this = mtd->priv;
620         int blockpage;
621         unsigned int i;
622
623         blockpage = (int) (addr >> this->page_shift);
624
625         /* Invalidate another BufferRAM */
626         i = ONENAND_NEXT_BUFFERRAM(this);
627         if (this->bufferram[i].blockpage == blockpage)
628                 this->bufferram[i].blockpage = -1;
629
630         /* Update BufferRAM */
631         i = ONENAND_CURRENT_BUFFERRAM(this);
632         if (valid)
633                 this->bufferram[i].blockpage = blockpage;
634         else
635                 this->bufferram[i].blockpage = -1;
636 }
637
638 /**
639  * onenand_get_device - [GENERIC] Get chip for selected access
640  * @param mtd           MTD device structure
641  * @param new_state     the state which is requested
642  *
643  * Get the device and lock it for exclusive access
644  */
645 static int onenand_get_device(struct mtd_info *mtd, int new_state)
646 {
647         struct onenand_chip *this = mtd->priv;
648         DECLARE_WAITQUEUE(wait, current);
649
650         /*
651          * Grab the lock and see if the device is available
652          */
653         while (1) {
654                 spin_lock(&this->chip_lock);
655                 if (this->state == FL_READY) {
656                         this->state = new_state;
657                         spin_unlock(&this->chip_lock);
658                         break;
659                 }
660                 if (new_state == FL_PM_SUSPENDED) {
661                         spin_unlock(&this->chip_lock);
662                         return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
663                 }
664                 set_current_state(TASK_UNINTERRUPTIBLE);
665                 add_wait_queue(&this->wq, &wait);
666                 spin_unlock(&this->chip_lock);
667                 schedule();
668                 remove_wait_queue(&this->wq, &wait);
669         }
670
671         return 0;
672 }
673
674 /**
675  * onenand_release_device - [GENERIC] release chip
676  * @param mtd           MTD device structure
677  *
678  * Deselect, release chip lock and wake up anyone waiting on the device
679  */
680 static void onenand_release_device(struct mtd_info *mtd)
681 {
682         struct onenand_chip *this = mtd->priv;
683
684         /* Release the chip */
685         spin_lock(&this->chip_lock);
686         this->state = FL_READY;
687         wake_up(&this->wq);
688         spin_unlock(&this->chip_lock);
689 }
690
691 /**
692  * onenand_read - [MTD Interface] Read data from flash
693  * @param mtd           MTD device structure
694  * @param from          offset to read from
695  * @param len           number of bytes to read
696  * @param retlen        pointer to variable to store the number of read bytes
697  * @param buf           the databuffer to put data
698  *
699  * Read with ecc
700 */
701 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
702         size_t *retlen, u_char *buf)
703 {
704         struct onenand_chip *this = mtd->priv;
705         struct mtd_ecc_stats stats;
706         int read = 0, column;
707         int thislen;
708         int ret = 0, boundary = 0;
709
710         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
711
712         /* Do not allow reads past end of device */
713         if ((from + len) > mtd->size) {
714                 printk(KERN_ERR "onenand_read: Attempt read beyond end of device\n");
715                 *retlen = 0;
716                 return -EINVAL;
717         }
718
719         /* Grab the lock and see if the device is available */
720         onenand_get_device(mtd, FL_READING);
721
722         stats = mtd->ecc_stats;
723
724         /* Read-while-load method */
725
726         /* Do first load to bufferRAM */
727         if (read < len) {
728                 if (!onenand_check_bufferram(mtd, from)) {
729                         this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
730                         ret = this->wait(mtd, FL_READING);
731                         onenand_update_bufferram(mtd, from, !ret);
732                 }
733         }
734
735         thislen = min_t(int, mtd->writesize, len - read);
736         column = from & (mtd->writesize - 1);
737         if (column + thislen > mtd->writesize)
738                 thislen = mtd->writesize - column;
739
740         while (!ret) {
741                 /* If there is more to load then start next load */
742                 from += thislen;
743                 if (read + thislen < len) {
744                         this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
745                         /*
746                          * Chip boundary handling in DDP
747                          * Now we issued chip 1 read and pointed chip 1
748                          * bufferam so we have to point chip 0 bufferam.
749                          */
750                         if (ONENAND_IS_DDP(this) &&
751                             unlikely(from == (this->chipsize >> 1))) {
752                                 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
753                                 boundary = 1;
754                         } else
755                                 boundary = 0;
756                         ONENAND_SET_PREV_BUFFERRAM(this);
757                 }
758                 /* While load is going, read from last bufferRAM */
759                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
760                 /* See if we are done */
761                 read += thislen;
762                 if (read == len)
763                         break;
764                 /* Set up for next read from bufferRAM */
765                 if (unlikely(boundary))
766                         this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
767                 ONENAND_SET_NEXT_BUFFERRAM(this);
768                 buf += thislen;
769                 thislen = min_t(int, mtd->writesize, len - read);
770                 column = 0;
771                 cond_resched();
772                 /* Now wait for load */
773                 ret = this->wait(mtd, FL_READING);
774                 onenand_update_bufferram(mtd, from, !ret);
775         }
776
777         /* Deselect and wake up anyone waiting on the device */
778         onenand_release_device(mtd);
779
780         /*
781          * Return success, if no ECC failures, else -EBADMSG
782          * fs driver will take care of that, because
783          * retlen == desired len and result == -EBADMSG
784          */
785         *retlen = read;
786
787         if (mtd->ecc_stats.failed - stats.failed)
788                 return -EBADMSG;
789
790         if (ret)
791                 return ret;
792
793         return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
794 }
795
796 /**
797  * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
798  * @param mtd           MTD device structure
799  * @param buf           destination address
800  * @param column        oob offset to read from
801  * @param thislen       oob length to read
802  */
803 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
804                                 int thislen)
805 {
806         struct onenand_chip *this = mtd->priv;
807         struct nand_oobfree *free;
808         int readcol = column;
809         int readend = column + thislen;
810         int lastgap = 0;
811         uint8_t *oob_buf = this->page_buf + mtd->writesize;
812
813         for (free = this->ecclayout->oobfree; free->length; ++free) {
814                 if (readcol >= lastgap)
815                         readcol += free->offset - lastgap;
816                 if (readend >= lastgap)
817                         readend += free->offset - lastgap;
818                 lastgap = free->offset + free->length;
819         }
820         this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
821         for (free = this->ecclayout->oobfree; free->length; ++free) {
822                 int free_end = free->offset + free->length;
823                 if (free->offset < readend && free_end > readcol) {
824                         int st = max_t(int,free->offset,readcol);
825                         int ed = min_t(int,free_end,readend);
826                         int n = ed - st;
827                         memcpy(buf, oob_buf + st, n);
828                         buf += n;
829                 }
830         }
831         return 0;
832 }
833
834 /**
835  * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
836  * @param mtd           MTD device structure
837  * @param from          offset to read from
838  * @param len           number of bytes to read
839  * @param retlen        pointer to variable to store the number of read bytes
840  * @param buf           the databuffer to put data
841  * @param mode          operation mode
842  *
843  * OneNAND read out-of-band data from the spare area
844  */
845 static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
846                         size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
847 {
848         struct onenand_chip *this = mtd->priv;
849         int read = 0, thislen, column, oobsize;
850         int ret = 0;
851
852         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
853
854         /* Initialize return length value */
855         *retlen = 0;
856
857         if (mode == MTD_OOB_AUTO)
858                 oobsize = this->ecclayout->oobavail;
859         else
860                 oobsize = mtd->oobsize;
861
862         column = from & (mtd->oobsize - 1);
863
864         if (unlikely(column >= oobsize)) {
865                 printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
866                 return -EINVAL;
867         }
868
869         /* Do not allow reads past end of device */
870         if (unlikely(from >= mtd->size ||
871                      column + len > ((mtd->size >> this->page_shift) -
872                                      (from >> this->page_shift)) * oobsize)) {
873                 printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
874                 return -EINVAL;
875         }
876
877         /* Grab the lock and see if the device is available */
878         onenand_get_device(mtd, FL_READING);
879
880         while (read < len) {
881                 cond_resched();
882
883                 thislen = oobsize - column;
884                 thislen = min_t(int, thislen, len);
885
886                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
887
888                 onenand_update_bufferram(mtd, from, 0);
889
890                 ret = this->wait(mtd, FL_READING);
891                 /* First copy data and check return value for ECC handling */
892
893                 if (mode == MTD_OOB_AUTO)
894                         onenand_transfer_auto_oob(mtd, buf, column, thislen);
895                 else
896                         this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
897
898                 if (ret) {
899                         printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
900                         break;
901                 }
902
903                 read += thislen;
904
905                 if (read == len)
906                         break;
907
908                 buf += thislen;
909
910                 /* Read more? */
911                 if (read < len) {
912                         /* Page size */
913                         from += mtd->writesize;
914                         column = 0;
915                 }
916         }
917
918         /* Deselect and wake up anyone waiting on the device */
919         onenand_release_device(mtd);
920
921         *retlen = read;
922         return ret;
923 }
924
925 /**
926  * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
927  * @mtd:        MTD device structure
928  * @from:       offset to read from
929  * @ops:        oob operation description structure
930  */
931 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
932                             struct mtd_oob_ops *ops)
933 {
934         switch (ops->mode) {
935         case MTD_OOB_PLACE:
936         case MTD_OOB_AUTO:
937                 break;
938         case MTD_OOB_RAW:
939                 /* Not implemented yet */
940         default:
941                 return -EINVAL;
942         }
943         return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
944                                    &ops->oobretlen, ops->oobbuf, ops->mode);
945 }
946
947 /**
948  * onenand_bbt_wait - [DEFAULT] wait until the command is done
949  * @param mtd           MTD device structure
950  * @param state         state to select the max. timeout value
951  *
952  * Wait for command done.
953  */
954 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
955 {
956         struct onenand_chip *this = mtd->priv;
957         unsigned long timeout;
958         unsigned int interrupt;
959         unsigned int ctrl;
960
961         /* The 20 msec is enough */
962         timeout = jiffies + msecs_to_jiffies(20);
963         while (time_before(jiffies, timeout)) {
964                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
965                 if (interrupt & ONENAND_INT_MASTER)
966                         break;
967         }
968         /* To get correct interrupt status in timeout case */
969         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
970         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
971
972         if (ctrl & ONENAND_CTRL_ERROR) {
973                 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
974                 /* Initial bad block case */
975                 if (ctrl & ONENAND_CTRL_LOAD)
976                         return ONENAND_BBT_READ_ERROR;
977                 return ONENAND_BBT_READ_FATAL_ERROR;
978         }
979
980         if (interrupt & ONENAND_INT_READ) {
981                 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
982                 if (ecc & ONENAND_ECC_2BIT_ALL)
983                         return ONENAND_BBT_READ_ERROR;
984         } else {
985                 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
986                         "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
987                 return ONENAND_BBT_READ_FATAL_ERROR;
988         }
989
990         return 0;
991 }
992
993 /**
994  * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
995  * @param mtd           MTD device structure
996  * @param from          offset to read from
997  * @param @ops          oob operation description structure
998  *
999  * OneNAND read out-of-band data from the spare area for bbt scan
1000  */
1001 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, 
1002                             struct mtd_oob_ops *ops)
1003 {
1004         struct onenand_chip *this = mtd->priv;
1005         int read = 0, thislen, column;
1006         int ret = 0;
1007         size_t len = ops->ooblen;
1008         u_char *buf = ops->oobbuf;
1009
1010         DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, len);
1011
1012         /* Initialize return value */
1013         ops->oobretlen = 0;
1014
1015         /* Do not allow reads past end of device */
1016         if (unlikely((from + len) > mtd->size)) {
1017                 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1018                 return ONENAND_BBT_READ_FATAL_ERROR;
1019         }
1020
1021         /* Grab the lock and see if the device is available */
1022         onenand_get_device(mtd, FL_READING);
1023
1024         column = from & (mtd->oobsize - 1);
1025
1026         while (read < len) {
1027                 cond_resched();
1028
1029                 thislen = mtd->oobsize - column;
1030                 thislen = min_t(int, thislen, len);
1031
1032                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1033
1034                 onenand_update_bufferram(mtd, from, 0);
1035
1036                 ret = onenand_bbt_wait(mtd, FL_READING);
1037                 if (ret)
1038                         break;
1039
1040                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1041                 read += thislen;
1042                 if (read == len)
1043                         break;
1044
1045                 buf += thislen;
1046
1047                 /* Read more? */
1048                 if (read < len) {
1049                         /* Update Page size */
1050                         from += mtd->writesize;
1051                         column = 0;
1052                 }
1053         }
1054
1055         /* Deselect and wake up anyone waiting on the device */
1056         onenand_release_device(mtd);
1057
1058         ops->oobretlen = read;
1059         return ret;
1060 }
1061
1062 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1063 /**
1064  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1065  * @param mtd           MTD device structure
1066  * @param buf           the databuffer to verify
1067  * @param to            offset to read from
1068  *
1069  */
1070 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1071 {
1072         struct onenand_chip *this = mtd->priv;
1073         char *readp = this->page_buf + mtd->writesize;
1074         int status, i;
1075
1076         this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1077         onenand_update_bufferram(mtd, to, 0);
1078         status = this->wait(mtd, FL_READING);
1079         if (status)
1080                 return status;
1081
1082         this->read_bufferram(mtd, ONENAND_SPARERAM, readp, 0, mtd->oobsize);
1083         for(i = 0; i < mtd->oobsize; i++)
1084                 if (buf[i] != 0xFF && buf[i] != readp[i])
1085                         return -EBADMSG;
1086
1087         return 0;
1088 }
1089
1090 /**
1091  * onenand_verify - [GENERIC] verify the chip contents after a write
1092  * @param mtd          MTD device structure
1093  * @param buf          the databuffer to verify
1094  * @param addr         offset to read from
1095  * @param len          number of bytes to read and compare
1096  *
1097  */
1098 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1099 {
1100         struct onenand_chip *this = mtd->priv;
1101         void __iomem *dataram;
1102         int ret = 0;
1103         int thislen, column;
1104
1105         while (len != 0) {
1106                 thislen = min_t(int, mtd->writesize, len);
1107                 column = addr & (mtd->writesize - 1);
1108                 if (column + thislen > mtd->writesize)
1109                         thislen = mtd->writesize - column;
1110
1111                 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
1112
1113                 onenand_update_bufferram(mtd, addr, 0);
1114
1115                 ret = this->wait(mtd, FL_READING);
1116                 if (ret)
1117                         return ret;
1118
1119                 onenand_update_bufferram(mtd, addr, 1);
1120
1121                 dataram = this->base + ONENAND_DATARAM;
1122                 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1123
1124                 if (memcmp(buf, dataram + column, thislen))
1125                         return -EBADMSG;
1126
1127                 len -= thislen;
1128                 buf += thislen;
1129                 addr += thislen;
1130         }
1131
1132         return 0;
1133 }
1134 #else
1135 #define onenand_verify(...)             (0)
1136 #define onenand_verify_oob(...)         (0)
1137 #endif
1138
1139 #define NOTALIGNED(x)   ((x & (this->subpagesize - 1)) != 0)
1140
1141 /**
1142  * onenand_write - [MTD Interface] write buffer to FLASH
1143  * @param mtd           MTD device structure
1144  * @param to            offset to write to
1145  * @param len           number of bytes to write
1146  * @param retlen        pointer to variable to store the number of written bytes
1147  * @param buf           the data to write
1148  *
1149  * Write with ECC
1150  */
1151 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1152         size_t *retlen, const u_char *buf)
1153 {
1154         struct onenand_chip *this = mtd->priv;
1155         int written = 0;
1156         int ret = 0;
1157         int column, subpage;
1158
1159         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1160
1161         /* Initialize retlen, in case of early exit */
1162         *retlen = 0;
1163
1164         /* Do not allow writes past end of device */
1165         if (unlikely((to + len) > mtd->size)) {
1166                 printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
1167                 return -EINVAL;
1168         }
1169
1170         /* Reject writes, which are not page aligned */
1171         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1172                 printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
1173                 return -EINVAL;
1174         }
1175
1176         column = to & (mtd->writesize - 1);
1177
1178         /* Grab the lock and see if the device is available */
1179         onenand_get_device(mtd, FL_WRITING);
1180
1181         /* Loop until all data write */
1182         while (written < len) {
1183                 int thislen = min_t(int, mtd->writesize - column, len - written);
1184                 u_char *wbuf = (u_char *) buf;
1185
1186                 cond_resched();
1187
1188                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1189
1190                 /* Partial page write */
1191                 subpage = thislen < mtd->writesize;
1192                 if (subpage) {
1193                         memset(this->page_buf, 0xff, mtd->writesize);
1194                         memcpy(this->page_buf + column, buf, thislen);
1195                         wbuf = this->page_buf;
1196                 }
1197
1198                 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1199                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1200
1201                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1202
1203                 ret = this->wait(mtd, FL_WRITING);
1204
1205                 /* In partial page write we don't update bufferram */
1206                 onenand_update_bufferram(mtd, to, !ret && !subpage);
1207
1208                 if (ret) {
1209                         printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
1210                         break;
1211                 }
1212
1213                 /* Only check verify write turn on */
1214                 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1215                 if (ret) {
1216                         printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
1217                         break;
1218                 }
1219
1220                 written += thislen;
1221
1222                 if (written == len)
1223                         break;
1224
1225                 column = 0;
1226                 to += thislen;
1227                 buf += thislen;
1228         }
1229
1230         /* Deselect and wake up anyone waiting on the device */
1231         onenand_release_device(mtd);
1232
1233         *retlen = written;
1234
1235         return ret;
1236 }
1237
1238 /**
1239  * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1240  * @param mtd           MTD device structure
1241  * @param oob_buf       oob buffer
1242  * @param buf           source address
1243  * @param column        oob offset to write to
1244  * @param thislen       oob length to write
1245  */
1246 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1247                                   const u_char *buf, int column, int thislen)
1248 {
1249         struct onenand_chip *this = mtd->priv;
1250         struct nand_oobfree *free;
1251         int writecol = column;
1252         int writeend = column + thislen;
1253         int lastgap = 0;
1254
1255         for (free = this->ecclayout->oobfree; free->length; ++free) {
1256                 if (writecol >= lastgap)
1257                         writecol += free->offset - lastgap;
1258                 if (writeend >= lastgap)
1259                         writeend += free->offset - lastgap;
1260                 lastgap = free->offset + free->length;
1261         }
1262         for (free = this->ecclayout->oobfree; free->length; ++free) {
1263                 int free_end = free->offset + free->length;
1264                 if (free->offset < writeend && free_end > writecol) {
1265                         int st = max_t(int,free->offset,writecol);
1266                         int ed = min_t(int,free_end,writeend);
1267                         int n = ed - st;
1268                         memcpy(oob_buf + st, buf, n);
1269                         buf += n;
1270                 }
1271         }
1272         return 0;
1273 }
1274
1275 /**
1276  * onenand_do_write_oob - [Internal] OneNAND write out-of-band
1277  * @param mtd           MTD device structure
1278  * @param to            offset to write to
1279  * @param len           number of bytes to write
1280  * @param retlen        pointer to variable to store the number of written bytes
1281  * @param buf           the data to write
1282  * @param mode          operation mode
1283  *
1284  * OneNAND write out-of-band
1285  */
1286 static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
1287                                 size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
1288 {
1289         struct onenand_chip *this = mtd->priv;
1290         int column, ret = 0, oobsize;
1291         int written = 0;
1292
1293         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1294
1295         /* Initialize retlen, in case of early exit */
1296         *retlen = 0;
1297
1298         if (mode == MTD_OOB_AUTO)
1299                 oobsize = this->ecclayout->oobavail;
1300         else
1301                 oobsize = mtd->oobsize;
1302
1303         column = to & (mtd->oobsize - 1);
1304
1305         if (unlikely(column >= oobsize)) {
1306                 printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
1307                 return -EINVAL;
1308         }
1309
1310         /* For compatibility with NAND: Do not allow write past end of page */
1311         if (column + len > oobsize) {
1312                 printk(KERN_ERR "onenand_write_oob: "
1313                       "Attempt to write past end of page\n");
1314                 return -EINVAL;
1315         }
1316
1317         /* Do not allow reads past end of device */
1318         if (unlikely(to >= mtd->size ||
1319                      column + len > ((mtd->size >> this->page_shift) -
1320                                      (to >> this->page_shift)) * oobsize)) {
1321                 printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
1322                 return -EINVAL;
1323         }
1324
1325         /* Grab the lock and see if the device is available */
1326         onenand_get_device(mtd, FL_WRITING);
1327
1328         /* Loop until all data write */
1329         while (written < len) {
1330                 int thislen = min_t(int, oobsize, len - written);
1331
1332                 cond_resched();
1333
1334                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1335
1336                 /* We send data to spare ram with oobsize
1337                  * to prevent byte access */
1338                 memset(this->page_buf, 0xff, mtd->oobsize);
1339                 if (mode == MTD_OOB_AUTO)
1340                         onenand_fill_auto_oob(mtd, this->page_buf, buf, column, thislen);
1341                 else
1342                         memcpy(this->page_buf + column, buf, thislen);
1343                 this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
1344
1345                 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1346
1347                 onenand_update_bufferram(mtd, to, 0);
1348
1349                 ret = this->wait(mtd, FL_WRITING);
1350                 if (ret) {
1351                         printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
1352                         break;
1353                 }
1354
1355                 ret = onenand_verify_oob(mtd, this->page_buf, to);
1356                 if (ret) {
1357                         printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
1358                         break;
1359                 }
1360
1361                 written += thislen;
1362                 if (written == len)
1363                         break;
1364
1365                 to += mtd->writesize;
1366                 buf += thislen;
1367                 column = 0;
1368         }
1369
1370         /* Deselect and wake up anyone waiting on the device */
1371         onenand_release_device(mtd);
1372
1373         *retlen = written;
1374
1375         return ret;
1376 }
1377
1378 /**
1379  * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1380  * @mtd:        MTD device structure
1381  * @from:       offset to read from
1382  * @ops:        oob operation description structure
1383  */
1384 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1385                              struct mtd_oob_ops *ops)
1386 {
1387         switch (ops->mode) {
1388         case MTD_OOB_PLACE:
1389         case MTD_OOB_AUTO:
1390                 break;
1391         case MTD_OOB_RAW:
1392                 /* Not implemented yet */
1393         default:
1394                 return -EINVAL;
1395         }
1396         return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
1397                                     &ops->oobretlen, ops->oobbuf, ops->mode);
1398 }
1399
1400 /**
1401  * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1402  * @param mtd           MTD device structure
1403  * @param ofs           offset from device start
1404  * @param getchip       0, if the chip is already selected
1405  * @param allowbbt      1, if its allowed to access the bbt area
1406  *
1407  * Check, if the block is bad. Either by reading the bad block table or
1408  * calling of the scan function.
1409  */
1410 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1411 {
1412         struct onenand_chip *this = mtd->priv;
1413         struct bbm_info *bbm = this->bbm;
1414
1415         /* Return info from the table */
1416         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1417 }
1418
1419 /**
1420  * onenand_erase - [MTD Interface] erase block(s)
1421  * @param mtd           MTD device structure
1422  * @param instr         erase instruction
1423  *
1424  * Erase one ore more blocks
1425  */
1426 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1427 {
1428         struct onenand_chip *this = mtd->priv;
1429         unsigned int block_size;
1430         loff_t addr;
1431         int len;
1432         int ret = 0;
1433
1434         DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1435
1436         block_size = (1 << this->erase_shift);
1437
1438         /* Start address must align on block boundary */
1439         if (unlikely(instr->addr & (block_size - 1))) {
1440                 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1441                 return -EINVAL;
1442         }
1443
1444         /* Length must align on block boundary */
1445         if (unlikely(instr->len & (block_size - 1))) {
1446                 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1447                 return -EINVAL;
1448         }
1449
1450         /* Do not allow erase past end of device */
1451         if (unlikely((instr->len + instr->addr) > mtd->size)) {
1452                 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1453                 return -EINVAL;
1454         }
1455
1456         instr->fail_addr = 0xffffffff;
1457
1458         /* Grab the lock and see if the device is available */
1459         onenand_get_device(mtd, FL_ERASING);
1460
1461         /* Loop throught the pages */
1462         len = instr->len;
1463         addr = instr->addr;
1464
1465         instr->state = MTD_ERASING;
1466
1467         while (len) {
1468                 cond_resched();
1469
1470                 /* Check if we have a bad block, we do not erase bad blocks */
1471                 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1472                         printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1473                         instr->state = MTD_ERASE_FAILED;
1474                         goto erase_exit;
1475                 }
1476
1477                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1478
1479                 ret = this->wait(mtd, FL_ERASING);
1480                 /* Check, if it is write protected */
1481                 if (ret) {
1482                         printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1483                         instr->state = MTD_ERASE_FAILED;
1484                         instr->fail_addr = addr;
1485                         goto erase_exit;
1486                 }
1487
1488                 len -= block_size;
1489                 addr += block_size;
1490         }
1491
1492         instr->state = MTD_ERASE_DONE;
1493
1494 erase_exit:
1495
1496         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1497         /* Do call back function */
1498         if (!ret)
1499                 mtd_erase_callback(instr);
1500
1501         /* Deselect and wake up anyone waiting on the device */
1502         onenand_release_device(mtd);
1503
1504         return ret;
1505 }
1506
1507 /**
1508  * onenand_sync - [MTD Interface] sync
1509  * @param mtd           MTD device structure
1510  *
1511  * Sync is actually a wait for chip ready function
1512  */
1513 static void onenand_sync(struct mtd_info *mtd)
1514 {
1515         DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1516
1517         /* Grab the lock and see if the device is available */
1518         onenand_get_device(mtd, FL_SYNCING);
1519
1520         /* Release it and go back */
1521         onenand_release_device(mtd);
1522 }
1523
1524 /**
1525  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1526  * @param mtd           MTD device structure
1527  * @param ofs           offset relative to mtd start
1528  *
1529  * Check whether the block is bad
1530  */
1531 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1532 {
1533         /* Check for invalid offset */
1534         if (ofs > mtd->size)
1535                 return -EINVAL;
1536
1537         return onenand_block_checkbad(mtd, ofs, 1, 0);
1538 }
1539
1540 /**
1541  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1542  * @param mtd           MTD device structure
1543  * @param ofs           offset from device start
1544  *
1545  * This is the default implementation, which can be overridden by
1546  * a hardware specific driver.
1547  */
1548 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1549 {
1550         struct onenand_chip *this = mtd->priv;
1551         struct bbm_info *bbm = this->bbm;
1552         u_char buf[2] = {0, 0};
1553         size_t retlen;
1554         int block;
1555
1556         /* Get block number */
1557         block = ((int) ofs) >> bbm->bbt_erase_shift;
1558         if (bbm->bbt)
1559                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1560
1561         /* We write two bytes, so we dont have to mess with 16 bit access */
1562         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1563         return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
1564 }
1565
1566 /**
1567  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1568  * @param mtd           MTD device structure
1569  * @param ofs           offset relative to mtd start
1570  *
1571  * Mark the block as bad
1572  */
1573 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1574 {
1575         struct onenand_chip *this = mtd->priv;
1576         int ret;
1577
1578         ret = onenand_block_isbad(mtd, ofs);
1579         if (ret) {
1580                 /* If it was bad already, return success and do nothing */
1581                 if (ret > 0)
1582                         return 0;
1583                 return ret;
1584         }
1585
1586         return this->block_markbad(mtd, ofs);
1587 }
1588
1589 /**
1590  * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1591  * @param mtd           MTD device structure
1592  * @param ofs           offset relative to mtd start
1593  * @param len           number of bytes to lock or unlock
1594  *
1595  * Lock or unlock one or more blocks
1596  */
1597 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1598 {
1599         struct onenand_chip *this = mtd->priv;
1600         int start, end, block, value, status;
1601         int wp_status_mask;
1602
1603         start = ofs >> this->erase_shift;
1604         end = len >> this->erase_shift;
1605
1606         if (cmd == ONENAND_CMD_LOCK)
1607                 wp_status_mask = ONENAND_WP_LS;
1608         else
1609                 wp_status_mask = ONENAND_WP_US;
1610
1611         /* Continuous lock scheme */
1612         if (this->options & ONENAND_HAS_CONT_LOCK) {
1613                 /* Set start block address */
1614                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1615                 /* Set end block address */
1616                 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1617                 /* Write lock command */
1618                 this->command(mtd, cmd, 0, 0);
1619
1620                 /* There's no return value */
1621                 this->wait(mtd, FL_LOCKING);
1622
1623                 /* Sanity check */
1624                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1625                     & ONENAND_CTRL_ONGO)
1626                         continue;
1627
1628                 /* Check lock status */
1629                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1630                 if (!(status & wp_status_mask))
1631                         printk(KERN_ERR "wp status = 0x%x\n", status);
1632
1633                 return 0;
1634         }
1635
1636         /* Block lock scheme */
1637         for (block = start; block < start + end; block++) {
1638                 /* Set block address */
1639                 value = onenand_block_address(this, block);
1640                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1641                 /* Select DataRAM for DDP */
1642                 value = onenand_bufferram_address(this, block);
1643                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1644                 /* Set start block address */
1645                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1646                 /* Write lock command */
1647                 this->command(mtd, cmd, 0, 0);
1648
1649                 /* There's no return value */
1650                 this->wait(mtd, FL_LOCKING);
1651
1652                 /* Sanity check */
1653                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1654                     & ONENAND_CTRL_ONGO)
1655                         continue;
1656
1657                 /* Check lock status */
1658                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1659                 if (!(status & wp_status_mask))
1660                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1661         }
1662
1663         return 0;
1664 }
1665
1666 /**
1667  * onenand_lock - [MTD Interface] Lock block(s)
1668  * @param mtd           MTD device structure
1669  * @param ofs           offset relative to mtd start
1670  * @param len           number of bytes to unlock
1671  *
1672  * Lock one or more blocks
1673  */
1674 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1675 {
1676         return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1677 }
1678
1679 /**
1680  * onenand_unlock - [MTD Interface] Unlock block(s)
1681  * @param mtd           MTD device structure
1682  * @param ofs           offset relative to mtd start
1683  * @param len           number of bytes to unlock
1684  *
1685  * Unlock one or more blocks
1686  */
1687 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1688 {
1689         return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1690 }
1691
1692 /**
1693  * onenand_check_lock_status - [OneNAND Interface] Check lock status
1694  * @param this          onenand chip data structure
1695  *
1696  * Check lock status
1697  */
1698 static void onenand_check_lock_status(struct onenand_chip *this)
1699 {
1700         unsigned int value, block, status;
1701         unsigned int end;
1702
1703         end = this->chipsize >> this->erase_shift;
1704         for (block = 0; block < end; block++) {
1705                 /* Set block address */
1706                 value = onenand_block_address(this, block);
1707                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1708                 /* Select DataRAM for DDP */
1709                 value = onenand_bufferram_address(this, block);
1710                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1711                 /* Set start block address */
1712                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1713
1714                 /* Check lock status */
1715                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1716                 if (!(status & ONENAND_WP_US))
1717                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1718         }
1719 }
1720
1721 /**
1722  * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1723  * @param mtd           MTD device structure
1724  *
1725  * Unlock all blocks
1726  */
1727 static int onenand_unlock_all(struct mtd_info *mtd)
1728 {
1729         struct onenand_chip *this = mtd->priv;
1730
1731         if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1732                 /* Set start block address */
1733                 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1734                 /* Write unlock command */
1735                 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1736
1737                 /* There's no return value */
1738                 this->wait(mtd, FL_LOCKING);
1739
1740                 /* Sanity check */
1741                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1742                     & ONENAND_CTRL_ONGO)
1743                         continue;
1744
1745                 /* Workaround for all block unlock in DDP */
1746                 if (ONENAND_IS_DDP(this)) {
1747                         /* 1st block on another chip */
1748                         loff_t ofs = this->chipsize >> 1;
1749                         size_t len = mtd->erasesize;
1750
1751                         onenand_unlock(mtd, ofs, len);
1752                 }
1753
1754                 onenand_check_lock_status(this);
1755
1756                 return 0;
1757         }
1758
1759         onenand_unlock(mtd, 0x0, this->chipsize);
1760
1761         return 0;
1762 }
1763
1764 #ifdef CONFIG_MTD_ONENAND_OTP
1765
1766 /* Interal OTP operation */
1767 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1768                 size_t *retlen, u_char *buf);
1769
1770 /**
1771  * do_otp_read - [DEFAULT] Read OTP block area
1772  * @param mtd           MTD device structure
1773  * @param from          The offset to read
1774  * @param len           number of bytes to read
1775  * @param retlen        pointer to variable to store the number of readbytes
1776  * @param buf           the databuffer to put/get data
1777  *
1778  * Read OTP block area.
1779  */
1780 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1781                 size_t *retlen, u_char *buf)
1782 {
1783         struct onenand_chip *this = mtd->priv;
1784         int ret;
1785
1786         /* Enter OTP access mode */
1787         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1788         this->wait(mtd, FL_OTPING);
1789
1790         ret = mtd->read(mtd, from, len, retlen, buf);
1791
1792         /* Exit OTP access mode */
1793         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1794         this->wait(mtd, FL_RESETING);
1795
1796         return ret;
1797 }
1798
1799 /**
1800  * do_otp_write - [DEFAULT] Write OTP block area
1801  * @param mtd           MTD device structure
1802  * @param from          The offset to write
1803  * @param len           number of bytes to write
1804  * @param retlen        pointer to variable to store the number of write bytes
1805  * @param buf           the databuffer to put/get data
1806  *
1807  * Write OTP block area.
1808  */
1809 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1810                 size_t *retlen, u_char *buf)
1811 {
1812         struct onenand_chip *this = mtd->priv;
1813         unsigned char *pbuf = buf;
1814         int ret;
1815
1816         /* Force buffer page aligned */
1817         if (len < mtd->writesize) {
1818                 memcpy(this->page_buf, buf, len);
1819                 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1820                 pbuf = this->page_buf;
1821                 len = mtd->writesize;
1822         }
1823
1824         /* Enter OTP access mode */
1825         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1826         this->wait(mtd, FL_OTPING);
1827
1828         ret = mtd->write(mtd, from, len, retlen, pbuf);
1829
1830         /* Exit OTP access mode */
1831         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1832         this->wait(mtd, FL_RESETING);
1833
1834         return ret;
1835 }
1836
1837 /**
1838  * do_otp_lock - [DEFAULT] Lock OTP block area
1839  * @param mtd           MTD device structure
1840  * @param from          The offset to lock
1841  * @param len           number of bytes to lock
1842  * @param retlen        pointer to variable to store the number of lock bytes
1843  * @param buf           the databuffer to put/get data
1844  *
1845  * Lock OTP block area.
1846  */
1847 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1848                 size_t *retlen, u_char *buf)
1849 {
1850         struct onenand_chip *this = mtd->priv;
1851         int ret;
1852
1853         /* Enter OTP access mode */
1854         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1855         this->wait(mtd, FL_OTPING);
1856
1857         ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
1858
1859         /* Exit OTP access mode */
1860         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1861         this->wait(mtd, FL_RESETING);
1862
1863         return ret;
1864 }
1865
1866 /**
1867  * onenand_otp_walk - [DEFAULT] Handle OTP operation
1868  * @param mtd           MTD device structure
1869  * @param from          The offset to read/write
1870  * @param len           number of bytes to read/write
1871  * @param retlen        pointer to variable to store the number of read bytes
1872  * @param buf           the databuffer to put/get data
1873  * @param action        do given action
1874  * @param mode          specify user and factory
1875  *
1876  * Handle OTP operation.
1877  */
1878 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1879                         size_t *retlen, u_char *buf,
1880                         otp_op_t action, int mode)
1881 {
1882         struct onenand_chip *this = mtd->priv;
1883         int otp_pages;
1884         int density;
1885         int ret = 0;
1886
1887         *retlen = 0;
1888
1889         density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1890         if (density < ONENAND_DEVICE_DENSITY_512Mb)
1891                 otp_pages = 20;
1892         else
1893                 otp_pages = 10;
1894
1895         if (mode == MTD_OTP_FACTORY) {
1896                 from += mtd->writesize * otp_pages;
1897                 otp_pages = 64 - otp_pages;
1898         }
1899
1900         /* Check User/Factory boundary */
1901         if (((mtd->writesize * otp_pages) - (from + len)) < 0)
1902                 return 0;
1903
1904         while (len > 0 && otp_pages > 0) {
1905                 if (!action) {  /* OTP Info functions */
1906                         struct otp_info *otpinfo;
1907
1908                         len -= sizeof(struct otp_info);
1909                         if (len <= 0)
1910                                 return -ENOSPC;
1911
1912                         otpinfo = (struct otp_info *) buf;
1913                         otpinfo->start = from;
1914                         otpinfo->length = mtd->writesize;
1915                         otpinfo->locked = 0;
1916
1917                         from += mtd->writesize;
1918                         buf += sizeof(struct otp_info);
1919                         *retlen += sizeof(struct otp_info);
1920                 } else {
1921                         size_t tmp_retlen;
1922                         int size = len;
1923
1924                         ret = action(mtd, from, len, &tmp_retlen, buf);
1925
1926                         buf += size;
1927                         len -= size;
1928                         *retlen += size;
1929
1930                         if (ret < 0)
1931                                 return ret;
1932                 }
1933                 otp_pages--;
1934         }
1935
1936         return 0;
1937 }
1938
1939 /**
1940  * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1941  * @param mtd           MTD device structure
1942  * @param buf           the databuffer to put/get data
1943  * @param len           number of bytes to read
1944  *
1945  * Read factory OTP info.
1946  */
1947 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1948                         struct otp_info *buf, size_t len)
1949 {
1950         size_t retlen;
1951         int ret;
1952
1953         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1954
1955         return ret ? : retlen;
1956 }
1957
1958 /**
1959  * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1960  * @param mtd           MTD device structure
1961  * @param from          The offset to read
1962  * @param len           number of bytes to read
1963  * @param retlen        pointer to variable to store the number of read bytes
1964  * @param buf           the databuffer to put/get data
1965  *
1966  * Read factory OTP area.
1967  */
1968 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1969                         size_t len, size_t *retlen, u_char *buf)
1970 {
1971         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
1972 }
1973
1974 /**
1975  * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
1976  * @param mtd           MTD device structure
1977  * @param buf           the databuffer to put/get data
1978  * @param len           number of bytes to read
1979  *
1980  * Read user OTP info.
1981  */
1982 static int onenand_get_user_prot_info(struct mtd_info *mtd,
1983                         struct otp_info *buf, size_t len)
1984 {
1985         size_t retlen;
1986         int ret;
1987
1988         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
1989
1990         return ret ? : retlen;
1991 }
1992
1993 /**
1994  * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
1995  * @param mtd           MTD device structure
1996  * @param from          The offset to read
1997  * @param len           number of bytes to read
1998  * @param retlen        pointer to variable to store the number of read bytes
1999  * @param buf           the databuffer to put/get data
2000  *
2001  * Read user OTP area.
2002  */
2003 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2004                         size_t len, size_t *retlen, u_char *buf)
2005 {
2006         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2007 }
2008
2009 /**
2010  * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2011  * @param mtd           MTD device structure
2012  * @param from          The offset to write
2013  * @param len           number of bytes to write
2014  * @param retlen        pointer to variable to store the number of write bytes
2015  * @param buf           the databuffer to put/get data
2016  *
2017  * Write user OTP area.
2018  */
2019 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2020                         size_t len, size_t *retlen, u_char *buf)
2021 {
2022         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2023 }
2024
2025 /**
2026  * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2027  * @param mtd           MTD device structure
2028  * @param from          The offset to lock
2029  * @param len           number of bytes to unlock
2030  *
2031  * Write lock mark on spare area in page 0 in OTP block
2032  */
2033 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2034                         size_t len)
2035 {
2036         unsigned char oob_buf[64];
2037         size_t retlen;
2038         int ret;
2039
2040         memset(oob_buf, 0xff, mtd->oobsize);
2041         /*
2042          * Note: OTP lock operation
2043          *       OTP block : 0xXXFC
2044          *       1st block : 0xXXF3 (If chip support)
2045          *       Both      : 0xXXF0 (If chip support)
2046          */
2047         oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2048
2049         /*
2050          * Write lock mark to 8th word of sector0 of page0 of the spare0.
2051          * We write 16 bytes spare area instead of 2 bytes.
2052          */
2053         from = 0;
2054         len = 16;
2055
2056         ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2057
2058         return ret ? : retlen;
2059 }
2060 #endif  /* CONFIG_MTD_ONENAND_OTP */
2061
2062 /**
2063  * onenand_check_features - Check and set OneNAND features
2064  * @param mtd           MTD data structure
2065  *
2066  * Check and set OneNAND features
2067  * - lock scheme
2068  */
2069 static void onenand_check_features(struct mtd_info *mtd)
2070 {
2071         struct onenand_chip *this = mtd->priv;
2072         unsigned int density, process;
2073
2074         /* Lock scheme depends on density and process */
2075         density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2076         process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2077
2078         /* Lock scheme */
2079         if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
2080                 /* A-Die has all block unlock */
2081                 if (process) {
2082                         printk(KERN_DEBUG "Chip support all block unlock\n");
2083                         this->options |= ONENAND_HAS_UNLOCK_ALL;
2084                 }
2085         } else {
2086                 /* Some OneNAND has continues lock scheme */
2087                 if (!process) {
2088                         printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
2089                         this->options |= ONENAND_HAS_CONT_LOCK;
2090                 }
2091         }
2092 }
2093
2094 /**
2095  * onenand_print_device_info - Print device ID
2096  * @param device        device ID
2097  *
2098  * Print device ID
2099  */
2100 static void onenand_print_device_info(int device, int version)
2101 {
2102         int vcc, demuxed, ddp, density;
2103
2104         vcc = device & ONENAND_DEVICE_VCC_MASK;
2105         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2106         ddp = device & ONENAND_DEVICE_IS_DDP;
2107         density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2108         printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2109                 demuxed ? "" : "Muxed ",
2110                 ddp ? "(DDP)" : "",
2111                 (16 << density),
2112                 vcc ? "2.65/3.3" : "1.8",
2113                 device);
2114         printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
2115 }
2116
2117 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2118         {ONENAND_MFR_SAMSUNG, "Samsung"},
2119 };
2120
2121 /**
2122  * onenand_check_maf - Check manufacturer ID
2123  * @param manuf         manufacturer ID
2124  *
2125  * Check manufacturer ID
2126  */
2127 static int onenand_check_maf(int manuf)
2128 {
2129         int size = ARRAY_SIZE(onenand_manuf_ids);
2130         char *name;
2131         int i;
2132
2133         for (i = 0; i < size; i++)
2134                 if (manuf == onenand_manuf_ids[i].id)
2135                         break;
2136
2137         if (i < size)
2138                 name = onenand_manuf_ids[i].name;
2139         else
2140                 name = "Unknown";
2141
2142         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2143
2144         return (i == size);
2145 }
2146
2147 /**
2148  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2149  * @param mtd           MTD device structure
2150  *
2151  * OneNAND detection method:
2152  *   Compare the the values from command with ones from register
2153  */
2154 static int onenand_probe(struct mtd_info *mtd)
2155 {
2156         struct onenand_chip *this = mtd->priv;
2157         int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2158         int density;
2159         int syscfg;
2160
2161         /* Save system configuration 1 */
2162         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2163         /* Clear Sync. Burst Read mode to read BootRAM */
2164         this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2165
2166         /* Send the command for reading device ID from BootRAM */
2167         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2168
2169         /* Read manufacturer and device IDs from BootRAM */
2170         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2171         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2172
2173         /* Reset OneNAND to read default register values */
2174         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2175         /* Wait reset */
2176         this->wait(mtd, FL_RESETING);
2177
2178         /* Restore system configuration 1 */
2179         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2180
2181         /* Check manufacturer ID */
2182         if (onenand_check_maf(bram_maf_id))
2183                 return -ENXIO;
2184
2185         /* Read manufacturer and device IDs from Register */
2186         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2187         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2188         ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2189
2190         /* Check OneNAND device */
2191         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2192                 return -ENXIO;
2193
2194         /* Flash device information */
2195         onenand_print_device_info(dev_id, ver_id);
2196         this->device_id = dev_id;
2197         this->version_id = ver_id;
2198
2199         density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2200         this->chipsize = (16 << density) << 20;
2201         /* Set density mask. it is used for DDP */
2202         if (ONENAND_IS_DDP(this))
2203                 this->density_mask = (1 << (density + 6));
2204         else
2205                 this->density_mask = 0;
2206
2207         /* OneNAND page size & block size */
2208         /* The data buffer size is equal to page size */
2209         mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2210         mtd->oobsize = mtd->writesize >> 5;
2211         /* Pages per a block are always 64 in OneNAND */
2212         mtd->erasesize = mtd->writesize << 6;
2213
2214         this->erase_shift = ffs(mtd->erasesize) - 1;
2215         this->page_shift = ffs(mtd->writesize) - 1;
2216         this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2217
2218         /* REVIST: Multichip handling */
2219
2220         mtd->size = this->chipsize;
2221
2222         /* Check OneNAND features */
2223         onenand_check_features(mtd);
2224
2225         return 0;
2226 }
2227
2228 /**
2229  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2230  * @param mtd           MTD device structure
2231  */
2232 static int onenand_suspend(struct mtd_info *mtd)
2233 {
2234         return onenand_get_device(mtd, FL_PM_SUSPENDED);
2235 }
2236
2237 /**
2238  * onenand_resume - [MTD Interface] Resume the OneNAND flash
2239  * @param mtd           MTD device structure
2240  */
2241 static void onenand_resume(struct mtd_info *mtd)
2242 {
2243         struct onenand_chip *this = mtd->priv;
2244
2245         if (this->state == FL_PM_SUSPENDED)
2246                 onenand_release_device(mtd);
2247         else
2248                 printk(KERN_ERR "resume() called for the chip which is not"
2249                                 "in suspended state\n");
2250 }
2251
2252 /**
2253  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2254  * @param mtd           MTD device structure
2255  * @param maxchips      Number of chips to scan for
2256  *
2257  * This fills out all the not initialized function pointers
2258  * with the defaults.
2259  * The flash ID is read and the mtd/chip structures are
2260  * filled with the appropriate values.
2261  */
2262 int onenand_scan(struct mtd_info *mtd, int maxchips)
2263 {
2264         int i;
2265         struct onenand_chip *this = mtd->priv;
2266
2267         if (!this->read_word)
2268                 this->read_word = onenand_readw;
2269         if (!this->write_word)
2270                 this->write_word = onenand_writew;
2271
2272         if (!this->command)
2273                 this->command = onenand_command;
2274         if (!this->wait)
2275                 onenand_setup_wait(mtd);
2276
2277         if (!this->read_bufferram)
2278                 this->read_bufferram = onenand_read_bufferram;
2279         if (!this->write_bufferram)
2280                 this->write_bufferram = onenand_write_bufferram;
2281
2282         if (!this->block_markbad)
2283                 this->block_markbad = onenand_default_block_markbad;
2284         if (!this->scan_bbt)
2285                 this->scan_bbt = onenand_default_bbt;
2286
2287         if (onenand_probe(mtd))
2288                 return -ENXIO;
2289
2290         /* Set Sync. Burst Read after probing */
2291         if (this->mmcontrol) {
2292                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2293                 this->read_bufferram = onenand_sync_read_bufferram;
2294         }
2295
2296         /* Allocate buffers, if necessary */
2297         if (!this->page_buf) {
2298                 size_t len;
2299                 len = mtd->writesize + mtd->oobsize;
2300                 this->page_buf = kmalloc(len, GFP_KERNEL);
2301                 if (!this->page_buf) {
2302                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2303                         return -ENOMEM;
2304                 }
2305                 this->options |= ONENAND_PAGEBUF_ALLOC;
2306         }
2307
2308         this->state = FL_READY;
2309         init_waitqueue_head(&this->wq);
2310         spin_lock_init(&this->chip_lock);
2311
2312         /*
2313          * Allow subpage writes up to oobsize.
2314          */
2315         switch (mtd->oobsize) {
2316         case 64:
2317                 this->ecclayout = &onenand_oob_64;
2318                 mtd->subpage_sft = 2;
2319                 break;
2320
2321         case 32:
2322                 this->ecclayout = &onenand_oob_32;
2323                 mtd->subpage_sft = 1;
2324                 break;
2325
2326         default:
2327                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2328                         mtd->oobsize);
2329                 mtd->subpage_sft = 0;
2330                 /* To prevent kernel oops */
2331                 this->ecclayout = &onenand_oob_32;
2332                 break;
2333         }
2334
2335         this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2336
2337         /*
2338          * The number of bytes available for a client to place data into
2339          * the out of band area
2340          */
2341         this->ecclayout->oobavail = 0;
2342         for (i = 0; this->ecclayout->oobfree[i].length; i++)
2343                 this->ecclayout->oobavail +=
2344                         this->ecclayout->oobfree[i].length;
2345
2346         mtd->ecclayout = this->ecclayout;
2347
2348         /* Fill in remaining MTD driver data */
2349         mtd->type = MTD_NANDFLASH;
2350         mtd->flags = MTD_CAP_NANDFLASH;
2351         mtd->ecctype = MTD_ECC_SW;
2352         mtd->erase = onenand_erase;
2353         mtd->point = NULL;
2354         mtd->unpoint = NULL;
2355         mtd->read = onenand_read;
2356         mtd->write = onenand_write;
2357         mtd->read_oob = onenand_read_oob;
2358         mtd->write_oob = onenand_write_oob;
2359 #ifdef CONFIG_MTD_ONENAND_OTP
2360         mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2361         mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2362         mtd->get_user_prot_info = onenand_get_user_prot_info;
2363         mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2364         mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2365         mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2366 #endif
2367         mtd->sync = onenand_sync;
2368         mtd->lock = onenand_lock;
2369         mtd->unlock = onenand_unlock;
2370         mtd->suspend = onenand_suspend;
2371         mtd->resume = onenand_resume;
2372         mtd->block_isbad = onenand_block_isbad;
2373         mtd->block_markbad = onenand_block_markbad;
2374         mtd->owner = THIS_MODULE;
2375
2376         /* Unlock whole block */
2377         onenand_unlock_all(mtd);
2378
2379         return this->scan_bbt(mtd);
2380 }
2381
2382 /**
2383  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2384  * @param mtd           MTD device structure
2385  */
2386 void onenand_release(struct mtd_info *mtd)
2387 {
2388         struct onenand_chip *this = mtd->priv;
2389
2390 #ifdef CONFIG_MTD_PARTITIONS
2391         /* Deregister partitions */
2392         del_mtd_partitions (mtd);
2393 #endif
2394         /* Deregister the device */
2395         del_mtd_device (mtd);
2396
2397         /* Free bad block table memory, if allocated */
2398         if (this->bbm) {
2399                 struct bbm_info *bbm = this->bbm;
2400                 kfree(bbm->bbt);
2401                 kfree(this->bbm);
2402         }
2403         /* Buffer allocated by onenand_scan */
2404         if (this->options & ONENAND_PAGEBUF_ALLOC)
2405                 kfree(this->page_buf);
2406 }
2407
2408 EXPORT_SYMBOL_GPL(onenand_scan);
2409 EXPORT_SYMBOL_GPL(onenand_release);
2410
2411 MODULE_LICENSE("GPL");
2412 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2413 MODULE_DESCRIPTION("Generic OneNAND flash driver code");