blob: a7054aebca6c5730b6f27ea1b558b7014d67dd7f [file] [log] [blame]
Kyungmin Park916527f2007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <common.h>
13
14#ifdef CONFIG_CMD_ONENAND
15
16#include <linux/mtd/compat.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/onenand.h>
19
20#include <asm/io.h>
21#include <asm/errno.h>
22
Kyungmin Park77e475c2008-03-31 10:40:36 +090023/* It should access 16-bit instead of 8-bit */
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +020024static inline void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park77e475c2008-03-31 10:40:36 +090025{
26 void *ret = dst;
27 short *d = dst;
28 const short *s = src;
29
30 len >>= 1;
31 while (len-- > 0)
32 *d++ = *s++;
33 return ret;
34}
35
Kyungmin Park916527f2007-09-10 17:13:49 +090036static const unsigned char ffchars[] = {
37 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
45};
46
47/**
48 * onenand_readw - [OneNAND Interface] Read OneNAND register
49 * @param addr address to read
50 *
51 * Read OneNAND register
52 */
53static unsigned short onenand_readw(void __iomem * addr)
54{
55 return readw(addr);
56}
57
58/**
59 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
60 * @param value value to write
61 * @param addr address to write
62 *
63 * Write OneNAND register with value
64 */
65static void onenand_writew(unsigned short value, void __iomem * addr)
66{
67 writew(value, addr);
68}
69
70/**
71 * onenand_block_address - [DEFAULT] Get block address
72 * @param device the device id
73 * @param block the block
74 * @return translated block address if DDP, otherwise same
75 *
76 * Setup Start Address 1 Register (F100h)
77 */
78static int onenand_block_address(int device, int block)
79{
80 if (device & ONENAND_DEVICE_IS_DDP) {
81 /* Device Flash Core select, NAND Flash Block Address */
82 int dfs = 0, density, mask;
83
84 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
85 mask = (1 << (density + 6));
86
87 if (block & mask)
88 dfs = 1;
89
90 return (dfs << ONENAND_DDP_SHIFT) | (block & (mask - 1));
91 }
92
93 return block;
94}
95
96/**
97 * onenand_bufferram_address - [DEFAULT] Get bufferram address
98 * @param device the device id
99 * @param block the block
100 * @return set DBS value if DDP, otherwise 0
101 *
102 * Setup Start Address 2 Register (F101h) for DDP
103 */
104static int onenand_bufferram_address(int device, int block)
105{
106 if (device & ONENAND_DEVICE_IS_DDP) {
107 /* Device BufferRAM Select */
108 int dbs = 0, density, mask;
109
110 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
111 mask = (1 << (density + 6));
112
113 if (block & mask)
114 dbs = 1;
115
116 return (dbs << ONENAND_DDP_SHIFT);
117 }
118
119 return 0;
120}
121
122/**
123 * onenand_page_address - [DEFAULT] Get page address
124 * @param page the page address
125 * @param sector the sector address
126 * @return combined page and sector address
127 *
128 * Setup Start Address 8 Register (F107h)
129 */
130static int onenand_page_address(int page, int sector)
131{
132 /* Flash Page Address, Flash Sector Address */
133 int fpa, fsa;
134
135 fpa = page & ONENAND_FPA_MASK;
136 fsa = sector & ONENAND_FSA_MASK;
137
138 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
139}
140
141/**
142 * onenand_buffer_address - [DEFAULT] Get buffer address
143 * @param dataram1 DataRAM index
144 * @param sectors the sector address
145 * @param count the number of sectors
146 * @return the start buffer value
147 *
148 * Setup Start Buffer Register (F200h)
149 */
150static int onenand_buffer_address(int dataram1, int sectors, int count)
151{
152 int bsa, bsc;
153
154 /* BufferRAM Sector Address */
155 bsa = sectors & ONENAND_BSA_MASK;
156
157 if (dataram1)
158 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
159 else
160 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
161
162 /* BufferRAM Sector Count */
163 bsc = count & ONENAND_BSC_MASK;
164
165 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
166}
167
168/**
169 * onenand_command - [DEFAULT] Send command to OneNAND device
170 * @param mtd MTD device structure
171 * @param cmd the command to be sent
172 * @param addr offset to read from or write to
173 * @param len number of bytes to read or write
174 *
175 * Send command to OneNAND device. This function is used for middle/large page
176 * devices (1KB/2KB Bytes per page)
177 */
178static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
179 size_t len)
180{
181 struct onenand_chip *this = mtd->priv;
182 int value, readcmd = 0;
183 int block, page;
184 /* Now we use page size operation */
185 int sectors = 4, count = 4;
186
187 /* Address translation */
188 switch (cmd) {
189 case ONENAND_CMD_UNLOCK:
190 case ONENAND_CMD_LOCK:
191 case ONENAND_CMD_LOCK_TIGHT:
192 block = -1;
193 page = -1;
194 break;
195
196 case ONENAND_CMD_ERASE:
197 case ONENAND_CMD_BUFFERRAM:
198 block = (int)(addr >> this->erase_shift);
199 page = -1;
200 break;
201
202 default:
203 block = (int)(addr >> this->erase_shift);
204 page = (int)(addr >> this->page_shift);
205 page &= this->page_mask;
206 break;
207 }
208
209 /* NOTE: The setting order of the registers is very important! */
210 if (cmd == ONENAND_CMD_BUFFERRAM) {
211 /* Select DataRAM for DDP */
212 value = onenand_bufferram_address(this->device_id, block);
213 this->write_word(value,
214 this->base + ONENAND_REG_START_ADDRESS2);
215
216 /* Switch to the next data buffer */
217 ONENAND_SET_NEXT_BUFFERRAM(this);
218
219 return 0;
220 }
221
222 if (block != -1) {
223 /* Write 'DFS, FBA' of Flash */
224 value = onenand_block_address(this->device_id, block);
225 this->write_word(value,
226 this->base + ONENAND_REG_START_ADDRESS1);
227 }
228
229 if (page != -1) {
230 int dataram;
231
232 switch (cmd) {
233 case ONENAND_CMD_READ:
234 case ONENAND_CMD_READOOB:
235 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
236 readcmd = 1;
237 break;
238
239 default:
240 dataram = ONENAND_CURRENT_BUFFERRAM(this);
241 break;
242 }
243
244 /* Write 'FPA, FSA' of Flash */
245 value = onenand_page_address(page, sectors);
246 this->write_word(value,
247 this->base + ONENAND_REG_START_ADDRESS8);
248
249 /* Write 'BSA, BSC' of DataRAM */
250 value = onenand_buffer_address(dataram, sectors, count);
251 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
252
253 if (readcmd) {
254 /* Select DataRAM for DDP */
255 value =
256 onenand_bufferram_address(this->device_id, block);
257 this->write_word(value,
258 this->base +
259 ONENAND_REG_START_ADDRESS2);
260 }
261 }
262
263 /* Interrupt clear */
264 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
265 /* Write command */
266 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
267
268 return 0;
269}
270
271/**
272 * onenand_wait - [DEFAULT] wait until the command is done
273 * @param mtd MTD device structure
274 * @param state state to select the max. timeout value
275 *
276 * Wait for command done. This applies to all OneNAND command
277 * Read can take up to 30us, erase up to 2ms and program up to 350us
278 * according to general OneNAND specs
279 */
280static int onenand_wait(struct mtd_info *mtd, int state)
281{
282 struct onenand_chip *this = mtd->priv;
283 unsigned int flags = ONENAND_INT_MASTER;
284 unsigned int interrupt = 0;
285 unsigned int ctrl, ecc;
286
287 while (1) {
288 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
289 if (interrupt & flags)
290 break;
291 }
292
293 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
294
295 if (ctrl & ONENAND_CTRL_ERROR) {
Scott Wood3167c532008-06-20 12:38:57 -0500296 MTDDEBUG (MTD_DEBUG_LEVEL0,
297 "onenand_wait: controller error = 0x%04x\n", ctrl);
Kyungmin Park916527f2007-09-10 17:13:49 +0900298 return -EAGAIN;
299 }
300
301 if (ctrl & ONENAND_CTRL_LOCK) {
Scott Wood3167c532008-06-20 12:38:57 -0500302 MTDDEBUG (MTD_DEBUG_LEVEL0,
303 "onenand_wait: it's locked error = 0x%04x\n", ctrl);
Kyungmin Park916527f2007-09-10 17:13:49 +0900304 return -EIO;
305 }
306
307 if (interrupt & ONENAND_INT_READ) {
308 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
309 if (ecc & ONENAND_ECC_2BIT_ALL) {
Scott Wood3167c532008-06-20 12:38:57 -0500310 MTDDEBUG (MTD_DEBUG_LEVEL0,
311 "onenand_wait: ECC error = 0x%04x\n", ecc);
Kyungmin Park916527f2007-09-10 17:13:49 +0900312 return -EBADMSG;
313 }
314 }
315
316 return 0;
317}
318
319/**
320 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
321 * @param mtd MTD data structure
322 * @param area BufferRAM area
323 * @return offset given area
324 *
325 * Return BufferRAM offset given area
326 */
327static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
328{
329 struct onenand_chip *this = mtd->priv;
330
331 if (ONENAND_CURRENT_BUFFERRAM(this)) {
332 if (area == ONENAND_DATARAM)
333 return mtd->oobblock;
334 if (area == ONENAND_SPARERAM)
335 return mtd->oobsize;
336 }
337
338 return 0;
339}
340
341/**
342 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
343 * @param mtd MTD data structure
344 * @param area BufferRAM area
345 * @param buffer the databuffer to put/get data
346 * @param offset offset to read from or write to
347 * @param count number of bytes to read/write
348 *
349 * Read the BufferRAM area
350 */
351static int onenand_read_bufferram(struct mtd_info *mtd, int area,
352 unsigned char *buffer, int offset,
353 size_t count)
354{
355 struct onenand_chip *this = mtd->priv;
356 void __iomem *bufferram;
357
358 bufferram = this->base + area;
359 bufferram += onenand_bufferram_offset(mtd, area);
360
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200361 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900362
363 return 0;
364}
365
366/**
367 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
368 * @param mtd MTD data structure
369 * @param area BufferRAM area
370 * @param buffer the databuffer to put/get data
371 * @param offset offset to read from or write to
372 * @param count number of bytes to read/write
373 *
374 * Read the BufferRAM area with Sync. Burst Mode
375 */
376static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
377 unsigned char *buffer, int offset,
378 size_t count)
379{
380 struct onenand_chip *this = mtd->priv;
381 void __iomem *bufferram;
382
383 bufferram = this->base + area;
384 bufferram += onenand_bufferram_offset(mtd, area);
385
386 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
387
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200388 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900389
390 this->mmcontrol(mtd, 0);
391
392 return 0;
393}
394
395/**
396 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
397 * @param mtd MTD data structure
398 * @param area BufferRAM area
399 * @param buffer the databuffer to put/get data
400 * @param offset offset to read from or write to
401 * @param count number of bytes to read/write
402 *
403 * Write the BufferRAM area
404 */
405static int onenand_write_bufferram(struct mtd_info *mtd, int area,
406 const unsigned char *buffer, int offset,
407 size_t count)
408{
409 struct onenand_chip *this = mtd->priv;
410 void __iomem *bufferram;
411
412 bufferram = this->base + area;
413 bufferram += onenand_bufferram_offset(mtd, area);
414
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200415 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900416
417 return 0;
418}
419
420/**
421 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
422 * @param mtd MTD data structure
423 * @param addr address to check
424 * @return 1 if there are valid data, otherwise 0
425 *
426 * Check bufferram if there is data we required
427 */
428static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
429{
430 struct onenand_chip *this = mtd->priv;
431 int block, page;
432 int i;
433
434 block = (int)(addr >> this->erase_shift);
435 page = (int)(addr >> this->page_shift);
436 page &= this->page_mask;
437
438 i = ONENAND_CURRENT_BUFFERRAM(this);
439
440 /* Is there valid data? */
441 if (this->bufferram[i].block == block &&
442 this->bufferram[i].page == page && this->bufferram[i].valid)
443 return 1;
444
445 return 0;
446}
447
448/**
449 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
450 * @param mtd MTD data structure
451 * @param addr address to update
452 * @param valid valid flag
453 *
454 * Update BufferRAM information
455 */
456static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
457 int valid)
458{
459 struct onenand_chip *this = mtd->priv;
460 int block, page;
461 int i;
462
463 block = (int)(addr >> this->erase_shift);
464 page = (int)(addr >> this->page_shift);
465 page &= this->page_mask;
466
467 /* Invalidate BufferRAM */
468 for (i = 0; i < MAX_BUFFERRAM; i++) {
469 if (this->bufferram[i].block == block &&
470 this->bufferram[i].page == page)
471 this->bufferram[i].valid = 0;
472 }
473
474 /* Update BufferRAM */
475 i = ONENAND_CURRENT_BUFFERRAM(this);
476 this->bufferram[i].block = block;
477 this->bufferram[i].page = page;
478 this->bufferram[i].valid = valid;
479
480 return 0;
481}
482
483/**
484 * onenand_get_device - [GENERIC] Get chip for selected access
485 * @param mtd MTD device structure
486 * @param new_state the state which is requested
487 *
488 * Get the device and lock it for exclusive access
489 */
490static void onenand_get_device(struct mtd_info *mtd, int new_state)
491{
492 /* Do nothing */
493}
494
495/**
496 * onenand_release_device - [GENERIC] release chip
497 * @param mtd MTD device structure
498 *
499 * Deselect, release chip lock and wake up anyone waiting on the device
500 */
501static void onenand_release_device(struct mtd_info *mtd)
502{
503 /* Do nothing */
504}
505
506/**
507 * onenand_read_ecc - [MTD Interface] Read data with ECC
508 * @param mtd MTD device structure
509 * @param from offset to read from
510 * @param len number of bytes to read
511 * @param retlen pointer to variable to store the number of read bytes
512 * @param buf the databuffer to put data
513 * @param oob_buf filesystem supplied oob data buffer
514 * @param oobsel oob selection structure
515 *
516 * OneNAND read with ECC
517 */
518static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
519 size_t * retlen, u_char * buf,
520 u_char * oob_buf, struct nand_oobinfo *oobsel)
521{
522 struct onenand_chip *this = mtd->priv;
523 int read = 0, column;
524 int thislen;
525 int ret = 0;
526
Scott Wood3167c532008-06-20 12:38:57 -0500527 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_ecc: "
528 "from = 0x%08x, len = %i\n",
529 (unsigned int)from, (int)len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900530
531 /* Do not allow reads past end of device */
532 if ((from + len) > mtd->size) {
Scott Wood3167c532008-06-20 12:38:57 -0500533 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_ecc: "
534 "Attempt read beyond end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900535 *retlen = 0;
536 return -EINVAL;
537 }
538
539 /* Grab the lock and see if the device is available */
540 onenand_get_device(mtd, FL_READING);
541
542 while (read < len) {
543 thislen = min_t(int, mtd->oobblock, len - read);
544
545 column = from & (mtd->oobblock - 1);
546 if (column + thislen > mtd->oobblock)
547 thislen = mtd->oobblock - column;
548
549 if (!onenand_check_bufferram(mtd, from)) {
550 this->command(mtd, ONENAND_CMD_READ, from,
551 mtd->oobblock);
552 ret = this->wait(mtd, FL_READING);
553 /* First copy data and check return value for ECC handling */
554 onenand_update_bufferram(mtd, from, 1);
555 }
556
557 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column,
558 thislen);
559
560 read += thislen;
561 if (read == len)
562 break;
563
564 if (ret) {
Scott Wood3167c532008-06-20 12:38:57 -0500565 MTDDEBUG (MTD_DEBUG_LEVEL0,
566 "onenand_read_ecc: read failed = %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +0900567 break;
568 }
569
570 from += thislen;
571 buf += thislen;
572 }
573
574 /* Deselect and wake up anyone waiting on the device */
575 onenand_release_device(mtd);
576
577 /*
578 * Return success, if no ECC failures, else -EBADMSG
579 * fs driver will take care of that, because
580 * retlen == desired len and result == -EBADMSG
581 */
582 *retlen = read;
583 return ret;
584}
585
586/**
587 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
588 * @param mtd MTD device structure
589 * @param from offset to read from
590 * @param len number of bytes to read
591 * @param retlen pointer to variable to store the number of read bytes
592 * @param buf the databuffer to put data
593 *
594 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
595*/
596int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
597 size_t * retlen, u_char * buf)
598{
599 return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
600}
601
602/**
603 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
604 * @param mtd MTD device structure
605 * @param from offset to read from
606 * @param len number of bytes to read
607 * @param retlen pointer to variable to store the number of read bytes
608 * @param buf the databuffer to put data
609 *
610 * OneNAND read out-of-band data from the spare area
611 */
612int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
613 size_t * retlen, u_char * buf)
614{
615 struct onenand_chip *this = mtd->priv;
616 int read = 0, thislen, column;
617 int ret = 0;
618
Scott Wood3167c532008-06-20 12:38:57 -0500619 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_oob: "
620 "from = 0x%08x, len = %i\n",
621 (unsigned int)from, (int)len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900622
623 /* Initialize return length value */
624 *retlen = 0;
625
626 /* Do not allow reads past end of device */
627 if (unlikely((from + len) > mtd->size)) {
Scott Wood3167c532008-06-20 12:38:57 -0500628 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_oob: "
629 "Attempt read beyond end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900630 return -EINVAL;
631 }
632
633 /* Grab the lock and see if the device is available */
634 onenand_get_device(mtd, FL_READING);
635
636 column = from & (mtd->oobsize - 1);
637
638 while (read < len) {
639 thislen = mtd->oobsize - column;
640 thislen = min_t(int, thislen, len);
641
642 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
643
644 onenand_update_bufferram(mtd, from, 0);
645
646 ret = this->wait(mtd, FL_READING);
647 /* First copy data and check return value for ECC handling */
648
649 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column,
650 thislen);
651
652 read += thislen;
653 if (read == len)
654 break;
655
656 if (ret) {
Scott Wood3167c532008-06-20 12:38:57 -0500657 MTDDEBUG (MTD_DEBUG_LEVEL0,
658 "onenand_read_oob: read failed = %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +0900659 break;
660 }
661
662 buf += thislen;
663 /* Read more? */
664 if (read < len) {
665 /* Page size */
666 from += mtd->oobblock;
667 column = 0;
668 }
669 }
670
671 /* Deselect and wake up anyone waiting on the device */
672 onenand_release_device(mtd);
673
674 *retlen = read;
675 return ret;
676}
677
678#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
679/**
680 * onenand_verify_page - [GENERIC] verify the chip contents after a write
681 * @param mtd MTD device structure
682 * @param buf the databuffer to verify
Kyungmin Park916527f2007-09-10 17:13:49 +0900683 *
684 * Check DataRAM area directly
685 */
686static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
Steve Sakoman4d57b0f2008-08-11 20:26:16 +0200687 loff_t addr)
Kyungmin Park916527f2007-09-10 17:13:49 +0900688{
689 struct onenand_chip *this = mtd->priv;
690 void __iomem *dataram0, *dataram1;
691 int ret = 0;
692
693 this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
694
695 ret = this->wait(mtd, FL_READING);
696 if (ret)
697 return ret;
698
699 onenand_update_bufferram(mtd, addr, 1);
700
701 /* Check, if the two dataram areas are same */
702 dataram0 = this->base + ONENAND_DATARAM;
703 dataram1 = dataram0 + mtd->oobblock;
704
705 if (memcmp(dataram0, dataram1, mtd->oobblock))
706 return -EBADMSG;
707
708 return 0;
709}
710#else
711#define onenand_verify_page(...) (0)
712#endif
713
714#define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
715
716/**
717 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
718 * @param mtd MTD device structure
719 * @param to offset to write to
720 * @param len number of bytes to write
721 * @param retlen pointer to variable to store the number of written bytes
722 * @param buf the data to write
723 * @param eccbuf filesystem supplied oob data buffer
724 * @param oobsel oob selection structure
725 *
726 * OneNAND write with ECC
727 */
728static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
729 size_t * retlen, const u_char * buf,
730 u_char * eccbuf, struct nand_oobinfo *oobsel)
731{
732 struct onenand_chip *this = mtd->priv;
733 int written = 0;
734 int ret = 0;
735
Scott Wood3167c532008-06-20 12:38:57 -0500736 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_ecc: "
737 "to = 0x%08x, len = %i\n",
738 (unsigned int)to, (int)len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900739
740 /* Initialize retlen, in case of early exit */
741 *retlen = 0;
742
743 /* Do not allow writes past end of device */
744 if (unlikely((to + len) > mtd->size)) {
Scott Wood3167c532008-06-20 12:38:57 -0500745 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
746 "Attempt write to past end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900747 return -EINVAL;
748 }
749
750 /* Reject writes, which are not page aligned */
751 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
Scott Wood3167c532008-06-20 12:38:57 -0500752 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
753 "Attempt to write not page aligned data\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900754 return -EINVAL;
755 }
756
757 /* Grab the lock and see if the device is available */
758 onenand_get_device(mtd, FL_WRITING);
759
760 /* Loop until all data write */
761 while (written < len) {
762 int thislen = min_t(int, mtd->oobblock, len - written);
763
764 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
765
766 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
767 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
768 mtd->oobsize);
769
770 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
771
772 onenand_update_bufferram(mtd, to, 1);
773
774 ret = this->wait(mtd, FL_WRITING);
775 if (ret) {
Scott Wood3167c532008-06-20 12:38:57 -0500776 MTDDEBUG (MTD_DEBUG_LEVEL0,
777 "onenand_write_ecc: write filaed %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +0900778 break;
779 }
780
781 written += thislen;
782
783 /* Only check verify write turn on */
Steve Sakoman4d57b0f2008-08-11 20:26:16 +0200784 ret = onenand_verify_page(mtd, (u_char *) buf, to);
Kyungmin Park916527f2007-09-10 17:13:49 +0900785 if (ret) {
Scott Wood3167c532008-06-20 12:38:57 -0500786 MTDDEBUG (MTD_DEBUG_LEVEL0,
787 "onenand_write_ecc: verify failed %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +0900788 break;
789 }
790
791 if (written == len)
792 break;
793
794 to += thislen;
795 buf += thislen;
796 }
797
798 /* Deselect and wake up anyone waiting on the device */
799 onenand_release_device(mtd);
800
801 *retlen = written;
802
803 return ret;
804}
805
806/**
807 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
808 * @param mtd MTD device structure
809 * @param to offset to write to
810 * @param len number of bytes to write
811 * @param retlen pointer to variable to store the number of written bytes
812 * @param buf the data to write
813 *
814 * This function simply calls onenand_write_ecc
815 * with oob buffer and oobsel = NULL
816 */
817int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
818 size_t * retlen, const u_char * buf)
819{
820 return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
821}
822
823/**
824 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
825 * @param mtd MTD device structure
826 * @param to offset to write to
827 * @param len number of bytes to write
828 * @param retlen pointer to variable to store the number of written bytes
829 * @param buf the data to write
830 *
831 * OneNAND write out-of-band
832 */
833int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
834 size_t * retlen, const u_char * buf)
835{
836 struct onenand_chip *this = mtd->priv;
837 int column, status;
838 int written = 0;
839
Scott Wood3167c532008-06-20 12:38:57 -0500840 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_oob: "
841 "to = 0x%08x, len = %i\n",
842 (unsigned int)to, (int)len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900843
844 /* Initialize retlen, in case of early exit */
845 *retlen = 0;
846
847 /* Do not allow writes past end of device */
848 if (unlikely((to + len) > mtd->size)) {
Scott Wood3167c532008-06-20 12:38:57 -0500849 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_oob: "
850 "Attempt write to past end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900851 return -EINVAL;
852 }
853
854 /* Grab the lock and see if the device is available */
855 onenand_get_device(mtd, FL_WRITING);
856
857 /* Loop until all data write */
858 while (written < len) {
859 int thislen = min_t(int, mtd->oobsize, len - written);
860
861 column = to & (mtd->oobsize - 1);
862
863 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
864
865 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
866 mtd->oobsize);
867 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
868 thislen);
869
870 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
871
872 onenand_update_bufferram(mtd, to, 0);
873
874 status = this->wait(mtd, FL_WRITING);
875 if (status)
876 break;
877
878 written += thislen;
879 if (written == len)
880 break;
881
882 to += thislen;
883 buf += thislen;
884 }
885
886 /* Deselect and wake up anyone waiting on the device */
887 onenand_release_device(mtd);
888
889 *retlen = written;
890
891 return 0;
892}
893
894/**
895 * onenand_erase - [MTD Interface] erase block(s)
896 * @param mtd MTD device structure
897 * @param instr erase instruction
898 *
899 * Erase one ore more blocks
900 */
901int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
902{
903 struct onenand_chip *this = mtd->priv;
904 unsigned int block_size;
905 loff_t addr;
906 int len;
907 int ret = 0;
908
Scott Wood3167c532008-06-20 12:38:57 -0500909 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
910 (unsigned int)instr->addr, (unsigned int)instr->len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900911
912 block_size = (1 << this->erase_shift);
913
914 /* Start address must align on block boundary */
915 if (unlikely(instr->addr & (block_size - 1))) {
Scott Wood3167c532008-06-20 12:38:57 -0500916 MTDDEBUG (MTD_DEBUG_LEVEL0,
917 "onenand_erase: Unaligned address\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900918 return -EINVAL;
919 }
920
921 /* Length must align on block boundary */
922 if (unlikely(instr->len & (block_size - 1))) {
Scott Wood3167c532008-06-20 12:38:57 -0500923 MTDDEBUG (MTD_DEBUG_LEVEL0,
924 "onenand_erase: Length not block aligned\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900925 return -EINVAL;
926 }
927
928 /* Do not allow erase past end of device */
929 if (unlikely((instr->len + instr->addr) > mtd->size)) {
Scott Wood3167c532008-06-20 12:38:57 -0500930 MTDDEBUG (MTD_DEBUG_LEVEL0,
931 "onenand_erase: Erase past end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900932 return -EINVAL;
933 }
934
935 instr->fail_addr = 0xffffffff;
936
937 /* Grab the lock and see if the device is available */
938 onenand_get_device(mtd, FL_ERASING);
939
940 /* Loop throught the pages */
941 len = instr->len;
942 addr = instr->addr;
943
944 instr->state = MTD_ERASING;
945
946 while (len) {
947
948 /* TODO Check badblock */
949
950 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
951
952 ret = this->wait(mtd, FL_ERASING);
953 /* Check, if it is write protected */
954 if (ret) {
955 if (ret == -EPERM)
Scott Wood3167c532008-06-20 12:38:57 -0500956 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
957 "Device is write protected!!!\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900958 else
Scott Wood3167c532008-06-20 12:38:57 -0500959 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
960 "Failed erase, block %d\n",
961 (unsigned)(addr >> this->erase_shift));
Kyungmin Park916527f2007-09-10 17:13:49 +0900962 instr->state = MTD_ERASE_FAILED;
963 instr->fail_addr = addr;
964 goto erase_exit;
965 }
966
967 len -= block_size;
968 addr += block_size;
969 }
970
971 instr->state = MTD_ERASE_DONE;
972
973 erase_exit:
974
975 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
976 /* Do call back function */
977 if (!ret)
978 mtd_erase_callback(instr);
979
980 /* Deselect and wake up anyone waiting on the device */
981 onenand_release_device(mtd);
982
983 return ret;
984}
985
986/**
987 * onenand_sync - [MTD Interface] sync
988 * @param mtd MTD device structure
989 *
990 * Sync is actually a wait for chip ready function
991 */
992void onenand_sync(struct mtd_info *mtd)
993{
Scott Wood3167c532008-06-20 12:38:57 -0500994 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
Kyungmin Park916527f2007-09-10 17:13:49 +0900995
996 /* Grab the lock and see if the device is available */
997 onenand_get_device(mtd, FL_SYNCING);
998
999 /* Release it and go back */
1000 onenand_release_device(mtd);
1001}
1002
1003/**
1004 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1005 * @param mtd MTD device structure
1006 * @param ofs offset relative to mtd start
1007 */
1008int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1009{
1010 /*
1011 * TODO
1012 * 1. Bad block table (BBT)
1013 * -> using NAND BBT to support JFFS2
1014 * 2. Bad block management (BBM)
1015 * -> bad block replace scheme
1016 *
1017 * Currently we do nothing
1018 */
1019 return 0;
1020}
1021
1022/**
1023 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1024 * @param mtd MTD device structure
1025 * @param ofs offset relative to mtd start
1026 */
1027int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1028{
1029 /* see above */
1030 return 0;
1031}
1032
1033/**
1034 * onenand_unlock - [MTD Interface] Unlock block(s)
1035 * @param mtd MTD device structure
1036 * @param ofs offset relative to mtd start
1037 * @param len number of bytes to unlock
1038 *
1039 * Unlock one or more blocks
1040 */
1041int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1042{
1043 struct onenand_chip *this = mtd->priv;
1044 int start, end, block, value, status;
1045
1046 start = ofs >> this->erase_shift;
1047 end = len >> this->erase_shift;
1048
1049 /* Continuous lock scheme */
1050 if (this->options & ONENAND_CONT_LOCK) {
1051 /* Set start block address */
1052 this->write_word(start,
1053 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1054 /* Set end block address */
1055 this->write_word(end - 1,
1056 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1057 /* Write unlock command */
1058 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1059
1060 /* There's no return value */
1061 this->wait(mtd, FL_UNLOCKING);
1062
1063 /* Sanity check */
1064 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1065 & ONENAND_CTRL_ONGO)
1066 continue;
1067
1068 /* Check lock status */
1069 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1070 if (!(status & ONENAND_WP_US))
1071 printk(KERN_ERR "wp status = 0x%x\n", status);
1072
1073 return 0;
1074 }
1075
1076 /* Block lock scheme */
1077 for (block = start; block < end; block++) {
1078 /* Set start block address */
1079 this->write_word(block,
1080 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1081 /* Write unlock command */
1082 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1083
1084 /* There's no return value */
1085 this->wait(mtd, FL_UNLOCKING);
1086
1087 /* Sanity check */
1088 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1089 & ONENAND_CTRL_ONGO)
1090 continue;
1091
1092 /* Set block address for read block status */
1093 value = onenand_block_address(this->device_id, block);
1094 this->write_word(value,
1095 this->base + ONENAND_REG_START_ADDRESS1);
1096
1097 /* Check lock status */
1098 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1099 if (!(status & ONENAND_WP_US))
1100 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1101 block, status);
1102 }
1103
1104 return 0;
1105}
1106
1107/**
1108 * onenand_print_device_info - Print device ID
1109 * @param device device ID
1110 *
1111 * Print device ID
1112 */
1113void onenand_print_device_info(int device, int verbose)
1114{
1115 int vcc, demuxed, ddp, density;
1116
1117 if (!verbose)
1118 return;
1119
1120 vcc = device & ONENAND_DEVICE_VCC_MASK;
1121 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1122 ddp = device & ONENAND_DEVICE_IS_DDP;
1123 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1124 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1125 demuxed ? "" : "Muxed ",
1126 ddp ? "(DDP)" : "",
1127 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
1128}
1129
1130static const struct onenand_manufacturers onenand_manuf_ids[] = {
1131 {ONENAND_MFR_SAMSUNG, "Samsung"},
1132 {ONENAND_MFR_UNKNOWN, "Unknown"}
1133};
1134
1135/**
1136 * onenand_check_maf - Check manufacturer ID
1137 * @param manuf manufacturer ID
1138 *
1139 * Check manufacturer ID
1140 */
1141static int onenand_check_maf(int manuf)
1142{
1143 int i;
1144
1145 for (i = 0; onenand_manuf_ids[i].id; i++) {
1146 if (manuf == onenand_manuf_ids[i].id)
1147 break;
1148 }
1149
1150#ifdef ONENAND_DEBUG
1151 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
1152 onenand_manuf_ids[i].name, manuf);
1153#endif
1154
1155 return (i != ONENAND_MFR_UNKNOWN);
1156}
1157
1158/**
1159 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1160 * @param mtd MTD device structure
1161 *
1162 * OneNAND detection method:
1163 * Compare the the values from command with ones from register
1164 */
1165static int onenand_probe(struct mtd_info *mtd)
1166{
1167 struct onenand_chip *this = mtd->priv;
1168 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1169 int version_id;
1170 int density;
1171
1172 /* Send the command for reading device ID from BootRAM */
1173 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1174
1175 /* Read manufacturer and device IDs from BootRAM */
1176 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1177 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1178
1179 /* Check manufacturer ID */
1180 if (onenand_check_maf(bram_maf_id))
1181 return -ENXIO;
1182
1183 /* Reset OneNAND to read default register values */
1184 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1185
1186 {
1187 int i;
1188 for (i = 0; i < 10000; i++) ;
1189 }
1190
1191 /* Read manufacturer and device IDs from Register */
1192 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1193 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1194
1195 /* Check OneNAND device */
1196 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1197 return -ENXIO;
1198
Kyungmin Park1bb707c2008-03-17 08:54:06 +09001199 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1200 if (dev_id & (1 << 9)) {
1201 printk("Not yet support Flex-OneNAND\n");
1202 return -ENXIO;
1203 }
1204
Kyungmin Park916527f2007-09-10 17:13:49 +09001205 /* Flash device information */
1206 onenand_print_device_info(dev_id, 0);
1207 this->device_id = dev_id;
1208
1209 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1210 this->chipsize = (16 << density) << 20;
1211
1212 /* OneNAND page size & block size */
1213 /* The data buffer size is equal to page size */
1214 mtd->oobblock =
1215 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1216 mtd->oobsize = mtd->oobblock >> 5;
1217 /* Pagers per block is always 64 in OneNAND */
1218 mtd->erasesize = mtd->oobblock << 6;
1219
1220 this->erase_shift = ffs(mtd->erasesize) - 1;
1221 this->page_shift = ffs(mtd->oobblock) - 1;
1222 this->ppb_shift = (this->erase_shift - this->page_shift);
1223 this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
1224
1225 /* REVIST: Multichip handling */
1226
1227 mtd->size = this->chipsize;
1228
1229 /* Version ID */
1230 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1231#ifdef ONENAND_DEBUG
1232 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1233#endif
1234
1235 /* Lock scheme */
1236 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1237 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1238 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1239 this->options |= ONENAND_CONT_LOCK;
1240 }
1241
1242 return 0;
1243}
1244
1245/**
1246 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1247 * @param mtd MTD device structure
1248 * @param maxchips Number of chips to scan for
1249 *
1250 * This fills out all the not initialized function pointers
1251 * with the defaults.
1252 * The flash ID is read and the mtd/chip structures are
1253 * filled with the appropriate values.
1254 */
1255int onenand_scan(struct mtd_info *mtd, int maxchips)
1256{
1257 struct onenand_chip *this = mtd->priv;
1258
1259 if (!this->read_word)
1260 this->read_word = onenand_readw;
1261 if (!this->write_word)
1262 this->write_word = onenand_writew;
1263
1264 if (!this->command)
1265 this->command = onenand_command;
1266 if (!this->wait)
1267 this->wait = onenand_wait;
1268
1269 if (!this->read_bufferram)
1270 this->read_bufferram = onenand_read_bufferram;
1271 if (!this->write_bufferram)
1272 this->write_bufferram = onenand_write_bufferram;
1273
1274 if (onenand_probe(mtd))
1275 return -ENXIO;
1276
1277 /* Set Sync. Burst Read after probing */
1278 if (this->mmcontrol) {
1279 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1280 this->read_bufferram = onenand_sync_read_bufferram;
1281 }
1282
1283 onenand_unlock(mtd, 0, mtd->size);
1284
1285 return onenand_default_bbt(mtd);
1286}
1287
1288/**
1289 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1290 * @param mtd MTD device structure
1291 */
1292void onenand_release(struct mtd_info *mtd)
1293{
1294}
1295
Kyungmin Park916527f2007-09-10 17:13:49 +09001296#endif /* CONFIG_CMD_ONENAND */