]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/onenand/onenand_base.c
OneNand: Fix free byte positions.
[linux-2.6-omap-h63xx.git] / 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;
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                 block = (int) (addr >> this->erase_shift);
211                 page = -1;
212                 break;
213
214         default:
215                 block = (int) (addr >> this->erase_shift);
216                 page = (int) (addr >> this->page_shift);
217                 page &= this->page_mask;
218                 break;
219         }
220
221         /* NOTE: The setting order of the registers is very important! */
222         if (cmd == ONENAND_CMD_BUFFERRAM) {
223                 /* Select DataRAM for DDP */
224                 value = onenand_bufferram_address(this, block);
225                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
226
227                 /* Switch to the next data buffer */
228                 ONENAND_SET_NEXT_BUFFERRAM(this);
229
230                 return 0;
231         }
232
233         if (block != -1) {
234                 /* Write 'DFS, FBA' of Flash */
235                 value = onenand_block_address(this, block);
236                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
237         }
238
239         if (page != -1) {
240                 int dataram;
241
242                 switch (cmd) {
243                 case ONENAND_CMD_READ:
244                 case ONENAND_CMD_READOOB:
245                         dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
246                         readcmd = 1;
247                         break;
248
249                 default:
250                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
251                         break;
252                 }
253
254                 /* Write 'FPA, FSA' of Flash */
255                 value = onenand_page_address(page, sectors);
256                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
257
258                 /* Write 'BSA, BSC' of DataRAM */
259                 value = onenand_buffer_address(dataram, sectors, count);
260                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
261
262                 if (readcmd) {
263                         /* Select DataRAM for DDP */
264                         value = onenand_bufferram_address(this, block);
265                         this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
266                 }
267         }
268
269         /* Interrupt clear */
270         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
271
272         /* Write command */
273         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
274
275         return 0;
276 }
277
278 /**
279  * onenand_wait - [DEFAULT] wait until the command is done
280  * @param mtd           MTD device structure
281  * @param state         state to select the max. timeout value
282  *
283  * Wait for command done. This applies to all OneNAND command
284  * Read can take up to 30us, erase up to 2ms and program up to 350us
285  * according to general OneNAND specs
286  */
287 static int onenand_wait(struct mtd_info *mtd, int state)
288 {
289         struct onenand_chip * this = mtd->priv;
290         unsigned long timeout;
291         unsigned int flags = ONENAND_INT_MASTER;
292         unsigned int interrupt = 0;
293         unsigned int ctrl, ecc;
294
295         /* The 20 msec is enough */
296         timeout = jiffies + msecs_to_jiffies(20);
297         while (time_before(jiffies, timeout)) {
298                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
299
300                 if (interrupt & flags)
301                         break;
302
303                 if (state != FL_READING)
304                         cond_resched();
305                 touch_softlockup_watchdog();
306         }
307         /* To get correct interrupt status in timeout case */
308         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
309
310         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
311
312         if (ctrl & ONENAND_CTRL_ERROR) {
313                 /* It maybe occur at initial bad block */
314                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
315                 /* Clear other interrupt bits for preventing ECC error */
316                 interrupt &= ONENAND_INT_MASTER;
317         }
318
319         if (ctrl & ONENAND_CTRL_LOCK) {
320                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
321                 return -EACCES;
322         }
323
324         if (interrupt & ONENAND_INT_READ) {
325                 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
326                 if (ecc & ONENAND_ECC_2BIT_ALL) {
327                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
328                         return -EBADMSG;
329                 }
330         }
331
332         return 0;
333 }
334
335 /**
336  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
337  * @param mtd           MTD data structure
338  * @param area          BufferRAM area
339  * @return              offset given area
340  *
341  * Return BufferRAM offset given area
342  */
343 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
344 {
345         struct onenand_chip *this = mtd->priv;
346
347         if (ONENAND_CURRENT_BUFFERRAM(this)) {
348                 if (area == ONENAND_DATARAM)
349                         return mtd->oobblock;
350                 if (area == ONENAND_SPARERAM)
351                         return mtd->oobsize;
352         }
353
354         return 0;
355 }
356
357 /**
358  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
359  * @param mtd           MTD data structure
360  * @param area          BufferRAM area
361  * @param buffer        the databuffer to put/get data
362  * @param offset        offset to read from or write to
363  * @param count         number of bytes to read/write
364  *
365  * Read the BufferRAM area
366  */
367 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
368                 unsigned char *buffer, int offset, size_t count)
369 {
370         struct onenand_chip *this = mtd->priv;
371         void __iomem *bufferram;
372
373         bufferram = this->base + area;
374
375         bufferram += onenand_bufferram_offset(mtd, area);
376
377         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
378                 unsigned short word;
379
380                 /* Align with word(16-bit) size */
381                 count--;
382
383                 /* Read word and save byte */
384                 word = this->read_word(bufferram + offset + count);
385                 buffer[count] = (word & 0xff);
386         }
387
388         memcpy(buffer, bufferram + offset, count);
389
390         return 0;
391 }
392
393 /**
394  * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
395  * @param mtd           MTD data structure
396  * @param area          BufferRAM area
397  * @param buffer        the databuffer to put/get data
398  * @param offset        offset to read from or write to
399  * @param count         number of bytes to read/write
400  *
401  * Read the BufferRAM area with Sync. Burst Mode
402  */
403 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
404                 unsigned char *buffer, int offset, size_t count)
405 {
406         struct onenand_chip *this = mtd->priv;
407         void __iomem *bufferram;
408
409         bufferram = this->base + area;
410
411         bufferram += onenand_bufferram_offset(mtd, area);
412
413         this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
414
415         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
416                 unsigned short word;
417
418                 /* Align with word(16-bit) size */
419                 count--;
420
421                 /* Read word and save byte */
422                 word = this->read_word(bufferram + offset + count);
423                 buffer[count] = (word & 0xff);
424         }
425
426         memcpy(buffer, bufferram + offset, count);
427
428         this->mmcontrol(mtd, 0);
429
430         return 0;
431 }
432
433 /**
434  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
435  * @param mtd           MTD data structure
436  * @param area          BufferRAM area
437  * @param buffer        the databuffer to put/get data
438  * @param offset        offset to read from or write to
439  * @param count         number of bytes to read/write
440  *
441  * Write the BufferRAM area
442  */
443 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
444                 const unsigned char *buffer, int offset, size_t count)
445 {
446         struct onenand_chip *this = mtd->priv;
447         void __iomem *bufferram;
448
449         bufferram = this->base + area;
450
451         bufferram += onenand_bufferram_offset(mtd, area);
452
453         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
454                 unsigned short word;
455                 int byte_offset;
456
457                 /* Align with word(16-bit) size */
458                 count--;
459
460                 /* Calculate byte access offset */
461                 byte_offset = offset + count;
462
463                 /* Read word and save byte */
464                 word = this->read_word(bufferram + byte_offset);
465                 word = (word & ~0xff) | buffer[count];
466                 this->write_word(word, bufferram + byte_offset);
467         }
468
469         memcpy(bufferram + offset, buffer, count);
470
471         return 0;
472 }
473
474 /**
475  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
476  * @param mtd           MTD data structure
477  * @param addr          address to check
478  * @return              1 if there are valid data, otherwise 0
479  *
480  * Check bufferram if there is data we required
481  */
482 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
483 {
484         struct onenand_chip *this = mtd->priv;
485         int block, page;
486         int i;
487
488         block = (int) (addr >> this->erase_shift);
489         page = (int) (addr >> this->page_shift);
490         page &= this->page_mask;
491
492         i = ONENAND_CURRENT_BUFFERRAM(this);
493
494         /* Is there valid data? */
495         if (this->bufferram[i].block == block &&
496             this->bufferram[i].page == page &&
497             this->bufferram[i].valid)
498                 return 1;
499
500         return 0;
501 }
502
503 /**
504  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
505  * @param mtd           MTD data structure
506  * @param addr          address to update
507  * @param valid         valid flag
508  *
509  * Update BufferRAM information
510  */
511 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
512                 int valid)
513 {
514         struct onenand_chip *this = mtd->priv;
515         int block, page;
516         int i;
517
518         block = (int) (addr >> this->erase_shift);
519         page = (int) (addr >> this->page_shift);
520         page &= this->page_mask;
521
522         /* Invalidate BufferRAM */
523         for (i = 0; i < MAX_BUFFERRAM; i++) {
524                 if (this->bufferram[i].block == block &&
525                     this->bufferram[i].page == page)
526                         this->bufferram[i].valid = 0;
527         }
528
529         /* Update BufferRAM */
530         i = ONENAND_CURRENT_BUFFERRAM(this);
531         this->bufferram[i].block = block;
532         this->bufferram[i].page = page;
533         this->bufferram[i].valid = valid;
534
535         return 0;
536 }
537
538 /**
539  * onenand_get_device - [GENERIC] Get chip for selected access
540  * @param mtd           MTD device structure
541  * @param new_state     the state which is requested
542  *
543  * Get the device and lock it for exclusive access
544  */
545 static int onenand_get_device(struct mtd_info *mtd, int new_state)
546 {
547         struct onenand_chip *this = mtd->priv;
548         DECLARE_WAITQUEUE(wait, current);
549
550         /*
551          * Grab the lock and see if the device is available
552          */
553         while (1) {
554                 spin_lock(&this->chip_lock);
555                 if (this->state == FL_READY) {
556                         this->state = new_state;
557                         spin_unlock(&this->chip_lock);
558                         break;
559                 }
560                 if (new_state == FL_PM_SUSPENDED) {
561                         spin_unlock(&this->chip_lock);
562                         return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
563                 }
564                 set_current_state(TASK_UNINTERRUPTIBLE);
565                 add_wait_queue(&this->wq, &wait);
566                 spin_unlock(&this->chip_lock);
567                 schedule();
568                 remove_wait_queue(&this->wq, &wait);
569         }
570
571         return 0;
572 }
573
574 /**
575  * onenand_release_device - [GENERIC] release chip
576  * @param mtd           MTD device structure
577  *
578  * Deselect, release chip lock and wake up anyone waiting on the device
579  */
580 static void onenand_release_device(struct mtd_info *mtd)
581 {
582         struct onenand_chip *this = mtd->priv;
583
584         /* Release the chip */
585         spin_lock(&this->chip_lock);
586         this->state = FL_READY;
587         wake_up(&this->wq);
588         spin_unlock(&this->chip_lock);
589 }
590
591 /**
592  * onenand_read_ecc - [MTD Interface] Read data with ECC
593  * @param mtd           MTD device structure
594  * @param from          offset to read from
595  * @param len           number of bytes to read
596  * @param retlen        pointer to variable to store the number of read bytes
597  * @param buf           the databuffer to put data
598  * @param oob_buf       filesystem supplied oob data buffer
599  * @param oobsel        oob selection structure
600  *
601  * OneNAND read with ECC
602  */
603 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
604         size_t *retlen, u_char *buf,
605         u_char *oob_buf, struct nand_oobinfo *oobsel)
606 {
607         struct onenand_chip *this = mtd->priv;
608         int read = 0, column;
609         int thislen;
610         int ret = 0;
611
612         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
613
614         /* Do not allow reads past end of device */
615         if ((from + len) > mtd->size) {
616                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: Attempt read beyond end of device\n");
617                 *retlen = 0;
618                 return -EINVAL;
619         }
620
621         /* Grab the lock and see if the device is available */
622         onenand_get_device(mtd, FL_READING);
623
624         /* TODO handling oob */
625
626         while (read < len) {
627                 thislen = min_t(int, mtd->oobblock, len - read);
628
629                 column = from & (mtd->oobblock - 1);
630                 if (column + thislen > mtd->oobblock)
631                         thislen = mtd->oobblock - column;
632
633                 if (!onenand_check_bufferram(mtd, from)) {
634                         this->command(mtd, ONENAND_CMD_READ, from, mtd->oobblock);
635
636                         ret = this->wait(mtd, FL_READING);
637                         /* First copy data and check return value for ECC handling */
638                         onenand_update_bufferram(mtd, from, 1);
639                 }
640
641                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
642
643                 read += thislen;
644
645                 if (read == len)
646                         break;
647
648                 if (ret) {
649                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: read failed = %d\n", ret);
650                         goto out;
651                 }
652
653                 from += thislen;
654                 buf += thislen;
655         }
656
657 out:
658         /* Deselect and wake up anyone waiting on the device */
659         onenand_release_device(mtd);
660
661         /*
662          * Return success, if no ECC failures, else -EBADMSG
663          * fs driver will take care of that, because
664          * retlen == desired len and result == -EBADMSG
665          */
666         *retlen = read;
667         return ret;
668 }
669
670 /**
671  * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
672  * @param mtd           MTD device structure
673  * @param from          offset to read from
674  * @param len           number of bytes to read
675  * @param retlen        pointer to variable to store the number of read bytes
676  * @param buf           the databuffer to put data
677  *
678  * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
679 */
680 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
681         size_t *retlen, u_char *buf)
682 {
683         return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
684 }
685
686 /**
687  * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
688  * @param mtd           MTD device structure
689  * @param from          offset to read from
690  * @param len           number of bytes to read
691  * @param retlen        pointer to variable to store the number of read bytes
692  * @param buf           the databuffer to put data
693  *
694  * OneNAND read out-of-band data from the spare area
695  */
696 static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
697         size_t *retlen, u_char *buf)
698 {
699         struct onenand_chip *this = mtd->priv;
700         int read = 0, thislen, column;
701         int ret = 0;
702
703         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
704
705         /* Initialize return length value */
706         *retlen = 0;
707
708         /* Do not allow reads past end of device */
709         if (unlikely((from + len) > mtd->size)) {
710                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
711                 return -EINVAL;
712         }
713
714         /* Grab the lock and see if the device is available */
715         onenand_get_device(mtd, FL_READING);
716
717         column = from & (mtd->oobsize - 1);
718
719         while (read < len) {
720                 thislen = mtd->oobsize - column;
721                 thislen = min_t(int, thislen, len);
722
723                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
724
725                 onenand_update_bufferram(mtd, from, 0);
726
727                 ret = this->wait(mtd, FL_READING);
728                 /* First copy data and check return value for ECC handling */
729
730                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
731
732                 read += thislen;
733
734                 if (read == len)
735                         break;
736
737                 if (ret) {
738                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
739                         goto out;
740                 }
741
742                 buf += thislen;
743
744                 /* Read more? */
745                 if (read < len) {
746                         /* Page size */
747                         from += mtd->oobblock;
748                         column = 0;
749                 }
750         }
751
752 out:
753         /* Deselect and wake up anyone waiting on the device */
754         onenand_release_device(mtd);
755
756         *retlen = read;
757         return ret;
758 }
759
760 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
761 /**
762  * onenand_verify_page - [GENERIC] verify the chip contents after a write
763  * @param mtd           MTD device structure
764  * @param buf           the databuffer to verify
765  *
766  * Check DataRAM area directly
767  */
768 static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
769 {
770         struct onenand_chip *this = mtd->priv;
771         void __iomem *dataram0, *dataram1;
772         int ret = 0;
773
774         this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
775
776         ret = this->wait(mtd, FL_READING);
777         if (ret)
778                 return ret;
779
780         onenand_update_bufferram(mtd, addr, 1);
781
782         /* Check, if the two dataram areas are same */
783         dataram0 = this->base + ONENAND_DATARAM;
784         dataram1 = dataram0 + mtd->oobblock;
785
786         if (memcmp(dataram0, dataram1, mtd->oobblock))
787                 return -EBADMSG;
788
789         return 0;
790 }
791 #else
792 #define onenand_verify_page(...)        (0)
793 #endif
794
795 #define NOTALIGNED(x)   ((x & (mtd->oobblock - 1)) != 0)
796
797 /**
798  * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
799  * @param mtd           MTD device structure
800  * @param to            offset to write to
801  * @param len           number of bytes to write
802  * @param retlen        pointer to variable to store the number of written bytes
803  * @param buf           the data to write
804  * @param eccbuf        filesystem supplied oob data buffer
805  * @param oobsel        oob selection structure
806  *
807  * OneNAND write with ECC
808  */
809 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
810         size_t *retlen, const u_char *buf,
811         u_char *eccbuf, struct nand_oobinfo *oobsel)
812 {
813         struct onenand_chip *this = mtd->priv;
814         int written = 0;
815         int ret = 0;
816
817         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
818
819         /* Initialize retlen, in case of early exit */
820         *retlen = 0;
821
822         /* Do not allow writes past end of device */
823         if (unlikely((to + len) > mtd->size)) {
824                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
825                 return -EINVAL;
826         }
827
828         /* Reject writes, which are not page aligned */
829         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
830                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
831                 return -EINVAL;
832         }
833
834         /* Grab the lock and see if the device is available */
835         onenand_get_device(mtd, FL_WRITING);
836
837         /* Loop until all data write */
838         while (written < len) {
839                 int thislen = min_t(int, mtd->oobblock, len - written);
840
841                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
842
843                 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
844                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
845
846                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
847
848                 onenand_update_bufferram(mtd, to, 1);
849
850                 ret = this->wait(mtd, FL_WRITING);
851                 if (ret) {
852                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
853                         goto out;
854                 }
855
856                 written += thislen;
857
858                 /* Only check verify write turn on */
859                 ret = onenand_verify_page(mtd, (u_char *) buf, to);
860                 if (ret) {
861                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
862                         goto out;
863                 }
864
865                 if (written == len)
866                         break;
867
868                 to += thislen;
869                 buf += thislen;
870         }
871
872 out:
873         /* Deselect and wake up anyone waiting on the device */
874         onenand_release_device(mtd);
875
876         *retlen = written;
877
878         return ret;
879 }
880
881 /**
882  * onenand_write - [MTD Interface] compability function for onenand_write_ecc
883  * @param mtd           MTD device structure
884  * @param to            offset to write to
885  * @param len           number of bytes to write
886  * @param retlen        pointer to variable to store the number of written bytes
887  * @param buf           the data to write
888  *
889  * This function simply calls onenand_write_ecc
890  * with oob buffer and oobsel = NULL
891  */
892 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
893         size_t *retlen, const u_char *buf)
894 {
895         return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
896 }
897
898 /**
899  * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
900  * @param mtd           MTD device structure
901  * @param to            offset to write to
902  * @param len           number of bytes to write
903  * @param retlen        pointer to variable to store the number of written bytes
904  * @param buf           the data to write
905  *
906  * OneNAND write out-of-band
907  */
908 static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
909         size_t *retlen, const u_char *buf)
910 {
911         struct onenand_chip *this = mtd->priv;
912         int column, status;
913         int written = 0;
914
915         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
916
917         /* Initialize retlen, in case of early exit */
918         *retlen = 0;
919
920         /* Do not allow writes past end of device */
921         if (unlikely((to + len) > mtd->size)) {
922                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
923                 return -EINVAL;
924         }
925
926         /* Grab the lock and see if the device is available */
927         onenand_get_device(mtd, FL_WRITING);
928
929         /* Loop until all data write */
930         while (written < len) {
931                 int thislen = min_t(int, mtd->oobsize, len - written);
932
933                 column = to & (mtd->oobsize - 1);
934
935                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
936
937                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
938                 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
939
940                 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
941
942                 onenand_update_bufferram(mtd, to, 0);
943
944                 status = this->wait(mtd, FL_WRITING);
945                 if (status)
946                         goto out;
947
948                 written += thislen;
949
950                 if (written == len)
951                         break;
952
953                 to += thislen;
954                 buf += thislen;
955         }
956
957 out:
958         /* Deselect and wake up anyone waiting on the device */
959         onenand_release_device(mtd);
960
961         *retlen = written;
962
963         return 0;
964 }
965
966 /**
967  * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
968  * @param mtd           MTD device structure
969  * @param vecs          the iovectors to write
970  * @param count         number of vectors
971  * @param to            offset to write to
972  * @param retlen        pointer to variable to store the number of written bytes
973  * @param eccbuf        filesystem supplied oob data buffer
974  * @param oobsel        oob selection structure
975  *
976  * OneNAND write with iovec with ecc
977  */
978 static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
979         unsigned long count, loff_t to, size_t *retlen,
980         u_char *eccbuf, struct nand_oobinfo *oobsel)
981 {
982         struct onenand_chip *this = mtd->priv;
983         unsigned char *pbuf;
984         size_t total_len, len;
985         int i, written = 0;
986         int ret = 0;
987
988         /* Preset written len for early exit */
989         *retlen = 0;
990
991         /* Calculate total length of data */
992         total_len = 0;
993         for (i = 0; i < count; i++)
994                 total_len += vecs[i].iov_len;
995
996         DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
997
998         /* Do not allow write past end of the device */
999         if (unlikely((to + total_len) > mtd->size)) {
1000                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
1001                 return -EINVAL;
1002         }
1003
1004         /* Reject writes, which are not page aligned */
1005         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
1006                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
1007                 return -EINVAL;
1008         }
1009
1010         /* Grab the lock and see if the device is available */
1011         onenand_get_device(mtd, FL_WRITING);
1012
1013         /* TODO handling oob */
1014
1015         /* Loop until all keve's data has been written */
1016         len = 0;
1017         while (count) {
1018                 pbuf = this->page_buf;
1019                 /*
1020                  * If the given tuple is >= pagesize then
1021                  * write it out from the iov
1022                  */
1023                 if ((vecs->iov_len - len) >= mtd->oobblock) {
1024                         pbuf = vecs->iov_base + len;
1025
1026                         len += mtd->oobblock;
1027
1028                         /* Check, if we have to switch to the next tuple */
1029                         if (len >= (int) vecs->iov_len) {
1030                                 vecs++;
1031                                 len = 0;
1032                                 count--;
1033                         }
1034                 } else {
1035                         int cnt = 0, thislen;
1036                         while (cnt < mtd->oobblock) {
1037                                 thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
1038                                 memcpy(this->page_buf + cnt, vecs->iov_base + len, thislen);
1039                                 cnt += thislen;
1040                                 len += thislen;
1041
1042                                 /* Check, if we have to switch to the next tuple */
1043                                 if (len >= (int) vecs->iov_len) {
1044                                         vecs++;
1045                                         len = 0;
1046                                         count--;
1047                                 }
1048                         }
1049                 }
1050
1051                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
1052
1053                 this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
1054                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1055
1056                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
1057
1058                 onenand_update_bufferram(mtd, to, 1);
1059
1060                 ret = this->wait(mtd, FL_WRITING);
1061                 if (ret) {
1062                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
1063                         goto out;
1064                 }
1065
1066
1067                 /* Only check verify write turn on */
1068                 ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
1069                 if (ret) {
1070                         DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
1071                         goto out;
1072                 }
1073
1074                 written += mtd->oobblock;
1075
1076                 to += mtd->oobblock;
1077         }
1078
1079 out:
1080         /* Deselect and wakt up anyone waiting on the device */
1081         onenand_release_device(mtd);
1082
1083         *retlen = written;
1084
1085         return 0;
1086 }
1087
1088 /**
1089  * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
1090  * @param mtd           MTD device structure
1091  * @param vecs          the iovectors to write
1092  * @param count         number of vectors
1093  * @param to            offset to write to
1094  * @param retlen        pointer to variable to store the number of written bytes
1095  *
1096  * OneNAND write with kvec. This just calls the ecc function
1097  */
1098 static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
1099         unsigned long count, loff_t to, size_t *retlen)
1100 {
1101         return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
1102 }
1103
1104 /**
1105  * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1106  * @param mtd           MTD device structure
1107  * @param ofs           offset from device start
1108  * @param getchip       0, if the chip is already selected
1109  * @param allowbbt      1, if its allowed to access the bbt area
1110  *
1111  * Check, if the block is bad. Either by reading the bad block table or
1112  * calling of the scan function.
1113  */
1114 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1115 {
1116         struct onenand_chip *this = mtd->priv;
1117         struct bbm_info *bbm = this->bbm;
1118
1119         /* Return info from the table */
1120         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1121 }
1122
1123 /**
1124  * onenand_erase - [MTD Interface] erase block(s)
1125  * @param mtd           MTD device structure
1126  * @param instr         erase instruction
1127  *
1128  * Erase one ore more blocks
1129  */
1130 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1131 {
1132         struct onenand_chip *this = mtd->priv;
1133         unsigned int block_size;
1134         loff_t addr;
1135         int len;
1136         int ret = 0;
1137
1138         DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1139
1140         block_size = (1 << this->erase_shift);
1141
1142         /* Start address must align on block boundary */
1143         if (unlikely(instr->addr & (block_size - 1))) {
1144                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1145                 return -EINVAL;
1146         }
1147
1148         /* Length must align on block boundary */
1149         if (unlikely(instr->len & (block_size - 1))) {
1150                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1151                 return -EINVAL;
1152         }
1153
1154         /* Do not allow erase past end of device */
1155         if (unlikely((instr->len + instr->addr) > mtd->size)) {
1156                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1157                 return -EINVAL;
1158         }
1159
1160         instr->fail_addr = 0xffffffff;
1161
1162         /* Grab the lock and see if the device is available */
1163         onenand_get_device(mtd, FL_ERASING);
1164
1165         /* Loop throught the pages */
1166         len = instr->len;
1167         addr = instr->addr;
1168
1169         instr->state = MTD_ERASING;
1170
1171         while (len) {
1172
1173                 /* Check if we have a bad block, we do not erase bad blocks */
1174                 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1175                         printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1176                         instr->state = MTD_ERASE_FAILED;
1177                         goto erase_exit;
1178                 }
1179
1180                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1181
1182                 ret = this->wait(mtd, FL_ERASING);
1183                 /* Check, if it is write protected */
1184                 if (ret) {
1185                         if (ret == -EPERM)
1186                                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
1187                         else
1188                                 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1189                         instr->state = MTD_ERASE_FAILED;
1190                         instr->fail_addr = addr;
1191                         goto erase_exit;
1192                 }
1193
1194                 len -= block_size;
1195                 addr += block_size;
1196         }
1197
1198         instr->state = MTD_ERASE_DONE;
1199
1200 erase_exit:
1201
1202         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1203         /* Do call back function */
1204         if (!ret)
1205                 mtd_erase_callback(instr);
1206
1207         /* Deselect and wake up anyone waiting on the device */
1208         onenand_release_device(mtd);
1209
1210         return ret;
1211 }
1212
1213 /**
1214  * onenand_sync - [MTD Interface] sync
1215  * @param mtd           MTD device structure
1216  *
1217  * Sync is actually a wait for chip ready function
1218  */
1219 static void onenand_sync(struct mtd_info *mtd)
1220 {
1221         DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1222
1223         /* Grab the lock and see if the device is available */
1224         onenand_get_device(mtd, FL_SYNCING);
1225
1226         /* Release it and go back */
1227         onenand_release_device(mtd);
1228 }
1229
1230
1231 /**
1232  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1233  * @param mtd           MTD device structure
1234  * @param ofs           offset relative to mtd start
1235  *
1236  * Check whether the block is bad
1237  */
1238 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1239 {
1240         /* Check for invalid offset */
1241         if (ofs > mtd->size)
1242                 return -EINVAL;
1243
1244         return onenand_block_checkbad(mtd, ofs, 1, 0);
1245 }
1246
1247 /**
1248  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1249  * @param mtd           MTD device structure
1250  * @param ofs           offset from device start
1251  *
1252  * This is the default implementation, which can be overridden by
1253  * a hardware specific driver.
1254  */
1255 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1256 {
1257         struct onenand_chip *this = mtd->priv;
1258         struct bbm_info *bbm = this->bbm;
1259         u_char buf[2] = {0, 0};
1260         size_t retlen;
1261         int block;
1262
1263         /* Get block number */
1264         block = ((int) ofs) >> bbm->bbt_erase_shift;
1265         if (bbm->bbt)
1266                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1267
1268         /* We write two bytes, so we dont have to mess with 16 bit access */
1269         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1270         return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
1271 }
1272
1273 /**
1274  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1275  * @param mtd           MTD device structure
1276  * @param ofs           offset relative to mtd start
1277  *
1278  * Mark the block as bad
1279  */
1280 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1281 {
1282         struct onenand_chip *this = mtd->priv;
1283         int ret;
1284
1285         ret = onenand_block_isbad(mtd, ofs);
1286         if (ret) {
1287                 /* If it was bad already, return success and do nothing */
1288                 if (ret > 0)
1289                         return 0;
1290                 return ret;
1291         }
1292
1293         return this->block_markbad(mtd, ofs);
1294 }
1295
1296 /**
1297  * onenand_unlock - [MTD Interface] Unlock block(s)
1298  * @param mtd           MTD device structure
1299  * @param ofs           offset relative to mtd start
1300  * @param len           number of bytes to unlock
1301  *
1302  * Unlock one or more blocks
1303  */
1304 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1305 {
1306         struct onenand_chip *this = mtd->priv;
1307         int start, end, block, value, status;
1308
1309         start = ofs >> this->erase_shift;
1310         end = len >> this->erase_shift;
1311
1312         /* Continuous lock scheme */
1313         if (this->options & ONENAND_CONT_LOCK) {
1314                 /* Set start block address */
1315                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1316                 /* Set end block address */
1317                 this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1318                 /* Write unlock command */
1319                 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1320
1321                 /* There's no return value */
1322                 this->wait(mtd, FL_UNLOCKING);
1323
1324                 /* Sanity check */
1325                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1326                     & ONENAND_CTRL_ONGO)
1327                         continue;
1328
1329                 /* Check lock status */
1330                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1331                 if (!(status & ONENAND_WP_US))
1332                         printk(KERN_ERR "wp status = 0x%x\n", status);
1333
1334                 return 0;
1335         }
1336
1337         /* Block lock scheme */
1338         for (block = start; block < end; block++) {
1339                 /* Set block address */
1340                 value = onenand_block_address(this, block);
1341                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1342                 /* Select DataRAM for DDP */
1343                 value = onenand_bufferram_address(this, block);
1344                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1345                 /* Set start block address */
1346                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1347                 /* Write unlock command */
1348                 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1349
1350                 /* There's no return value */
1351                 this->wait(mtd, FL_UNLOCKING);
1352
1353                 /* Sanity check */
1354                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1355                     & ONENAND_CTRL_ONGO)
1356                         continue;
1357
1358                 /* Check lock status */
1359                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1360                 if (!(status & ONENAND_WP_US))
1361                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1362         }
1363
1364         return 0;
1365 }
1366
1367 /**
1368  * onenand_print_device_info - Print device ID
1369  * @param device        device ID
1370  *
1371  * Print device ID
1372  */
1373 static void onenand_print_device_info(int device)
1374 {
1375         int vcc, demuxed, ddp, density;
1376
1377         vcc = device & ONENAND_DEVICE_VCC_MASK;
1378         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1379         ddp = device & ONENAND_DEVICE_IS_DDP;
1380         density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1381         printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1382                 demuxed ? "" : "Muxed ",
1383                 ddp ? "(DDP)" : "",
1384                 (16 << density),
1385                 vcc ? "2.65/3.3" : "1.8",
1386                 device);
1387 }
1388
1389 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1390         {ONENAND_MFR_SAMSUNG, "Samsung"},
1391 };
1392
1393 /**
1394  * onenand_check_maf - Check manufacturer ID
1395  * @param manuf         manufacturer ID
1396  *
1397  * Check manufacturer ID
1398  */
1399 static int onenand_check_maf(int manuf)
1400 {
1401         int size = ARRAY_SIZE(onenand_manuf_ids);
1402         char *name;
1403         int i;
1404
1405         for (i = 0; i < size; i++)
1406                 if (manuf == onenand_manuf_ids[i].id)
1407                         break;
1408
1409         if (i < size)
1410                 name = onenand_manuf_ids[i].name;
1411         else
1412                 name = "Unknown";
1413
1414         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1415
1416         return (i == size);
1417 }
1418
1419 /**
1420  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1421  * @param mtd           MTD device structure
1422  *
1423  * OneNAND detection method:
1424  *   Compare the the values from command with ones from register
1425  */
1426 static int onenand_probe(struct mtd_info *mtd)
1427 {
1428         struct onenand_chip *this = mtd->priv;
1429         int bram_maf_id, bram_dev_id, maf_id, dev_id;
1430         int version_id;
1431         int density;
1432
1433         /* Send the command for reading device ID from BootRAM */
1434         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1435
1436         /* Read manufacturer and device IDs from BootRAM */
1437         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1438         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1439
1440         /* Check manufacturer ID */
1441         if (onenand_check_maf(bram_maf_id))
1442                 return -ENXIO;
1443
1444         /* Reset OneNAND to read default register values */
1445         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1446
1447         /* Read manufacturer and device IDs from Register */
1448         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1449         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1450
1451         /* Check OneNAND device */
1452         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1453                 return -ENXIO;
1454
1455         /* Flash device information */
1456         onenand_print_device_info(dev_id);
1457         this->device_id = dev_id;
1458
1459         density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1460         this->chipsize = (16 << density) << 20;
1461         /* Set density mask. it is used for DDP */
1462         this->density_mask = (1 << (density + 6));
1463
1464         /* OneNAND page size & block size */
1465         /* The data buffer size is equal to page size */
1466         mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1467         mtd->oobsize = mtd->oobblock >> 5;
1468         /* Pagers per block is always 64 in OneNAND */
1469         mtd->erasesize = mtd->oobblock << 6;
1470
1471         this->erase_shift = ffs(mtd->erasesize) - 1;
1472         this->page_shift = ffs(mtd->oobblock) - 1;
1473         this->ppb_shift = (this->erase_shift - this->page_shift);
1474         this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
1475
1476         /* REVIST: Multichip handling */
1477
1478         mtd->size = this->chipsize;
1479
1480         /* Version ID */
1481         version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1482         printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1483
1484         /* Lock scheme */
1485         if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1486             !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1487                 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1488                 this->options |= ONENAND_CONT_LOCK;
1489         }
1490
1491         return 0;
1492 }
1493
1494 /**
1495  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1496  * @param mtd           MTD device structure
1497  */
1498 static int onenand_suspend(struct mtd_info *mtd)
1499 {
1500         return onenand_get_device(mtd, FL_PM_SUSPENDED);
1501 }
1502
1503 /**
1504  * onenand_resume - [MTD Interface] Resume the OneNAND flash
1505  * @param mtd           MTD device structure
1506  */
1507 static void onenand_resume(struct mtd_info *mtd)
1508 {
1509         struct onenand_chip *this = mtd->priv;
1510
1511         if (this->state == FL_PM_SUSPENDED)
1512                 onenand_release_device(mtd);
1513         else
1514                 printk(KERN_ERR "resume() called for the chip which is not"
1515                                 "in suspended state\n");
1516 }
1517
1518
1519 /**
1520  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1521  * @param mtd           MTD device structure
1522  * @param maxchips      Number of chips to scan for
1523  *
1524  * This fills out all the not initialized function pointers
1525  * with the defaults.
1526  * The flash ID is read and the mtd/chip structures are
1527  * filled with the appropriate values.
1528  */
1529 int onenand_scan(struct mtd_info *mtd, int maxchips)
1530 {
1531         struct onenand_chip *this = mtd->priv;
1532
1533         if (!this->read_word)
1534                 this->read_word = onenand_readw;
1535         if (!this->write_word)
1536                 this->write_word = onenand_writew;
1537
1538         if (!this->command)
1539                 this->command = onenand_command;
1540         if (!this->wait)
1541                 this->wait = onenand_wait;
1542
1543         if (!this->read_bufferram)
1544                 this->read_bufferram = onenand_read_bufferram;
1545         if (!this->write_bufferram)
1546                 this->write_bufferram = onenand_write_bufferram;
1547
1548         if (!this->block_markbad)
1549                 this->block_markbad = onenand_default_block_markbad;
1550         if (!this->scan_bbt)
1551                 this->scan_bbt = onenand_default_bbt;
1552
1553         if (onenand_probe(mtd))
1554                 return -ENXIO;
1555
1556         /* Set Sync. Burst Read after probing */
1557         if (this->mmcontrol) {
1558                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1559                 this->read_bufferram = onenand_sync_read_bufferram;
1560         }
1561
1562         /* Allocate buffers, if necessary */
1563         if (!this->page_buf) {
1564                 size_t len;
1565                 len = mtd->oobblock + mtd->oobsize;
1566                 this->page_buf = kmalloc(len, GFP_KERNEL);
1567                 if (!this->page_buf) {
1568                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
1569                         return -ENOMEM;
1570                 }
1571                 this->options |= ONENAND_PAGEBUF_ALLOC;
1572         }
1573
1574         this->state = FL_READY;
1575         init_waitqueue_head(&this->wq);
1576         spin_lock_init(&this->chip_lock);
1577
1578         switch (mtd->oobsize) {
1579         case 64:
1580                 this->autooob = &onenand_oob_64;
1581                 break;
1582
1583         case 32:
1584                 this->autooob = &onenand_oob_32;
1585                 break;
1586
1587         default:
1588                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
1589                         mtd->oobsize);
1590                 /* To prevent kernel oops */
1591                 this->autooob = &onenand_oob_32;
1592                 break;
1593         }
1594
1595         memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
1596
1597         /* Fill in remaining MTD driver data */
1598         mtd->type = MTD_NANDFLASH;
1599         mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
1600         mtd->ecctype = MTD_ECC_SW;
1601         mtd->erase = onenand_erase;
1602         mtd->point = NULL;
1603         mtd->unpoint = NULL;
1604         mtd->read = onenand_read;
1605         mtd->write = onenand_write;
1606         mtd->read_ecc = onenand_read_ecc;
1607         mtd->write_ecc = onenand_write_ecc;
1608         mtd->read_oob = onenand_read_oob;
1609         mtd->write_oob = onenand_write_oob;
1610         mtd->readv = NULL;
1611         mtd->readv_ecc = NULL;
1612         mtd->writev = onenand_writev;
1613         mtd->writev_ecc = onenand_writev_ecc;
1614         mtd->sync = onenand_sync;
1615         mtd->lock = NULL;
1616         mtd->unlock = onenand_unlock;
1617         mtd->suspend = onenand_suspend;
1618         mtd->resume = onenand_resume;
1619         mtd->block_isbad = onenand_block_isbad;
1620         mtd->block_markbad = onenand_block_markbad;
1621         mtd->owner = THIS_MODULE;
1622
1623         /* Unlock whole block */
1624         mtd->unlock(mtd, 0x0, this->chipsize);
1625
1626         return this->scan_bbt(mtd);
1627 }
1628
1629 /**
1630  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1631  * @param mtd           MTD device structure
1632  */
1633 void onenand_release(struct mtd_info *mtd)
1634 {
1635         struct onenand_chip *this = mtd->priv;
1636
1637 #ifdef CONFIG_MTD_PARTITIONS
1638         /* Deregister partitions */
1639         del_mtd_partitions (mtd);
1640 #endif
1641         /* Deregister the device */
1642         del_mtd_device (mtd);
1643
1644         /* Free bad block table memory, if allocated */
1645         if (this->bbm)
1646                 kfree(this->bbm);
1647         /* Buffer allocated by onenand_scan */
1648         if (this->options & ONENAND_PAGEBUF_ALLOC)
1649                 kfree(this->page_buf);
1650 }
1651
1652 EXPORT_SYMBOL_GPL(onenand_scan);
1653 EXPORT_SYMBOL_GPL(onenand_release);
1654
1655 MODULE_LICENSE("GPL");
1656 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
1657 MODULE_DESCRIPTION("Generic OneNAND flash driver code");