blob: e342ba04118a642be1fc24b563e259b41d5ed188 [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
968 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
969 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
970
971 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
972
973 onenand_update_bufferram(mtd, to, 0);
974
Kyungmin Park8e6ec692006-05-12 17:02:41 +0300975 ret = this->wait(mtd, FL_WRITING);
976 if (ret) {
977 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100978 goto out;
Kyungmin Park8e6ec692006-05-12 17:02:41 +0300979 }
980
981 ret = onenand_verify_oob(mtd, buf, to, thislen);
982 if (ret) {
983 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
984 goto out;
985 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100986
987 written += thislen;
988
989 if (written == len)
990 break;
991
992 to += thislen;
993 buf += thislen;
994 }
995
996out:
997 /* Deselect and wake up anyone waiting on the device */
998 onenand_release_device(mtd);
999
1000 *retlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001001
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001002 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001003}
1004
1005/**
1006 * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
1007 * @param mtd MTD device structure
1008 * @param vecs the iovectors to write
1009 * @param count number of vectors
1010 * @param to offset to write to
1011 * @param retlen pointer to variable to store the number of written bytes
1012 * @param eccbuf filesystem supplied oob data buffer
1013 * @param oobsel oob selection structure
1014 *
1015 * OneNAND write with iovec with ecc
1016 */
1017static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
1018 unsigned long count, loff_t to, size_t *retlen,
1019 u_char *eccbuf, struct nand_oobinfo *oobsel)
1020{
1021 struct onenand_chip *this = mtd->priv;
Kyungmin Park532a37c2005-12-16 11:17:29 +09001022 unsigned char *pbuf;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001023 size_t total_len, len;
1024 int i, written = 0;
1025 int ret = 0;
1026
1027 /* Preset written len for early exit */
1028 *retlen = 0;
1029
1030 /* Calculate total length of data */
1031 total_len = 0;
1032 for (i = 0; i < count; i++)
1033 total_len += vecs[i].iov_len;
1034
1035 DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1036
1037 /* Do not allow write past end of the device */
1038 if (unlikely((to + total_len) > mtd->size)) {
1039 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
1040 return -EINVAL;
1041 }
1042
1043 /* Reject writes, which are not page aligned */
1044 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
1045 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
1046 return -EINVAL;
1047 }
1048
1049 /* Grab the lock and see if the device is available */
1050 onenand_get_device(mtd, FL_WRITING);
1051
1052 /* TODO handling oob */
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001053
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001054 /* Loop until all keve's data has been written */
1055 len = 0;
1056 while (count) {
Kyungmin Park532a37c2005-12-16 11:17:29 +09001057 pbuf = this->page_buf;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001058 /*
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001059 * If the given tuple is >= pagesize then
1060 * write it out from the iov
1061 */
1062 if ((vecs->iov_len - len) >= mtd->oobblock) {
1063 pbuf = vecs->iov_base + len;
1064
1065 len += mtd->oobblock;
1066
1067 /* Check, if we have to switch to the next tuple */
1068 if (len >= (int) vecs->iov_len) {
1069 vecs++;
1070 len = 0;
1071 count--;
1072 }
1073 } else {
1074 int cnt = 0, thislen;
1075 while (cnt < mtd->oobblock) {
1076 thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
Kyungmin Park532a37c2005-12-16 11:17:29 +09001077 memcpy(this->page_buf + cnt, vecs->iov_base + len, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001078 cnt += thislen;
1079 len += thislen;
1080
1081 /* Check, if we have to switch to the next tuple */
1082 if (len >= (int) vecs->iov_len) {
1083 vecs++;
1084 len = 0;
1085 count--;
1086 }
1087 }
1088 }
1089
1090 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
1091
1092 this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
1093 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1094
1095 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
1096
1097 onenand_update_bufferram(mtd, to, 1);
1098
1099 ret = this->wait(mtd, FL_WRITING);
1100 if (ret) {
1101 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
1102 goto out;
1103 }
1104
1105
1106 /* Only check verify write turn on */
Kyungmin Parkd36d63d2005-09-03 07:36:21 +01001107 ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001108 if (ret) {
1109 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
1110 goto out;
1111 }
1112
1113 written += mtd->oobblock;
1114
1115 to += mtd->oobblock;
1116 }
1117
1118out:
1119 /* Deselect and wakt up anyone waiting on the device */
1120 onenand_release_device(mtd);
1121
1122 *retlen = written;
1123
1124 return 0;
1125}
1126
1127/**
1128 * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
1129 * @param mtd MTD device structure
1130 * @param vecs the iovectors to write
1131 * @param count number of vectors
1132 * @param to offset to write to
1133 * @param retlen pointer to variable to store the number of written bytes
1134 *
1135 * OneNAND write with kvec. This just calls the ecc function
1136 */
1137static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
1138 unsigned long count, loff_t to, size_t *retlen)
1139{
1140 return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
1141}
1142
1143/**
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001144 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1145 * @param mtd MTD device structure
1146 * @param ofs offset from device start
1147 * @param getchip 0, if the chip is already selected
1148 * @param allowbbt 1, if its allowed to access the bbt area
1149 *
1150 * Check, if the block is bad. Either by reading the bad block table or
1151 * calling of the scan function.
1152 */
1153static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1154{
1155 struct onenand_chip *this = mtd->priv;
1156 struct bbm_info *bbm = this->bbm;
1157
1158 /* Return info from the table */
1159 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1160}
1161
1162/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001163 * onenand_erase - [MTD Interface] erase block(s)
1164 * @param mtd MTD device structure
1165 * @param instr erase instruction
1166 *
1167 * Erase one ore more blocks
1168 */
1169static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1170{
1171 struct onenand_chip *this = mtd->priv;
1172 unsigned int block_size;
1173 loff_t addr;
1174 int len;
1175 int ret = 0;
1176
1177 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1178
1179 block_size = (1 << this->erase_shift);
1180
1181 /* Start address must align on block boundary */
1182 if (unlikely(instr->addr & (block_size - 1))) {
1183 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1184 return -EINVAL;
1185 }
1186
1187 /* Length must align on block boundary */
1188 if (unlikely(instr->len & (block_size - 1))) {
1189 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1190 return -EINVAL;
1191 }
1192
1193 /* Do not allow erase past end of device */
1194 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1195 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1196 return -EINVAL;
1197 }
1198
1199 instr->fail_addr = 0xffffffff;
1200
1201 /* Grab the lock and see if the device is available */
1202 onenand_get_device(mtd, FL_ERASING);
1203
1204 /* Loop throught the pages */
1205 len = instr->len;
1206 addr = instr->addr;
1207
1208 instr->state = MTD_ERASING;
1209
1210 while (len) {
1211
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001212 /* Check if we have a bad block, we do not erase bad blocks */
1213 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1214 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1215 instr->state = MTD_ERASE_FAILED;
1216 goto erase_exit;
1217 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001218
1219 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1220
1221 ret = this->wait(mtd, FL_ERASING);
1222 /* Check, if it is write protected */
1223 if (ret) {
1224 if (ret == -EPERM)
1225 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
1226 else
1227 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1228 instr->state = MTD_ERASE_FAILED;
1229 instr->fail_addr = addr;
1230 goto erase_exit;
1231 }
1232
1233 len -= block_size;
1234 addr += block_size;
1235 }
1236
1237 instr->state = MTD_ERASE_DONE;
1238
1239erase_exit:
1240
1241 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1242 /* Do call back function */
1243 if (!ret)
1244 mtd_erase_callback(instr);
1245
1246 /* Deselect and wake up anyone waiting on the device */
1247 onenand_release_device(mtd);
1248
1249 return ret;
1250}
1251
1252/**
1253 * onenand_sync - [MTD Interface] sync
1254 * @param mtd MTD device structure
1255 *
1256 * Sync is actually a wait for chip ready function
1257 */
1258static void onenand_sync(struct mtd_info *mtd)
1259{
1260 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1261
1262 /* Grab the lock and see if the device is available */
1263 onenand_get_device(mtd, FL_SYNCING);
1264
1265 /* Release it and go back */
1266 onenand_release_device(mtd);
1267}
1268
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001269
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001270/**
1271 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1272 * @param mtd MTD device structure
1273 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001274 *
1275 * Check whether the block is bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001276 */
1277static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1278{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001279 /* Check for invalid offset */
1280 if (ofs > mtd->size)
1281 return -EINVAL;
1282
1283 return onenand_block_checkbad(mtd, ofs, 1, 0);
1284}
1285
1286/**
1287 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1288 * @param mtd MTD device structure
1289 * @param ofs offset from device start
1290 *
1291 * This is the default implementation, which can be overridden by
1292 * a hardware specific driver.
1293 */
1294static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1295{
1296 struct onenand_chip *this = mtd->priv;
1297 struct bbm_info *bbm = this->bbm;
1298 u_char buf[2] = {0, 0};
1299 size_t retlen;
1300 int block;
1301
1302 /* Get block number */
1303 block = ((int) ofs) >> bbm->bbt_erase_shift;
1304 if (bbm->bbt)
1305 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1306
1307 /* We write two bytes, so we dont have to mess with 16 bit access */
1308 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1309 return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001310}
1311
1312/**
1313 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1314 * @param mtd MTD device structure
1315 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001316 *
1317 * Mark the block as bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001318 */
1319static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1320{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001321 struct onenand_chip *this = mtd->priv;
1322 int ret;
1323
1324 ret = onenand_block_isbad(mtd, ofs);
1325 if (ret) {
1326 /* If it was bad already, return success and do nothing */
1327 if (ret > 0)
1328 return 0;
1329 return ret;
1330 }
1331
1332 return this->block_markbad(mtd, ofs);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001333}
1334
1335/**
1336 * onenand_unlock - [MTD Interface] Unlock block(s)
1337 * @param mtd MTD device structure
1338 * @param ofs offset relative to mtd start
1339 * @param len number of bytes to unlock
1340 *
1341 * Unlock one or more blocks
1342 */
1343static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1344{
1345 struct onenand_chip *this = mtd->priv;
1346 int start, end, block, value, status;
1347
1348 start = ofs >> this->erase_shift;
1349 end = len >> this->erase_shift;
1350
1351 /* Continuous lock scheme */
1352 if (this->options & ONENAND_CONT_LOCK) {
1353 /* Set start block address */
1354 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1355 /* Set end block address */
1356 this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1357 /* Write unlock command */
1358 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1359
1360 /* There's no return value */
1361 this->wait(mtd, FL_UNLOCKING);
1362
1363 /* Sanity check */
1364 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1365 & ONENAND_CTRL_ONGO)
1366 continue;
1367
1368 /* Check lock status */
1369 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1370 if (!(status & ONENAND_WP_US))
1371 printk(KERN_ERR "wp status = 0x%x\n", status);
1372
1373 return 0;
1374 }
1375
1376 /* Block lock scheme */
1377 for (block = start; block < end; block++) {
Kyungmin Park20ba89a2005-12-16 11:17:29 +09001378 /* Set block address */
1379 value = onenand_block_address(this, block);
1380 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1381 /* Select DataRAM for DDP */
1382 value = onenand_bufferram_address(this, block);
1383 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001384 /* Set start block address */
1385 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1386 /* Write unlock command */
1387 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1388
1389 /* There's no return value */
1390 this->wait(mtd, FL_UNLOCKING);
1391
1392 /* Sanity check */
1393 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1394 & ONENAND_CTRL_ONGO)
1395 continue;
1396
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001397 /* Check lock status */
1398 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1399 if (!(status & ONENAND_WP_US))
1400 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1401 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001402
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001403 return 0;
1404}
1405
1406/**
1407 * onenand_print_device_info - Print device ID
1408 * @param device device ID
1409 *
1410 * Print device ID
1411 */
1412static void onenand_print_device_info(int device)
1413{
1414 int vcc, demuxed, ddp, density;
1415
1416 vcc = device & ONENAND_DEVICE_VCC_MASK;
1417 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1418 ddp = device & ONENAND_DEVICE_IS_DDP;
1419 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1420 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1421 demuxed ? "" : "Muxed ",
1422 ddp ? "(DDP)" : "",
1423 (16 << density),
1424 vcc ? "2.65/3.3" : "1.8",
1425 device);
1426}
1427
1428static const struct onenand_manufacturers onenand_manuf_ids[] = {
1429 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001430};
1431
1432/**
1433 * onenand_check_maf - Check manufacturer ID
1434 * @param manuf manufacturer ID
1435 *
1436 * Check manufacturer ID
1437 */
1438static int onenand_check_maf(int manuf)
1439{
Kyungmin Park37b1cc32005-12-16 11:17:29 +09001440 int size = ARRAY_SIZE(onenand_manuf_ids);
1441 char *name;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001442 int i;
1443
Kyungmin Park37b1cc32005-12-16 11:17:29 +09001444 for (i = 0; i < size; i++)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001445 if (manuf == onenand_manuf_ids[i].id)
1446 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001447
Kyungmin Park37b1cc32005-12-16 11:17:29 +09001448 if (i < size)
1449 name = onenand_manuf_ids[i].name;
1450 else
1451 name = "Unknown";
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001452
Kyungmin Park37b1cc32005-12-16 11:17:29 +09001453 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1454
1455 return (i == size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001456}
1457
1458/**
1459 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1460 * @param mtd MTD device structure
1461 *
1462 * OneNAND detection method:
1463 * Compare the the values from command with ones from register
1464 */
1465static int onenand_probe(struct mtd_info *mtd)
1466{
1467 struct onenand_chip *this = mtd->priv;
1468 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1469 int version_id;
1470 int density;
1471
1472 /* Send the command for reading device ID from BootRAM */
1473 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1474
1475 /* Read manufacturer and device IDs from BootRAM */
1476 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1477 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1478
1479 /* Check manufacturer ID */
1480 if (onenand_check_maf(bram_maf_id))
1481 return -ENXIO;
1482
1483 /* Reset OneNAND to read default register values */
1484 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1485
1486 /* Read manufacturer and device IDs from Register */
1487 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1488 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1489
1490 /* Check OneNAND device */
1491 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1492 return -ENXIO;
1493
1494 /* Flash device information */
1495 onenand_print_device_info(dev_id);
1496 this->device_id = dev_id;
1497
1498 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1499 this->chipsize = (16 << density) << 20;
Kyungmin Park83a36832005-09-29 04:53:16 +01001500 /* Set density mask. it is used for DDP */
1501 this->density_mask = (1 << (density + 6));
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001502
1503 /* OneNAND page size & block size */
1504 /* The data buffer size is equal to page size */
1505 mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1506 mtd->oobsize = mtd->oobblock >> 5;
1507 /* Pagers per block is always 64 in OneNAND */
1508 mtd->erasesize = mtd->oobblock << 6;
1509
1510 this->erase_shift = ffs(mtd->erasesize) - 1;
1511 this->page_shift = ffs(mtd->oobblock) - 1;
1512 this->ppb_shift = (this->erase_shift - this->page_shift);
1513 this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
1514
1515 /* REVIST: Multichip handling */
1516
1517 mtd->size = this->chipsize;
1518
1519 /* Version ID */
1520 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1521 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1522
1523 /* Lock scheme */
1524 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1525 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1526 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1527 this->options |= ONENAND_CONT_LOCK;
1528 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001529
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001530 return 0;
1531}
1532
Kyungmin Parka41371e2005-09-29 03:55:31 +01001533/**
1534 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1535 * @param mtd MTD device structure
1536 */
1537static int onenand_suspend(struct mtd_info *mtd)
1538{
1539 return onenand_get_device(mtd, FL_PM_SUSPENDED);
1540}
1541
1542/**
1543 * onenand_resume - [MTD Interface] Resume the OneNAND flash
1544 * @param mtd MTD device structure
1545 */
1546static void onenand_resume(struct mtd_info *mtd)
1547{
1548 struct onenand_chip *this = mtd->priv;
1549
1550 if (this->state == FL_PM_SUSPENDED)
1551 onenand_release_device(mtd);
1552 else
1553 printk(KERN_ERR "resume() called for the chip which is not"
1554 "in suspended state\n");
1555}
1556
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001557
1558/**
1559 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1560 * @param mtd MTD device structure
1561 * @param maxchips Number of chips to scan for
1562 *
1563 * This fills out all the not initialized function pointers
1564 * with the defaults.
1565 * The flash ID is read and the mtd/chip structures are
1566 * filled with the appropriate values.
1567 */
1568int onenand_scan(struct mtd_info *mtd, int maxchips)
1569{
1570 struct onenand_chip *this = mtd->priv;
1571
1572 if (!this->read_word)
1573 this->read_word = onenand_readw;
1574 if (!this->write_word)
1575 this->write_word = onenand_writew;
1576
1577 if (!this->command)
1578 this->command = onenand_command;
1579 if (!this->wait)
1580 this->wait = onenand_wait;
1581
1582 if (!this->read_bufferram)
1583 this->read_bufferram = onenand_read_bufferram;
1584 if (!this->write_bufferram)
1585 this->write_bufferram = onenand_write_bufferram;
1586
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001587 if (!this->block_markbad)
1588 this->block_markbad = onenand_default_block_markbad;
1589 if (!this->scan_bbt)
1590 this->scan_bbt = onenand_default_bbt;
1591
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001592 if (onenand_probe(mtd))
1593 return -ENXIO;
1594
Kyungmin Park52b0eea2005-09-03 07:07:19 +01001595 /* Set Sync. Burst Read after probing */
1596 if (this->mmcontrol) {
1597 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1598 this->read_bufferram = onenand_sync_read_bufferram;
1599 }
1600
Kyungmin Park532a37c2005-12-16 11:17:29 +09001601 /* Allocate buffers, if necessary */
1602 if (!this->page_buf) {
1603 size_t len;
1604 len = mtd->oobblock + mtd->oobsize;
1605 this->page_buf = kmalloc(len, GFP_KERNEL);
1606 if (!this->page_buf) {
1607 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
1608 return -ENOMEM;
1609 }
1610 this->options |= ONENAND_PAGEBUF_ALLOC;
1611 }
1612
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001613 this->state = FL_READY;
1614 init_waitqueue_head(&this->wq);
1615 spin_lock_init(&this->chip_lock);
1616
1617 switch (mtd->oobsize) {
1618 case 64:
1619 this->autooob = &onenand_oob_64;
1620 break;
1621
1622 case 32:
1623 this->autooob = &onenand_oob_32;
1624 break;
1625
1626 default:
1627 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
1628 mtd->oobsize);
1629 /* To prevent kernel oops */
1630 this->autooob = &onenand_oob_32;
1631 break;
1632 }
1633
1634 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001635
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001636 /* Fill in remaining MTD driver data */
1637 mtd->type = MTD_NANDFLASH;
1638 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
1639 mtd->ecctype = MTD_ECC_SW;
1640 mtd->erase = onenand_erase;
1641 mtd->point = NULL;
1642 mtd->unpoint = NULL;
1643 mtd->read = onenand_read;
1644 mtd->write = onenand_write;
1645 mtd->read_ecc = onenand_read_ecc;
1646 mtd->write_ecc = onenand_write_ecc;
1647 mtd->read_oob = onenand_read_oob;
1648 mtd->write_oob = onenand_write_oob;
1649 mtd->readv = NULL;
1650 mtd->readv_ecc = NULL;
1651 mtd->writev = onenand_writev;
1652 mtd->writev_ecc = onenand_writev_ecc;
1653 mtd->sync = onenand_sync;
1654 mtd->lock = NULL;
1655 mtd->unlock = onenand_unlock;
Kyungmin Parka41371e2005-09-29 03:55:31 +01001656 mtd->suspend = onenand_suspend;
1657 mtd->resume = onenand_resume;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001658 mtd->block_isbad = onenand_block_isbad;
1659 mtd->block_markbad = onenand_block_markbad;
1660 mtd->owner = THIS_MODULE;
1661
1662 /* Unlock whole block */
1663 mtd->unlock(mtd, 0x0, this->chipsize);
1664
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001665 return this->scan_bbt(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001666}
1667
1668/**
1669 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1670 * @param mtd MTD device structure
1671 */
1672void onenand_release(struct mtd_info *mtd)
1673{
Kyungmin Park532a37c2005-12-16 11:17:29 +09001674 struct onenand_chip *this = mtd->priv;
1675
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001676#ifdef CONFIG_MTD_PARTITIONS
1677 /* Deregister partitions */
1678 del_mtd_partitions (mtd);
1679#endif
1680 /* Deregister the device */
1681 del_mtd_device (mtd);
Kyungmin Park532a37c2005-12-16 11:17:29 +09001682
1683 /* Free bad block table memory, if allocated */
1684 if (this->bbm)
1685 kfree(this->bbm);
1686 /* Buffer allocated by onenand_scan */
1687 if (this->options & ONENAND_PAGEBUF_ALLOC)
1688 kfree(this->page_buf);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001689}
1690
1691EXPORT_SYMBOL_GPL(onenand_scan);
1692EXPORT_SYMBOL_GPL(onenand_release);
1693
1694MODULE_LICENSE("GPL");
1695MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
1696MODULE_DESCRIPTION("Generic OneNAND flash driver code");