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