3a3fe1d8fcdd15052eba6bcb060bec00b5b7a2d7
[safe/jmp/linux-2.6] / drivers / mtd / onenand / onenand_base.c
1 /*
2  *  linux/drivers/mtd/onenand/onenand_base.c
3  *
4  *  Copyright (C) 2005 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/jiffies.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/onenand.h>
19 #include <linux/mtd/partitions.h>
20
21 #include <asm/io.h>
22
23 /**
24  * onenand_oob_64 - oob info for large (2KB) page
25  */
26 static struct nand_oobinfo onenand_oob_64 = {
27         .useecc         = MTD_NANDECC_AUTOPLACE,
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_oobinfo onenand_oob_32 = {
45         .useecc         = MTD_NANDECC_AUTOPLACE,
46         .eccbytes       = 10,
47         .eccpos         = {
48                 8, 9, 10, 11, 12,
49                 24, 25, 26, 27, 28,
50                 },
51         .oobfree        = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
52 };
53
54 static const unsigned char ffchars[] = {
55         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
57         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
59         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
61         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
63 };
64
65 /**
66  * onenand_readw - [OneNAND Interface] Read OneNAND register
67  * @param addr          address to read
68  *
69  * Read OneNAND register
70  */
71 static unsigned short onenand_readw(void __iomem *addr)
72 {
73         return readw(addr);
74 }
75
76 /**
77  * onenand_writew - [OneNAND Interface] Write OneNAND register with value
78  * @param value         value to write
79  * @param addr          address to write
80  *
81  * Write OneNAND register with value
82  */
83 static void onenand_writew(unsigned short value, void __iomem *addr)
84 {
85         writew(value, addr);
86 }
87
88 /**
89  * onenand_block_address - [DEFAULT] Get block address
90  * @param this          onenand chip data structure
91  * @param block         the block
92  * @return              translated block address if DDP, otherwise same
93  *
94  * Setup Start Address 1 Register (F100h)
95  */
96 static int onenand_block_address(struct onenand_chip *this, int block)
97 {
98         if (this->device_id & ONENAND_DEVICE_IS_DDP) {
99                 /* Device Flash Core select, NAND Flash Block Address */
100                 int dfs = 0;
101
102                 if (block & this->density_mask)
103                         dfs = 1;
104
105                 return (dfs << ONENAND_DDP_SHIFT) |
106                         (block & (this->density_mask - 1));
107         }
108
109         return block;
110 }
111
112 /**
113  * onenand_bufferram_address - [DEFAULT] Get bufferram address
114  * @param this          onenand chip data structure
115  * @param block         the block
116  * @return              set DBS value if DDP, otherwise 0
117  *
118  * Setup Start Address 2 Register (F101h) for DDP
119  */
120 static int onenand_bufferram_address(struct onenand_chip *this, int block)
121 {
122         if (this->device_id & ONENAND_DEVICE_IS_DDP) {
123                 /* Device BufferRAM Select */
124                 int dbs = 0;
125
126                 if (block & this->density_mask)
127                         dbs = 1;
128
129                 return (dbs << ONENAND_DDP_SHIFT);
130         }
131
132         return 0;
133 }
134
135 /**
136  * onenand_page_address - [DEFAULT] Get page address
137  * @param page          the page address
138  * @param sector        the sector address
139  * @return              combined page and sector address
140  *
141  * Setup Start Address 8 Register (F107h)
142  */
143 static int onenand_page_address(int page, int sector)
144 {
145         /* Flash Page Address, Flash Sector Address */
146         int fpa, fsa;
147
148         fpa = page & ONENAND_FPA_MASK;
149         fsa = sector & ONENAND_FSA_MASK;
150
151         return ((fpa << ONENAND_FPA_SHIFT) | fsa);
152 }
153
154 /**
155  * onenand_buffer_address - [DEFAULT] Get buffer address
156  * @param dataram1      DataRAM index
157  * @param sectors       the sector address
158  * @param count         the number of sectors
159  * @return              the start buffer value
160  *
161  * Setup Start Buffer Register (F200h)
162  */
163 static int onenand_buffer_address(int dataram1, int sectors, int count)
164 {
165         int bsa, bsc;
166
167         /* BufferRAM Sector Address */
168         bsa = sectors & ONENAND_BSA_MASK;
169
170         if (dataram1)
171                 bsa |= ONENAND_BSA_DATARAM1;    /* DataRAM1 */
172         else
173                 bsa |= ONENAND_BSA_DATARAM0;    /* DataRAM0 */
174
175         /* BufferRAM Sector Count */
176         bsc = count & ONENAND_BSC_MASK;
177
178         return ((bsa << ONENAND_BSA_SHIFT) | bsc);
179 }
180
181 /**
182  * onenand_command - [DEFAULT] Send command to OneNAND device
183  * @param mtd           MTD device structure
184  * @param cmd           the command to be sent
185  * @param addr          offset to read from or write to
186  * @param len           number of bytes to read or write
187  *
188  * Send command to OneNAND device. This function is used for middle/large page
189  * devices (1KB/2KB Bytes per page)
190  */
191 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
192 {
193         struct onenand_chip *this = mtd->priv;
194         int value, readcmd = 0, block_cmd = 0;
195         int block, page;
196         /* Now we use page size operation */
197         int sectors = 4, count = 4;
198
199         /* Address translation */
200         switch (cmd) {
201         case ONENAND_CMD_UNLOCK:
202         case ONENAND_CMD_LOCK:
203         case ONENAND_CMD_LOCK_TIGHT:
204                 block = -1;
205                 page = -1;
206                 break;
207
208         case ONENAND_CMD_ERASE:
209         case ONENAND_CMD_BUFFERRAM:
210         case ONENAND_CMD_OTP_ACCESS:
211                 block_cmd = 1;
212                 block = (int) (addr >> this->erase_shift);
213                 page = -1;
214                 break;
215
216         default:
217                 block = (int) (addr >> this->erase_shift);
218                 page = (int) (addr >> this->page_shift);
219                 page &= this->page_mask;
220                 break;
221         }
222
223         /* NOTE: The setting order of the registers is very important! */
224         if (cmd == ONENAND_CMD_BUFFERRAM) {
225                 /* Select DataRAM for DDP */
226                 value = onenand_bufferram_address(this, block);
227                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
228
229                 /* Switch to the next data buffer */
230                 ONENAND_SET_NEXT_BUFFERRAM(this);
231
232                 return 0;
233         }
234
235         if (block != -1) {
236                 /* Write 'DFS, FBA' of Flash */
237                 value = onenand_block_address(this, block);
238                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
239
240                 if (block_cmd) {
241                         /* Select DataRAM for DDP */
242                         value = onenand_bufferram_address(this, block);
243                         this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
244                 }
245         }
246
247         if (page != -1) {
248                 int dataram;
249
250                 switch (cmd) {
251                 case ONENAND_CMD_READ:
252                 case ONENAND_CMD_READOOB:
253                         dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
254                         readcmd = 1;
255                         break;
256
257                 default:
258                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
259                         break;
260                 }
261
262                 /* Write 'FPA, FSA' of Flash */
263                 value = onenand_page_address(page, sectors);
264                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
265
266                 /* Write 'BSA, BSC' of DataRAM */
267                 value = onenand_buffer_address(dataram, sectors, count);
268                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
269
270                 if (readcmd) {
271                         /* Select DataRAM for DDP */
272                         value = onenand_bufferram_address(this, block);
273                         this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
274                 }
275         }
276
277         /* Interrupt clear */
278         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
279
280         /* Write command */
281         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
282
283         return 0;
284 }
285
286 /**
287  * onenand_wait - [DEFAULT] wait until the command is done
288  * @param mtd           MTD device structure
289  * @param state         state to select the max. timeout value
290  *
291  * Wait for command done. This applies to all OneNAND command
292  * Read can take up to 30us, erase up to 2ms and program up to 350us
293  * according to general OneNAND specs
294  */
295 static int onenand_wait(struct mtd_info *mtd, int state)
296 {
297         struct onenand_chip * this = mtd->priv;
298         unsigned long timeout;
299         unsigned int flags = ONENAND_INT_MASTER;
300         unsigned int interrupt = 0;
301         unsigned int ctrl, ecc;
302
303         /* The 20 msec is enough */
304         timeout = jiffies + msecs_to_jiffies(20);
305         while (time_before(jiffies, timeout)) {
306                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
307
308                 if (interrupt & flags)
309                         break;
310
311                 if (state != FL_READING)
312                         cond_resched();
313                 touch_softlockup_watchdog();
314         }
315         /* To get correct interrupt status in timeout case */
316         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
317
318         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
319
320         if (ctrl & ONENAND_CTRL_ERROR) {
321                 /* It maybe occur at initial bad block */
322                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
323                 /* Clear other interrupt bits for preventing ECC error */
324                 interrupt &= ONENAND_INT_MASTER;
325         }
326
327         if (ctrl & ONENAND_CTRL_LOCK) {
328                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
329                 return -EACCES;
330         }
331
332         if (interrupt & ONENAND_INT_READ) {
333                 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
334                 if (ecc & ONENAND_ECC_2BIT_ALL) {
335                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
336                         return -EBADMSG;
337                 }
338         }
339
340         return 0;
341 }
342
343 /**
344  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
345  * @param mtd           MTD data structure
346  * @param area          BufferRAM area
347  * @return              offset given area
348  *
349  * Return BufferRAM offset given area
350  */
351 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
352 {
353         struct onenand_chip *this = mtd->priv;
354
355         if (ONENAND_CURRENT_BUFFERRAM(this)) {
356                 if (area == ONENAND_DATARAM)
357                         return mtd->writesize;
358                 if (area == ONENAND_SPARERAM)
359                         return mtd->oobsize;
360         }
361
362         return 0;
363 }
364
365 /**
366  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
367  * @param mtd           MTD data structure
368  * @param area          BufferRAM area
369  * @param buffer        the databuffer to put/get data
370  * @param offset        offset to read from or write to
371  * @param count         number of bytes to read/write
372  *
373  * Read the BufferRAM area
374  */
375 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
376                 unsigned char *buffer, int offset, size_t count)
377 {
378         struct onenand_chip *this = mtd->priv;
379         void __iomem *bufferram;
380
381         bufferram = this->base + area;
382
383         bufferram += onenand_bufferram_offset(mtd, area);
384
385         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
386                 unsigned short word;
387
388                 /* Align with word(16-bit) size */
389                 count--;
390
391                 /* Read word and save byte */
392                 word = this->read_word(bufferram + offset + count);
393                 buffer[count] = (word & 0xff);
394         }
395
396         memcpy(buffer, bufferram + offset, count);
397
398         return 0;
399 }
400
401 /**
402  * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
403  * @param mtd           MTD data structure
404  * @param area          BufferRAM area
405  * @param buffer        the databuffer to put/get data
406  * @param offset        offset to read from or write to
407  * @param count         number of bytes to read/write
408  *
409  * Read the BufferRAM area with Sync. Burst Mode
410  */
411 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
412                 unsigned char *buffer, int offset, size_t count)
413 {
414         struct onenand_chip *this = mtd->priv;
415         void __iomem *bufferram;
416
417         bufferram = this->base + area;
418
419         bufferram += onenand_bufferram_offset(mtd, area);
420
421         this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
422
423         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
424                 unsigned short word;
425
426                 /* Align with word(16-bit) size */
427                 count--;
428
429                 /* Read word and save byte */
430                 word = this->read_word(bufferram + offset + count);
431                 buffer[count] = (word & 0xff);
432         }
433
434         memcpy(buffer, bufferram + offset, count);
435
436         this->mmcontrol(mtd, 0);
437
438         return 0;
439 }
440
441 /**
442  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
443  * @param mtd           MTD data structure
444  * @param area          BufferRAM area
445  * @param buffer        the databuffer to put/get data
446  * @param offset        offset to read from or write to
447  * @param count         number of bytes to read/write
448  *
449  * Write the BufferRAM area
450  */
451 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
452                 const unsigned char *buffer, int offset, size_t count)
453 {
454         struct onenand_chip *this = mtd->priv;
455         void __iomem *bufferram;
456
457         bufferram = this->base + area;
458
459         bufferram += onenand_bufferram_offset(mtd, area);
460
461         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
462                 unsigned short word;
463                 int byte_offset;
464
465                 /* Align with word(16-bit) size */
466                 count--;
467
468                 /* Calculate byte access offset */
469                 byte_offset = offset + count;
470
471                 /* Read word and save byte */
472                 word = this->read_word(bufferram + byte_offset);
473                 word = (word & ~0xff) | buffer[count];
474                 this->write_word(word, bufferram + byte_offset);
475         }
476
477         memcpy(bufferram + offset, buffer, count);
478
479         return 0;
480 }
481
482 /**
483  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
484  * @param mtd           MTD data structure
485  * @param addr          address to check
486  * @return              1 if there are valid data, otherwise 0
487  *
488  * Check bufferram if there is data we required
489  */
490 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
491 {
492         struct onenand_chip *this = mtd->priv;
493         int block, page;
494         int i;
495
496         block = (int) (addr >> this->erase_shift);
497         page = (int) (addr >> this->page_shift);
498         page &= this->page_mask;
499
500         i = ONENAND_CURRENT_BUFFERRAM(this);
501
502         /* Is there valid data? */
503         if (this->bufferram[i].block == block &&
504             this->bufferram[i].page == page &&
505             this->bufferram[i].valid)
506                 return 1;
507
508         return 0;
509 }
510
511 /**
512  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
513  * @param mtd           MTD data structure
514  * @param addr          address to update
515  * @param valid         valid flag
516  *
517  * Update BufferRAM information
518  */
519 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
520                 int valid)
521 {
522         struct onenand_chip *this = mtd->priv;
523         int block, page;
524         int i;
525
526         block = (int) (addr >> this->erase_shift);
527         page = (int) (addr >> this->page_shift);
528         page &= this->page_mask;
529
530         /* Invalidate BufferRAM */
531         for (i = 0; i < MAX_BUFFERRAM; i++) {
532                 if (this->bufferram[i].block == block &&
533                     this->bufferram[i].page == page)
534                         this->bufferram[i].valid = 0;
535         }
536
537         /* Update BufferRAM */
538         i = ONENAND_CURRENT_BUFFERRAM(this);
539         this->bufferram[i].block = block;
540         this->bufferram[i].page = page;
541         this->bufferram[i].valid = valid;
542
543         return 0;
544 }
545
546 /**
547  * onenand_get_device - [GENERIC] Get chip for selected access
548  * @param mtd           MTD device structure
549  * @param new_state     the state which is requested
550  *
551  * Get the device and lock it for exclusive access
552  */
553 static int onenand_get_device(struct mtd_info *mtd, int new_state)
554 {
555         struct onenand_chip *this = mtd->priv;
556         DECLARE_WAITQUEUE(wait, current);
557
558         /*
559          * Grab the lock and see if the device is available
560          */
561         while (1) {
562                 spin_lock(&this->chip_lock);
563                 if (this->state == FL_READY) {
564                         this->state = new_state;
565                         spin_unlock(&this->chip_lock);
566                         break;
567                 }
568                 if (new_state == FL_PM_SUSPENDED) {
569                         spin_unlock(&this->chip_lock);
570                         return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
571                 }
572                 set_current_state(TASK_UNINTERRUPTIBLE);
573                 add_wait_queue(&this->wq, &wait);
574                 spin_unlock(&this->chip_lock);
575                 schedule();
576                 remove_wait_queue(&this->wq, &wait);
577         }
578
579         return 0;
580 }
581
582 /**
583  * onenand_release_device - [GENERIC] release chip
584  * @param mtd           MTD device structure
585  *
586  * Deselect, release chip lock and wake up anyone waiting on the device
587  */
588 static void onenand_release_device(struct mtd_info *mtd)
589 {
590         struct onenand_chip *this = mtd->priv;
591
592         /* Release the chip */
593         spin_lock(&this->chip_lock);
594         this->state = FL_READY;
595         wake_up(&this->wq);
596         spin_unlock(&this->chip_lock);
597 }
598
599 /**
600  * onenand_read_ecc - [MTD Interface] Read data with ECC
601  * @param mtd           MTD device structure
602  * @param from          offset to read from
603  * @param len           number of bytes to read
604  * @param retlen        pointer to variable to store the number of read bytes
605  * @param buf           the databuffer to put data
606  * @param oob_buf       filesystem supplied oob data buffer
607  * @param oobsel        oob selection structure
608  *
609  * OneNAND read with ECC
610  */
611 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
612         size_t *retlen, u_char *buf,
613         u_char *oob_buf, struct nand_oobinfo *oobsel)
614 {
615         struct onenand_chip *this = mtd->priv;
616         int read = 0, column;
617         int thislen;
618         int ret = 0;
619
620         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
621
622         /* Do not allow reads past end of device */
623         if ((from + len) > mtd->size) {
624                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: Attempt read beyond end of device\n");
625                 *retlen = 0;
626                 return -EINVAL;
627         }
628
629         /* Grab the lock and see if the device is available */
630         onenand_get_device(mtd, FL_READING);
631
632         /* TODO handling oob */
633
634         while (read < len) {
635                 thislen = min_t(int, mtd->writesize, len - read);
636
637                 column = from & (mtd->writesize - 1);
638                 if (column + thislen > mtd->writesize)
639                         thislen = mtd->writesize - column;
640
641                 if (!onenand_check_bufferram(mtd, from)) {
642                         this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
643
644                         ret = this->wait(mtd, FL_READING);
645                         /* First copy data and check return value for ECC handling */
646                         onenand_update_bufferram(mtd, from, 1);
647                 }
648
649                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
650
651                 read += thislen;
652
653                 if (read == len)
654                         break;
655
656                 if (ret) {
657                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: read failed = %d\n", ret);
658                         goto out;
659                 }
660
661                 from += thislen;
662                 buf += thislen;
663         }
664
665 out:
666         /* Deselect and wake up anyone waiting on the device */
667         onenand_release_device(mtd);
668
669         /*
670          * Return success, if no ECC failures, else -EBADMSG
671          * fs driver will take care of that, because
672          * retlen == desired len and result == -EBADMSG
673          */
674         *retlen = read;
675         return ret;
676 }
677
678 /**
679  * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
680  * @param mtd           MTD device structure
681  * @param from          offset to read from
682  * @param len           number of bytes to read
683  * @param retlen        pointer to variable to store the number of read bytes
684  * @param buf           the databuffer to put data
685  *
686  * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
687 */
688 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
689         size_t *retlen, u_char *buf)
690 {
691         return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
692 }
693
694 /**
695  * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
696  * @param mtd           MTD device structure
697  * @param from          offset to read from
698  * @param len           number of bytes to read
699  * @param retlen        pointer to variable to store the number of read bytes
700  * @param buf           the databuffer to put data
701  *
702  * OneNAND read out-of-band data from the spare area
703  */
704 static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
705         size_t *retlen, u_char *buf)
706 {
707         struct onenand_chip *this = mtd->priv;
708         int read = 0, thislen, column;
709         int ret = 0;
710
711         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
712
713         /* Initialize return length value */
714         *retlen = 0;
715
716         /* Do not allow reads past end of device */
717         if (unlikely((from + len) > mtd->size)) {
718                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
719                 return -EINVAL;
720         }
721
722         /* Grab the lock and see if the device is available */
723         onenand_get_device(mtd, FL_READING);
724
725         column = from & (mtd->oobsize - 1);
726
727         while (read < len) {
728                 thislen = mtd->oobsize - column;
729                 thislen = min_t(int, thislen, len);
730
731                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
732
733                 onenand_update_bufferram(mtd, from, 0);
734
735                 ret = this->wait(mtd, FL_READING);
736                 /* First copy data and check return value for ECC handling */
737
738                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
739
740                 read += thislen;
741
742                 if (read == len)
743                         break;
744
745                 if (ret) {
746                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
747                         goto out;
748                 }
749
750                 buf += thislen;
751
752                 /* Read more? */
753                 if (read < len) {
754                         /* Page size */
755                         from += mtd->writesize;
756                         column = 0;
757                 }
758         }
759
760 out:
761         /* Deselect and wake up anyone waiting on the device */
762         onenand_release_device(mtd);
763
764         *retlen = read;
765         return ret;
766 }
767
768 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
769 /**
770  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
771  * @param mtd           MTD device structure
772  * @param buf           the databuffer to verify
773  * @param to            offset to read from
774  * @param len           number of bytes to read and compare
775  *
776  */
777 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
778 {
779         struct onenand_chip *this = mtd->priv;
780         char *readp = this->page_buf;
781         int column = to & (mtd->oobsize - 1);
782         int status, i;
783
784         this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
785         onenand_update_bufferram(mtd, to, 0);
786         status = this->wait(mtd, FL_READING);
787         if (status)
788                 return status;
789
790         this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
791
792         for(i = 0; i < len; i++)
793                 if (buf[i] != 0xFF && buf[i] != readp[i])
794                         return -EBADMSG;
795
796         return 0;
797 }
798
799 /**
800  * onenand_verify_page - [GENERIC] verify the chip contents after a write
801  * @param mtd           MTD device structure
802  * @param buf           the databuffer to verify
803  *
804  * Check DataRAM area directly
805  */
806 static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
807 {
808         struct onenand_chip *this = mtd->priv;
809         void __iomem *dataram0, *dataram1;
810         int ret = 0;
811
812         this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
813
814         ret = this->wait(mtd, FL_READING);
815         if (ret)
816                 return ret;
817
818         onenand_update_bufferram(mtd, addr, 1);
819
820         /* Check, if the two dataram areas are same */
821         dataram0 = this->base + ONENAND_DATARAM;
822         dataram1 = dataram0 + mtd->writesize;
823
824         if (memcmp(dataram0, dataram1, mtd->writesize))
825                 return -EBADMSG;
826
827         return 0;
828 }
829 #else
830 #define onenand_verify_page(...)        (0)
831 #define onenand_verify_oob(...)         (0)
832 #endif
833
834 #define NOTALIGNED(x)   ((x & (mtd->writesize - 1)) != 0)
835
836 /**
837  * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
838  * @param mtd           MTD device structure
839  * @param to            offset to write to
840  * @param len           number of bytes to write
841  * @param retlen        pointer to variable to store the number of written bytes
842  * @param buf           the data to write
843  * @param eccbuf        filesystem supplied oob data buffer
844  * @param oobsel        oob selection structure
845  *
846  * OneNAND write with ECC
847  */
848 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
849         size_t *retlen, const u_char *buf,
850         u_char *eccbuf, struct nand_oobinfo *oobsel)
851 {
852         struct onenand_chip *this = mtd->priv;
853         int written = 0;
854         int ret = 0;
855
856         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
857
858         /* Initialize retlen, in case of early exit */
859         *retlen = 0;
860
861         /* Do not allow writes past end of device */
862         if (unlikely((to + len) > mtd->size)) {
863                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
864                 return -EINVAL;
865         }
866
867         /* Reject writes, which are not page aligned */
868         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
869                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
870                 return -EINVAL;
871         }
872
873         /* Grab the lock and see if the device is available */
874         onenand_get_device(mtd, FL_WRITING);
875
876         /* Loop until all data write */
877         while (written < len) {
878                 int thislen = min_t(int, mtd->writesize, len - written);
879
880                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
881
882                 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
883                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
884
885                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
886
887                 onenand_update_bufferram(mtd, to, 1);
888
889                 ret = this->wait(mtd, FL_WRITING);
890                 if (ret) {
891                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
892                         goto out;
893                 }
894
895                 written += thislen;
896
897                 /* Only check verify write turn on */
898                 ret = onenand_verify_page(mtd, (u_char *) buf, to);
899                 if (ret) {
900                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
901                         goto out;
902                 }
903
904                 if (written == len)
905                         break;
906
907                 to += thislen;
908                 buf += thislen;
909         }
910
911 out:
912         /* Deselect and wake up anyone waiting on the device */
913         onenand_release_device(mtd);
914
915         *retlen = written;
916
917         return ret;
918 }
919
920 /**
921  * onenand_write - [MTD Interface] compability function for onenand_write_ecc
922  * @param mtd           MTD device structure
923  * @param to            offset to write to
924  * @param len           number of bytes to write
925  * @param retlen        pointer to variable to store the number of written bytes
926  * @param buf           the data to write
927  *
928  * This function simply calls onenand_write_ecc
929  * with oob buffer and oobsel = NULL
930  */
931 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
932         size_t *retlen, const u_char *buf)
933 {
934         return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
935 }
936
937 /**
938  * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
939  * @param mtd           MTD device structure
940  * @param to            offset to write to
941  * @param len           number of bytes to write
942  * @param retlen        pointer to variable to store the number of written bytes
943  * @param buf           the data to write
944  *
945  * OneNAND write out-of-band
946  */
947 static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
948         size_t *retlen, const u_char *buf)
949 {
950         struct onenand_chip *this = mtd->priv;
951         int column, ret = 0;
952         int written = 0;
953
954         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
955
956         /* Initialize retlen, in case of early exit */
957         *retlen = 0;
958
959         /* Do not allow writes past end of device */
960         if (unlikely((to + len) > mtd->size)) {
961                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
962                 return -EINVAL;
963         }
964
965         /* Grab the lock and see if the device is available */
966         onenand_get_device(mtd, FL_WRITING);
967
968         /* Loop until all data write */
969         while (written < len) {
970                 int thislen = min_t(int, mtd->oobsize, len - written);
971
972                 column = to & (mtd->oobsize - 1);
973
974                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
975
976                 /* We send data to spare ram with oobsize
977                  * to prevent byte access */
978                 memset(this->page_buf, 0xff, mtd->oobsize);
979                 memcpy(this->page_buf + column, buf, thislen);
980                 this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
981
982                 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
983
984                 onenand_update_bufferram(mtd, to, 0);
985
986                 ret = this->wait(mtd, FL_WRITING);
987                 if (ret) {
988                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
989                         goto out;
990                 }
991
992                 ret = onenand_verify_oob(mtd, buf, to, thislen);
993                 if (ret) {
994                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
995                         goto out;
996                 }
997
998                 written += thislen;
999
1000                 if (written == len)
1001                         break;
1002
1003                 to += thislen;
1004                 buf += thislen;
1005         }
1006
1007 out:
1008         /* Deselect and wake up anyone waiting on the device */
1009         onenand_release_device(mtd);
1010
1011         *retlen = written;
1012
1013         return ret;
1014 }
1015
1016 /**
1017  * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1018  * @param mtd           MTD device structure
1019  * @param ofs           offset from device start
1020  * @param getchip       0, if the chip is already selected
1021  * @param allowbbt      1, if its allowed to access the bbt area
1022  *
1023  * Check, if the block is bad. Either by reading the bad block table or
1024  * calling of the scan function.
1025  */
1026 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1027 {
1028         struct onenand_chip *this = mtd->priv;
1029         struct bbm_info *bbm = this->bbm;
1030
1031         /* Return info from the table */
1032         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1033 }
1034
1035 /**
1036  * onenand_erase - [MTD Interface] erase block(s)
1037  * @param mtd           MTD device structure
1038  * @param instr         erase instruction
1039  *
1040  * Erase one ore more blocks
1041  */
1042 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1043 {
1044         struct onenand_chip *this = mtd->priv;
1045         unsigned int block_size;
1046         loff_t addr;
1047         int len;
1048         int ret = 0;
1049
1050         DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1051
1052         block_size = (1 << this->erase_shift);
1053
1054         /* Start address must align on block boundary */
1055         if (unlikely(instr->addr & (block_size - 1))) {
1056                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1057                 return -EINVAL;
1058         }
1059
1060         /* Length must align on block boundary */
1061         if (unlikely(instr->len & (block_size - 1))) {
1062                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1063                 return -EINVAL;
1064         }
1065
1066         /* Do not allow erase past end of device */
1067         if (unlikely((instr->len + instr->addr) > mtd->size)) {
1068                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1069                 return -EINVAL;
1070         }
1071
1072         instr->fail_addr = 0xffffffff;
1073
1074         /* Grab the lock and see if the device is available */
1075         onenand_get_device(mtd, FL_ERASING);
1076
1077         /* Loop throught the pages */
1078         len = instr->len;
1079         addr = instr->addr;
1080
1081         instr->state = MTD_ERASING;
1082
1083         while (len) {
1084
1085                 /* Check if we have a bad block, we do not erase bad blocks */
1086                 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1087                         printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1088                         instr->state = MTD_ERASE_FAILED;
1089                         goto erase_exit;
1090                 }
1091
1092                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1093
1094                 ret = this->wait(mtd, FL_ERASING);
1095                 /* Check, if it is write protected */
1096                 if (ret) {
1097                         if (ret == -EPERM)
1098                                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
1099                         else
1100                                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1101                         instr->state = MTD_ERASE_FAILED;
1102                         instr->fail_addr = addr;
1103                         goto erase_exit;
1104                 }
1105
1106                 len -= block_size;
1107                 addr += block_size;
1108         }
1109
1110         instr->state = MTD_ERASE_DONE;
1111
1112 erase_exit:
1113
1114         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1115         /* Do call back function */
1116         if (!ret)
1117                 mtd_erase_callback(instr);
1118
1119         /* Deselect and wake up anyone waiting on the device */
1120         onenand_release_device(mtd);
1121
1122         return ret;
1123 }
1124
1125 /**
1126  * onenand_sync - [MTD Interface] sync
1127  * @param mtd           MTD device structure
1128  *
1129  * Sync is actually a wait for chip ready function
1130  */
1131 static void onenand_sync(struct mtd_info *mtd)
1132 {
1133         DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1134
1135         /* Grab the lock and see if the device is available */
1136         onenand_get_device(mtd, FL_SYNCING);
1137
1138         /* Release it and go back */
1139         onenand_release_device(mtd);
1140 }
1141
1142
1143 /**
1144  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1145  * @param mtd           MTD device structure
1146  * @param ofs           offset relative to mtd start
1147  *
1148  * Check whether the block is bad
1149  */
1150 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1151 {
1152         /* Check for invalid offset */
1153         if (ofs > mtd->size)
1154                 return -EINVAL;
1155
1156         return onenand_block_checkbad(mtd, ofs, 1, 0);
1157 }
1158
1159 /**
1160  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1161  * @param mtd           MTD device structure
1162  * @param ofs           offset from device start
1163  *
1164  * This is the default implementation, which can be overridden by
1165  * a hardware specific driver.
1166  */
1167 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1168 {
1169         struct onenand_chip *this = mtd->priv;
1170         struct bbm_info *bbm = this->bbm;
1171         u_char buf[2] = {0, 0};
1172         size_t retlen;
1173         int block;
1174
1175         /* Get block number */
1176         block = ((int) ofs) >> bbm->bbt_erase_shift;
1177         if (bbm->bbt)
1178                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1179
1180         /* We write two bytes, so we dont have to mess with 16 bit access */
1181         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1182         return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
1183 }
1184
1185 /**
1186  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1187  * @param mtd           MTD device structure
1188  * @param ofs           offset relative to mtd start
1189  *
1190  * Mark the block as bad
1191  */
1192 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1193 {
1194         struct onenand_chip *this = mtd->priv;
1195         int ret;
1196
1197         ret = onenand_block_isbad(mtd, ofs);
1198         if (ret) {
1199                 /* If it was bad already, return success and do nothing */
1200                 if (ret > 0)
1201                         return 0;
1202                 return ret;
1203         }
1204
1205         return this->block_markbad(mtd, ofs);
1206 }
1207
1208 /**
1209  * onenand_unlock - [MTD Interface] Unlock block(s)
1210  * @param mtd           MTD device structure
1211  * @param ofs           offset relative to mtd start
1212  * @param len           number of bytes to unlock
1213  *
1214  * Unlock one or more blocks
1215  */
1216 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1217 {
1218         struct onenand_chip *this = mtd->priv;
1219         int start, end, block, value, status;
1220
1221         start = ofs >> this->erase_shift;
1222         end = len >> this->erase_shift;
1223
1224         /* Continuous lock scheme */
1225         if (this->options & ONENAND_CONT_LOCK) {
1226                 /* Set start block address */
1227                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1228                 /* Set end block address */
1229                 this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1230                 /* Write unlock command */
1231                 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1232
1233                 /* There's no return value */
1234                 this->wait(mtd, FL_UNLOCKING);
1235
1236                 /* Sanity check */
1237                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1238                     & ONENAND_CTRL_ONGO)
1239                         continue;
1240
1241                 /* Check lock status */
1242                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1243                 if (!(status & ONENAND_WP_US))
1244                         printk(KERN_ERR "wp status = 0x%x\n", status);
1245
1246                 return 0;
1247         }
1248
1249         /* Block lock scheme */
1250         for (block = start; block < end; block++) {
1251                 /* Set block address */
1252                 value = onenand_block_address(this, block);
1253                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1254                 /* Select DataRAM for DDP */
1255                 value = onenand_bufferram_address(this, block);
1256                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1257                 /* Set start block address */
1258                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1259                 /* Write unlock command */
1260                 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1261
1262                 /* There's no return value */
1263                 this->wait(mtd, FL_UNLOCKING);
1264
1265                 /* Sanity check */
1266                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1267                     & ONENAND_CTRL_ONGO)
1268                         continue;
1269
1270                 /* Check lock status */
1271                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1272                 if (!(status & ONENAND_WP_US))
1273                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1274         }
1275
1276         return 0;
1277 }
1278
1279 #ifdef CONFIG_MTD_ONENAND_OTP
1280
1281 /* Interal OTP operation */
1282 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1283                 size_t *retlen, u_char *buf);
1284
1285 /**
1286  * do_otp_read - [DEFAULT] Read OTP block area
1287  * @param mtd           MTD device structure
1288  * @param from          The offset to read
1289  * @param len           number of bytes to read
1290  * @param retlen        pointer to variable to store the number of readbytes
1291  * @param buf           the databuffer to put/get data
1292  *
1293  * Read OTP block area.
1294  */
1295 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1296                 size_t *retlen, u_char *buf)
1297 {
1298         struct onenand_chip *this = mtd->priv;
1299         int ret;
1300
1301         /* Enter OTP access mode */
1302         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1303         this->wait(mtd, FL_OTPING);
1304
1305         ret = mtd->read(mtd, from, len, retlen, buf);
1306
1307         /* Exit OTP access mode */
1308         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1309         this->wait(mtd, FL_RESETING);
1310
1311         return ret;
1312 }
1313
1314 /**
1315  * do_otp_write - [DEFAULT] Write OTP block area
1316  * @param mtd           MTD device structure
1317  * @param from          The offset to write
1318  * @param len           number of bytes to write
1319  * @param retlen        pointer to variable to store the number of write bytes
1320  * @param buf           the databuffer to put/get data
1321  *
1322  * Write OTP block area.
1323  */
1324 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1325                 size_t *retlen, u_char *buf)
1326 {
1327         struct onenand_chip *this = mtd->priv;
1328         unsigned char *pbuf = buf;
1329         int ret;
1330
1331         /* Force buffer page aligned */
1332         if (len < mtd->writesize) {
1333                 memcpy(this->page_buf, buf, len);
1334                 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1335                 pbuf = this->page_buf;
1336                 len = mtd->writesize;
1337         }
1338
1339         /* Enter OTP access mode */
1340         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1341         this->wait(mtd, FL_OTPING);
1342
1343         ret = mtd->write(mtd, from, len, retlen, pbuf);
1344
1345         /* Exit OTP access mode */
1346         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1347         this->wait(mtd, FL_RESETING);
1348
1349         return ret;
1350 }
1351
1352 /**
1353  * do_otp_lock - [DEFAULT] Lock OTP block area
1354  * @param mtd           MTD device structure
1355  * @param from          The offset to lock
1356  * @param len           number of bytes to lock
1357  * @param retlen        pointer to variable to store the number of lock bytes
1358  * @param buf           the databuffer to put/get data
1359  *
1360  * Lock OTP block area.
1361  */
1362 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1363                 size_t *retlen, u_char *buf)
1364 {
1365         struct onenand_chip *this = mtd->priv;
1366         int ret;
1367
1368         /* Enter OTP access mode */
1369         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1370         this->wait(mtd, FL_OTPING);
1371
1372         ret = mtd->write_oob(mtd, from, len, retlen, buf);
1373
1374         /* Exit OTP access mode */
1375         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1376         this->wait(mtd, FL_RESETING);
1377
1378         return ret;
1379 }
1380
1381 /**
1382  * onenand_otp_walk - [DEFAULT] Handle OTP operation
1383  * @param mtd           MTD device structure
1384  * @param from          The offset to read/write
1385  * @param len           number of bytes to read/write
1386  * @param retlen        pointer to variable to store the number of read bytes
1387  * @param buf           the databuffer to put/get data
1388  * @param action        do given action
1389  * @param mode          specify user and factory
1390  *
1391  * Handle OTP operation.
1392  */
1393 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1394                         size_t *retlen, u_char *buf,
1395                         otp_op_t action, int mode)
1396 {
1397         struct onenand_chip *this = mtd->priv;
1398         int otp_pages;
1399         int density;
1400         int ret = 0;
1401
1402         *retlen = 0;
1403
1404         density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1405         if (density < ONENAND_DEVICE_DENSITY_512Mb)
1406                 otp_pages = 20;
1407         else
1408                 otp_pages = 10;
1409
1410         if (mode == MTD_OTP_FACTORY) {
1411                 from += mtd->writesize * otp_pages;
1412                 otp_pages = 64 - otp_pages;
1413         }
1414
1415         /* Check User/Factory boundary */
1416         if (((mtd->writesize * otp_pages) - (from + len)) < 0)
1417                 return 0;
1418
1419         while (len > 0 && otp_pages > 0) {
1420                 if (!action) {  /* OTP Info functions */
1421                         struct otp_info *otpinfo;
1422
1423                         len -= sizeof(struct otp_info);
1424                         if (len <= 0)
1425                                 return -ENOSPC;
1426
1427                         otpinfo = (struct otp_info *) buf;
1428                         otpinfo->start = from;
1429                         otpinfo->length = mtd->writesize;
1430                         otpinfo->locked = 0;
1431
1432                         from += mtd->writesize;
1433                         buf += sizeof(struct otp_info);
1434                         *retlen += sizeof(struct otp_info);
1435                 } else {
1436                         size_t tmp_retlen;
1437                         int size = len;
1438
1439                         ret = action(mtd, from, len, &tmp_retlen, buf);
1440
1441                         buf += size;
1442                         len -= size;
1443                         *retlen += size;
1444
1445                         if (ret < 0)
1446                                 return ret;
1447                 }
1448                 otp_pages--;
1449         }
1450
1451         return 0;
1452 }
1453
1454 /**
1455  * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1456  * @param mtd           MTD device structure
1457  * @param buf           the databuffer to put/get data
1458  * @param len           number of bytes to read
1459  *
1460  * Read factory OTP info.
1461  */
1462 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1463                         struct otp_info *buf, size_t len)
1464 {
1465         size_t retlen;
1466         int ret;
1467
1468         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1469
1470         return ret ? : retlen;
1471 }
1472
1473 /**
1474  * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1475  * @param mtd           MTD device structure
1476  * @param from          The offset to read
1477  * @param len           number of bytes to read
1478  * @param retlen        pointer to variable to store the number of read bytes
1479  * @param buf           the databuffer to put/get data
1480  *
1481  * Read factory OTP area.
1482  */
1483 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1484                         size_t len, size_t *retlen, u_char *buf)
1485 {
1486         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
1487 }
1488
1489 /**
1490  * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
1491  * @param mtd           MTD device structure
1492  * @param buf           the databuffer to put/get data
1493  * @param len           number of bytes to read
1494  *
1495  * Read user OTP info.
1496  */
1497 static int onenand_get_user_prot_info(struct mtd_info *mtd,
1498                         struct otp_info *buf, size_t len)
1499 {
1500         size_t retlen;
1501         int ret;
1502
1503         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
1504
1505         return ret ? : retlen;
1506 }
1507
1508 /**
1509  * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
1510  * @param mtd           MTD device structure
1511  * @param from          The offset to read
1512  * @param len           number of bytes to read
1513  * @param retlen        pointer to variable to store the number of read bytes
1514  * @param buf           the databuffer to put/get data
1515  *
1516  * Read user OTP area.
1517  */
1518 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1519                         size_t len, size_t *retlen, u_char *buf)
1520 {
1521         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
1522 }
1523
1524 /**
1525  * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
1526  * @param mtd           MTD device structure
1527  * @param from          The offset to write
1528  * @param len           number of bytes to write
1529  * @param retlen        pointer to variable to store the number of write bytes
1530  * @param buf           the databuffer to put/get data
1531  *
1532  * Write user OTP area.
1533  */
1534 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1535                         size_t len, size_t *retlen, u_char *buf)
1536 {
1537         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
1538 }
1539
1540 /**
1541  * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
1542  * @param mtd           MTD device structure
1543  * @param from          The offset to lock
1544  * @param len           number of bytes to unlock
1545  *
1546  * Write lock mark on spare area in page 0 in OTP block
1547  */
1548 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1549                         size_t len)
1550 {
1551         unsigned char oob_buf[64];
1552         size_t retlen;
1553         int ret;
1554
1555         memset(oob_buf, 0xff, mtd->oobsize);
1556         /*
1557          * Note: OTP lock operation
1558          *       OTP block : 0xXXFC
1559          *       1st block : 0xXXF3 (If chip support)
1560          *       Both      : 0xXXF0 (If chip support)
1561          */
1562         oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
1563
1564         /*
1565          * Write lock mark to 8th word of sector0 of page0 of the spare0.
1566          * We write 16 bytes spare area instead of 2 bytes.
1567          */
1568         from = 0;
1569         len = 16;
1570
1571         ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
1572
1573         return ret ? : retlen;
1574 }
1575 #endif  /* CONFIG_MTD_ONENAND_OTP */
1576
1577 /**
1578  * onenand_print_device_info - Print device ID
1579  * @param device        device ID
1580  *
1581  * Print device ID
1582  */
1583 static void onenand_print_device_info(int device)
1584 {
1585         int vcc, demuxed, ddp, density;
1586
1587         vcc = device & ONENAND_DEVICE_VCC_MASK;
1588         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1589         ddp = device & ONENAND_DEVICE_IS_DDP;
1590         density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1591         printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1592                 demuxed ? "" : "Muxed ",
1593                 ddp ? "(DDP)" : "",
1594                 (16 << density),
1595                 vcc ? "2.65/3.3" : "1.8",
1596                 device);
1597 }
1598
1599 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1600         {ONENAND_MFR_SAMSUNG, "Samsung"},
1601 };
1602
1603 /**
1604  * onenand_check_maf - Check manufacturer ID
1605  * @param manuf         manufacturer ID
1606  *
1607  * Check manufacturer ID
1608  */
1609 static int onenand_check_maf(int manuf)
1610 {
1611         int size = ARRAY_SIZE(onenand_manuf_ids);
1612         char *name;
1613         int i;
1614
1615         for (i = 0; i < size; i++)
1616                 if (manuf == onenand_manuf_ids[i].id)
1617                         break;
1618
1619         if (i < size)
1620                 name = onenand_manuf_ids[i].name;
1621         else
1622                 name = "Unknown";
1623
1624         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1625
1626         return (i == size);
1627 }
1628
1629 /**
1630  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1631  * @param mtd           MTD device structure
1632  *
1633  * OneNAND detection method:
1634  *   Compare the the values from command with ones from register
1635  */
1636 static int onenand_probe(struct mtd_info *mtd)
1637 {
1638         struct onenand_chip *this = mtd->priv;
1639         int bram_maf_id, bram_dev_id, maf_id, dev_id;
1640         int version_id;
1641         int density;
1642
1643         /* Send the command for reading device ID from BootRAM */
1644         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1645
1646         /* Read manufacturer and device IDs from BootRAM */
1647         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1648         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1649
1650         /* Check manufacturer ID */
1651         if (onenand_check_maf(bram_maf_id))
1652                 return -ENXIO;
1653
1654         /* Reset OneNAND to read default register values */
1655         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1656
1657         /* Read manufacturer and device IDs from Register */
1658         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1659         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1660
1661         /* Check OneNAND device */
1662         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1663                 return -ENXIO;
1664
1665         /* Flash device information */
1666         onenand_print_device_info(dev_id);
1667         this->device_id = dev_id;
1668
1669         density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1670         this->chipsize = (16 << density) << 20;
1671         /* Set density mask. it is used for DDP */
1672         this->density_mask = (1 << (density + 6));
1673
1674         /* OneNAND page size & block size */
1675         /* The data buffer size is equal to page size */
1676         mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1677         mtd->oobsize = mtd->writesize >> 5;
1678         /* Pagers per block is always 64 in OneNAND */
1679         mtd->erasesize = mtd->writesize << 6;
1680
1681         this->erase_shift = ffs(mtd->erasesize) - 1;
1682         this->page_shift = ffs(mtd->writesize) - 1;
1683         this->ppb_shift = (this->erase_shift - this->page_shift);
1684         this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
1685
1686         /* REVIST: Multichip handling */
1687
1688         mtd->size = this->chipsize;
1689
1690         /* Version ID */
1691         version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1692         printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1693
1694         /* Lock scheme */
1695         if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1696             !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1697                 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1698                 this->options |= ONENAND_CONT_LOCK;
1699         }
1700
1701         return 0;
1702 }
1703
1704 /**
1705  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1706  * @param mtd           MTD device structure
1707  */
1708 static int onenand_suspend(struct mtd_info *mtd)
1709 {
1710         return onenand_get_device(mtd, FL_PM_SUSPENDED);
1711 }
1712
1713 /**
1714  * onenand_resume - [MTD Interface] Resume the OneNAND flash
1715  * @param mtd           MTD device structure
1716  */
1717 static void onenand_resume(struct mtd_info *mtd)
1718 {
1719         struct onenand_chip *this = mtd->priv;
1720
1721         if (this->state == FL_PM_SUSPENDED)
1722                 onenand_release_device(mtd);
1723         else
1724                 printk(KERN_ERR "resume() called for the chip which is not"
1725                                 "in suspended state\n");
1726 }
1727
1728 /**
1729  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1730  * @param mtd           MTD device structure
1731  * @param maxchips      Number of chips to scan for
1732  *
1733  * This fills out all the not initialized function pointers
1734  * with the defaults.
1735  * The flash ID is read and the mtd/chip structures are
1736  * filled with the appropriate values.
1737  */
1738 int onenand_scan(struct mtd_info *mtd, int maxchips)
1739 {
1740         struct onenand_chip *this = mtd->priv;
1741
1742         if (!this->read_word)
1743                 this->read_word = onenand_readw;
1744         if (!this->write_word)
1745                 this->write_word = onenand_writew;
1746
1747         if (!this->command)
1748                 this->command = onenand_command;
1749         if (!this->wait)
1750                 this->wait = onenand_wait;
1751
1752         if (!this->read_bufferram)
1753                 this->read_bufferram = onenand_read_bufferram;
1754         if (!this->write_bufferram)
1755                 this->write_bufferram = onenand_write_bufferram;
1756
1757         if (!this->block_markbad)
1758                 this->block_markbad = onenand_default_block_markbad;
1759         if (!this->scan_bbt)
1760                 this->scan_bbt = onenand_default_bbt;
1761
1762         if (onenand_probe(mtd))
1763                 return -ENXIO;
1764
1765         /* Set Sync. Burst Read after probing */
1766         if (this->mmcontrol) {
1767                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1768                 this->read_bufferram = onenand_sync_read_bufferram;
1769         }
1770
1771         /* Allocate buffers, if necessary */
1772         if (!this->page_buf) {
1773                 size_t len;
1774                 len = mtd->writesize + mtd->oobsize;
1775                 this->page_buf = kmalloc(len, GFP_KERNEL);
1776                 if (!this->page_buf) {
1777                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
1778                         return -ENOMEM;
1779                 }
1780                 this->options |= ONENAND_PAGEBUF_ALLOC;
1781         }
1782
1783         this->state = FL_READY;
1784         init_waitqueue_head(&this->wq);
1785         spin_lock_init(&this->chip_lock);
1786
1787         switch (mtd->oobsize) {
1788         case 64:
1789                 this->autooob = &onenand_oob_64;
1790                 break;
1791
1792         case 32:
1793                 this->autooob = &onenand_oob_32;
1794                 break;
1795
1796         default:
1797                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
1798                         mtd->oobsize);
1799                 /* To prevent kernel oops */
1800                 this->autooob = &onenand_oob_32;
1801                 break;
1802         }
1803
1804         memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
1805
1806         /* Fill in remaining MTD driver data */
1807         mtd->type = MTD_NANDFLASH;
1808         mtd->flags = MTD_CAP_NANDFLASH;
1809         mtd->ecctype = MTD_ECC_SW;
1810         mtd->erase = onenand_erase;
1811         mtd->point = NULL;
1812         mtd->unpoint = NULL;
1813         mtd->read = onenand_read;
1814         mtd->write = onenand_write;
1815         mtd->read_ecc = onenand_read_ecc;
1816         mtd->write_ecc = onenand_write_ecc;
1817         mtd->read_oob = onenand_read_oob;
1818         mtd->write_oob = onenand_write_oob;
1819 #ifdef CONFIG_MTD_ONENAND_OTP
1820         mtd->get_fact_prot_info = onenand_get_fact_prot_info;
1821         mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
1822         mtd->get_user_prot_info = onenand_get_user_prot_info;
1823         mtd->read_user_prot_reg = onenand_read_user_prot_reg;
1824         mtd->write_user_prot_reg = onenand_write_user_prot_reg;
1825         mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
1826 #endif
1827         mtd->sync = onenand_sync;
1828         mtd->lock = NULL;
1829         mtd->unlock = onenand_unlock;
1830         mtd->suspend = onenand_suspend;
1831         mtd->resume = onenand_resume;
1832         mtd->block_isbad = onenand_block_isbad;
1833         mtd->block_markbad = onenand_block_markbad;
1834         mtd->owner = THIS_MODULE;
1835
1836         /* Unlock whole block */
1837         mtd->unlock(mtd, 0x0, this->chipsize);
1838
1839         return this->scan_bbt(mtd);
1840 }
1841
1842 /**
1843  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1844  * @param mtd           MTD device structure
1845  */
1846 void onenand_release(struct mtd_info *mtd)
1847 {
1848         struct onenand_chip *this = mtd->priv;
1849
1850 #ifdef CONFIG_MTD_PARTITIONS
1851         /* Deregister partitions */
1852         del_mtd_partitions (mtd);
1853 #endif
1854         /* Deregister the device */
1855         del_mtd_device (mtd);
1856
1857         /* Free bad block table memory, if allocated */
1858         if (this->bbm)
1859                 kfree(this->bbm);
1860         /* Buffer allocated by onenand_scan */
1861         if (this->options & ONENAND_PAGEBUF_ALLOC)
1862                 kfree(this->page_buf);
1863 }
1864
1865 EXPORT_SYMBOL_GPL(onenand_scan);
1866 EXPORT_SYMBOL_GPL(onenand_release);
1867
1868 MODULE_LICENSE("GPL");
1869 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
1870 MODULE_DESCRIPTION("Generic OneNAND flash driver code");