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