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