blob: eac9b28785967e7fcbe79509f190b7d3f5d1b73f [file] [log] [blame]
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
Kyungmin Park75384b02007-01-18 11:10:57 +09004 * Copyright (C) 2005-2007 Samsung Electronics
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01005 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Adrian Hunter81280d52007-02-15 09:47:29 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
Andrew Morton015953d2005-11-08 21:34:28 -080020#include <linux/sched.h>
Kyungmin Park2c221202006-11-16 11:23:48 +090021#include <linux/interrupt.h>
Andrew Morton015953d2005-11-08 21:34:28 -080022#include <linux/jiffies.h>
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010023#include <linux/mtd/mtd.h>
24#include <linux/mtd/onenand.h>
25#include <linux/mtd/partitions.h>
26
27#include <asm/io.h>
28
29/**
30 * onenand_oob_64 - oob info for large (2KB) page
31 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020032static struct nand_ecclayout onenand_oob_64 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010033 .eccbytes = 20,
34 .eccpos = {
35 8, 9, 10, 11, 12,
36 24, 25, 26, 27, 28,
37 40, 41, 42, 43, 44,
38 56, 57, 58, 59, 60,
39 },
40 .oobfree = {
41 {2, 3}, {14, 2}, {18, 3}, {30, 2},
Jarkko Lavinend9777f12006-05-12 17:02:35 +030042 {34, 3}, {46, 2}, {50, 3}, {62, 2}
43 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010044};
45
46/**
47 * onenand_oob_32 - oob info for middle (1KB) page
48 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020049static struct nand_ecclayout onenand_oob_32 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010050 .eccbytes = 10,
51 .eccpos = {
52 8, 9, 10, 11, 12,
53 24, 25, 26, 27, 28,
54 },
55 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
56};
57
58static const unsigned char ffchars[] = {
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
67};
68
69/**
70 * onenand_readw - [OneNAND Interface] Read OneNAND register
71 * @param addr address to read
72 *
73 * Read OneNAND register
74 */
75static unsigned short onenand_readw(void __iomem *addr)
76{
77 return readw(addr);
78}
79
80/**
81 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
82 * @param value value to write
83 * @param addr address to write
84 *
85 * Write OneNAND register with value
86 */
87static void onenand_writew(unsigned short value, void __iomem *addr)
88{
89 writew(value, addr);
90}
91
92/**
93 * onenand_block_address - [DEFAULT] Get block address
Kyungmin Park83a36832005-09-29 04:53:16 +010094 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010095 * @param block the block
96 * @return translated block address if DDP, otherwise same
97 *
98 * Setup Start Address 1 Register (F100h)
99 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100100static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100101{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900102 /* Device Flash Core select, NAND Flash Block Address */
103 if (block & this->density_mask)
104 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100105
106 return block;
107}
108
109/**
110 * onenand_bufferram_address - [DEFAULT] Get bufferram address
Kyungmin Park83a36832005-09-29 04:53:16 +0100111 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100112 * @param block the block
113 * @return set DBS value if DDP, otherwise 0
114 *
115 * Setup Start Address 2 Register (F101h) for DDP
116 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100117static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100118{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900119 /* Device BufferRAM Select */
120 if (block & this->density_mask)
121 return ONENAND_DDP_CHIP1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100122
Kyungmin Park738d61f2007-01-15 17:09:14 +0900123 return ONENAND_DDP_CHIP0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100124}
125
126/**
127 * onenand_page_address - [DEFAULT] Get page address
128 * @param page the page address
129 * @param sector the sector address
130 * @return combined page and sector address
131 *
132 * Setup Start Address 8 Register (F107h)
133 */
134static int onenand_page_address(int page, int sector)
135{
136 /* Flash Page Address, Flash Sector Address */
137 int fpa, fsa;
138
139 fpa = page & ONENAND_FPA_MASK;
140 fsa = sector & ONENAND_FSA_MASK;
141
142 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
143}
144
145/**
146 * onenand_buffer_address - [DEFAULT] Get buffer address
147 * @param dataram1 DataRAM index
148 * @param sectors the sector address
149 * @param count the number of sectors
150 * @return the start buffer value
151 *
152 * Setup Start Buffer Register (F200h)
153 */
154static int onenand_buffer_address(int dataram1, int sectors, int count)
155{
156 int bsa, bsc;
157
158 /* BufferRAM Sector Address */
159 bsa = sectors & ONENAND_BSA_MASK;
160
161 if (dataram1)
162 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
163 else
164 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
165
166 /* BufferRAM Sector Count */
167 bsc = count & ONENAND_BSC_MASK;
168
169 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
170}
171
172/**
173 * onenand_command - [DEFAULT] Send command to OneNAND device
174 * @param mtd MTD device structure
175 * @param cmd the command to be sent
176 * @param addr offset to read from or write to
177 * @param len number of bytes to read or write
178 *
179 * Send command to OneNAND device. This function is used for middle/large page
180 * devices (1KB/2KB Bytes per page)
181 */
182static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
183{
184 struct onenand_chip *this = mtd->priv;
Kyungmin Park493c6462006-05-12 17:03:07 +0300185 int value, readcmd = 0, block_cmd = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100186 int block, page;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100187
188 /* Address translation */
189 switch (cmd) {
190 case ONENAND_CMD_UNLOCK:
191 case ONENAND_CMD_LOCK:
192 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Park28b79ff2006-09-26 09:45:28 +0000193 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100194 block = -1;
195 page = -1;
196 break;
197
198 case ONENAND_CMD_ERASE:
199 case ONENAND_CMD_BUFFERRAM:
Kyungmin Park493c6462006-05-12 17:03:07 +0300200 case ONENAND_CMD_OTP_ACCESS:
201 block_cmd = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100202 block = (int) (addr >> this->erase_shift);
203 page = -1;
204 break;
205
206 default:
207 block = (int) (addr >> this->erase_shift);
208 page = (int) (addr >> this->page_shift);
209 page &= this->page_mask;
210 break;
211 }
212
213 /* NOTE: The setting order of the registers is very important! */
214 if (cmd == ONENAND_CMD_BUFFERRAM) {
215 /* Select DataRAM for DDP */
Kyungmin Park83a36832005-09-29 04:53:16 +0100216 value = onenand_bufferram_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100217 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
218
219 /* Switch to the next data buffer */
220 ONENAND_SET_NEXT_BUFFERRAM(this);
221
222 return 0;
223 }
224
225 if (block != -1) {
226 /* Write 'DFS, FBA' of Flash */
Kyungmin Park83a36832005-09-29 04:53:16 +0100227 value = onenand_block_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100228 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Park3cecf692006-05-12 17:02:51 +0300229
Kyungmin Park75287072006-05-12 17:03:23 +0300230 if (block_cmd) {
Kyungmin Park3cecf692006-05-12 17:02:51 +0300231 /* Select DataRAM for DDP */
232 value = onenand_bufferram_address(this, block);
233 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
234 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100235 }
236
237 if (page != -1) {
Kyungmin Park60d84f92006-12-22 16:21:54 +0900238 /* Now we use page size operation */
239 int sectors = 4, count = 4;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100240 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;
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900293 unsigned int ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100294
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();
305 }
306 /* To get correct interrupt status in timeout case */
307 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
308
309 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
310
311 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900312 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
Kyungmin Parkf6272482006-12-22 16:02:50 +0900313 if (ctrl & ONENAND_CTRL_LOCK)
Kyungmin Park211ac752007-02-07 12:15:01 +0900314 printk(KERN_ERR "onenand_wait: it's locked error.\n");
Kyungmin Parkf6272482006-12-22 16:02:50 +0900315 return ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100316 }
317
318 if (interrupt & ONENAND_INT_READ) {
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900319 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900320 if (ecc) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900321 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900322 if (ecc & ONENAND_ECC_2BIT_ALL) {
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900323 mtd->ecc_stats.failed++;
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900324 return ecc;
325 } else if (ecc & ONENAND_ECC_1BIT_ALL)
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900326 mtd->ecc_stats.corrected++;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100327 }
Adrian Hunter9d032802007-01-10 07:51:26 +0200328 } else if (state == FL_READING) {
329 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
330 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100331 }
332
333 return 0;
334}
335
Kyungmin Park2c221202006-11-16 11:23:48 +0900336/*
337 * onenand_interrupt - [DEFAULT] onenand interrupt handler
338 * @param irq onenand interrupt number
339 * @param dev_id interrupt data
340 *
341 * complete the work
342 */
343static irqreturn_t onenand_interrupt(int irq, void *data)
344{
345 struct onenand_chip *this = (struct onenand_chip *) data;
346
347 /* To handle shared interrupt */
348 if (!this->complete.done)
349 complete(&this->complete);
350
351 return IRQ_HANDLED;
352}
353
354/*
355 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
356 * @param mtd MTD device structure
357 * @param state state to select the max. timeout value
358 *
359 * Wait for command done.
360 */
361static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
362{
363 struct onenand_chip *this = mtd->priv;
364
Kyungmin Park2c221202006-11-16 11:23:48 +0900365 wait_for_completion(&this->complete);
366
367 return onenand_wait(mtd, state);
368}
369
370/*
371 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
372 * @param mtd MTD device structure
373 * @param state state to select the max. timeout value
374 *
375 * Try interrupt based wait (It is used one-time)
376 */
377static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
378{
379 struct onenand_chip *this = mtd->priv;
380 unsigned long remain, timeout;
381
382 /* We use interrupt wait first */
383 this->wait = onenand_interrupt_wait;
384
Kyungmin Park2c221202006-11-16 11:23:48 +0900385 timeout = msecs_to_jiffies(100);
386 remain = wait_for_completion_timeout(&this->complete, timeout);
387 if (!remain) {
388 printk(KERN_INFO "OneNAND: There's no interrupt. "
389 "We use the normal wait\n");
390
391 /* Release the irq */
392 free_irq(this->irq, this);
David Woodhousec9ac5972006-11-30 08:17:38 +0000393
Kyungmin Park2c221202006-11-16 11:23:48 +0900394 this->wait = onenand_wait;
395 }
396
397 return onenand_wait(mtd, state);
398}
399
400/*
401 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
402 * @param mtd MTD device structure
403 *
404 * There's two method to wait onenand work
405 * 1. polling - read interrupt status register
406 * 2. interrupt - use the kernel interrupt method
407 */
408static void onenand_setup_wait(struct mtd_info *mtd)
409{
410 struct onenand_chip *this = mtd->priv;
411 int syscfg;
412
413 init_completion(&this->complete);
414
415 if (this->irq <= 0) {
416 this->wait = onenand_wait;
417 return;
418 }
419
420 if (request_irq(this->irq, &onenand_interrupt,
421 IRQF_SHARED, "onenand", this)) {
422 /* If we can't get irq, use the normal wait */
423 this->wait = onenand_wait;
424 return;
425 }
426
427 /* Enable interrupt */
428 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
429 syscfg |= ONENAND_SYS_CFG1_IOBE;
430 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
431
432 this->wait = onenand_try_interrupt_wait;
433}
434
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100435/**
436 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
437 * @param mtd MTD data structure
438 * @param area BufferRAM area
439 * @return offset given area
440 *
441 * Return BufferRAM offset given area
442 */
443static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
444{
445 struct onenand_chip *this = mtd->priv;
446
447 if (ONENAND_CURRENT_BUFFERRAM(this)) {
448 if (area == ONENAND_DATARAM)
Joern Engel28318772006-05-22 23:18:05 +0200449 return mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100450 if (area == ONENAND_SPARERAM)
451 return mtd->oobsize;
452 }
453
454 return 0;
455}
456
457/**
458 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
459 * @param mtd MTD data structure
460 * @param area BufferRAM area
461 * @param buffer the databuffer to put/get data
462 * @param offset offset to read from or write to
463 * @param count number of bytes to read/write
464 *
465 * Read the BufferRAM area
466 */
467static int onenand_read_bufferram(struct mtd_info *mtd, int area,
468 unsigned char *buffer, int offset, size_t count)
469{
470 struct onenand_chip *this = mtd->priv;
471 void __iomem *bufferram;
472
473 bufferram = this->base + area;
474
475 bufferram += onenand_bufferram_offset(mtd, area);
476
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300477 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
478 unsigned short word;
479
480 /* Align with word(16-bit) size */
481 count--;
482
483 /* Read word and save byte */
484 word = this->read_word(bufferram + offset + count);
485 buffer[count] = (word & 0xff);
486 }
487
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100488 memcpy(buffer, bufferram + offset, count);
489
490 return 0;
491}
492
493/**
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100494 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
495 * @param mtd MTD data structure
496 * @param area BufferRAM area
497 * @param buffer the databuffer to put/get data
498 * @param offset offset to read from or write to
499 * @param count number of bytes to read/write
500 *
501 * Read the BufferRAM area with Sync. Burst Mode
502 */
503static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
504 unsigned char *buffer, int offset, size_t count)
505{
506 struct onenand_chip *this = mtd->priv;
507 void __iomem *bufferram;
508
509 bufferram = this->base + area;
510
511 bufferram += onenand_bufferram_offset(mtd, area);
512
513 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
514
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300515 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
516 unsigned short word;
517
518 /* Align with word(16-bit) size */
519 count--;
520
521 /* Read word and save byte */
522 word = this->read_word(bufferram + offset + count);
523 buffer[count] = (word & 0xff);
524 }
525
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100526 memcpy(buffer, bufferram + offset, count);
527
528 this->mmcontrol(mtd, 0);
529
530 return 0;
531}
532
533/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100534 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
535 * @param mtd MTD data structure
536 * @param area BufferRAM area
537 * @param buffer the databuffer to put/get data
538 * @param offset offset to read from or write to
539 * @param count number of bytes to read/write
540 *
541 * Write the BufferRAM area
542 */
543static int onenand_write_bufferram(struct mtd_info *mtd, int area,
544 const unsigned char *buffer, int offset, size_t count)
545{
546 struct onenand_chip *this = mtd->priv;
547 void __iomem *bufferram;
548
549 bufferram = this->base + area;
550
551 bufferram += onenand_bufferram_offset(mtd, area);
552
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300553 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
554 unsigned short word;
555 int byte_offset;
556
557 /* Align with word(16-bit) size */
558 count--;
559
560 /* Calculate byte access offset */
561 byte_offset = offset + count;
562
563 /* Read word and save byte */
564 word = this->read_word(bufferram + byte_offset);
565 word = (word & ~0xff) | buffer[count];
566 this->write_word(word, bufferram + byte_offset);
567 }
568
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100569 memcpy(bufferram + offset, buffer, count);
570
571 return 0;
572}
573
574/**
575 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
576 * @param mtd MTD data structure
577 * @param addr address to check
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000578 * @return 1 if there are valid data, otherwise 0
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100579 *
580 * Check bufferram if there is data we required
581 */
582static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
583{
584 struct onenand_chip *this = mtd->priv;
Adrian Huntercde36b32007-02-08 10:28:08 +0200585 int blockpage, found = 0;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900586 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000587
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900588 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100589
590 /* Is there valid data? */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900591 i = ONENAND_CURRENT_BUFFERRAM(this);
592 if (this->bufferram[i].blockpage == blockpage)
Adrian Huntercde36b32007-02-08 10:28:08 +0200593 found = 1;
594 else {
595 /* Check another BufferRAM */
596 i = ONENAND_NEXT_BUFFERRAM(this);
597 if (this->bufferram[i].blockpage == blockpage) {
598 ONENAND_SET_NEXT_BUFFERRAM(this);
599 found = 1;
600 }
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900601 }
602
Adrian Huntercde36b32007-02-08 10:28:08 +0200603 if (found && ONENAND_IS_DDP(this)) {
604 /* Select DataRAM for DDP */
605 int block = (int) (addr >> this->erase_shift);
606 int value = onenand_bufferram_address(this, block);
607 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
608 }
609
610 return found;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100611}
612
613/**
614 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
615 * @param mtd MTD data structure
616 * @param addr address to update
617 * @param valid valid flag
618 *
619 * Update BufferRAM information
620 */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900621static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100622 int valid)
623{
624 struct onenand_chip *this = mtd->priv;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900625 int blockpage;
626 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000627
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900628 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100629
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900630 /* Invalidate another BufferRAM */
631 i = ONENAND_NEXT_BUFFERRAM(this);
Kyungmin Park5b4246f2007-02-02 09:39:21 +0900632 if (this->bufferram[i].blockpage == blockpage)
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900633 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100634
635 /* Update BufferRAM */
636 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900637 if (valid)
638 this->bufferram[i].blockpage = blockpage;
639 else
640 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100641}
642
643/**
Adrian Hunter480b9df2007-02-07 13:55:19 +0200644 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
645 * @param mtd MTD data structure
646 * @param addr start address to invalidate
647 * @param len length to invalidate
648 *
649 * Invalidate BufferRAM information
650 */
651static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
652 unsigned int len)
653{
654 struct onenand_chip *this = mtd->priv;
655 int i;
656 loff_t end_addr = addr + len;
657
658 /* Invalidate BufferRAM */
659 for (i = 0; i < MAX_BUFFERRAM; i++) {
660 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
661 if (buf_addr >= addr && buf_addr < end_addr)
662 this->bufferram[i].blockpage = -1;
663 }
664}
665
666/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100667 * onenand_get_device - [GENERIC] Get chip for selected access
668 * @param mtd MTD device structure
669 * @param new_state the state which is requested
670 *
671 * Get the device and lock it for exclusive access
672 */
Kyungmin Parka41371e2005-09-29 03:55:31 +0100673static int onenand_get_device(struct mtd_info *mtd, int new_state)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100674{
675 struct onenand_chip *this = mtd->priv;
676 DECLARE_WAITQUEUE(wait, current);
677
678 /*
679 * Grab the lock and see if the device is available
680 */
681 while (1) {
682 spin_lock(&this->chip_lock);
683 if (this->state == FL_READY) {
684 this->state = new_state;
685 spin_unlock(&this->chip_lock);
686 break;
687 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100688 if (new_state == FL_PM_SUSPENDED) {
689 spin_unlock(&this->chip_lock);
690 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
691 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100692 set_current_state(TASK_UNINTERRUPTIBLE);
693 add_wait_queue(&this->wq, &wait);
694 spin_unlock(&this->chip_lock);
695 schedule();
696 remove_wait_queue(&this->wq, &wait);
697 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100698
699 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100700}
701
702/**
703 * onenand_release_device - [GENERIC] release chip
704 * @param mtd MTD device structure
705 *
706 * Deselect, release chip lock and wake up anyone waiting on the device
707 */
708static void onenand_release_device(struct mtd_info *mtd)
709{
710 struct onenand_chip *this = mtd->priv;
711
712 /* Release the chip */
713 spin_lock(&this->chip_lock);
714 this->state = FL_READY;
715 wake_up(&this->wq);
716 spin_unlock(&this->chip_lock);
717}
718
719/**
Thomas Gleixner9223a452006-05-23 17:21:03 +0200720 * onenand_read - [MTD Interface] Read data from flash
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100721 * @param mtd MTD device structure
722 * @param from offset to read from
723 * @param len number of bytes to read
724 * @param retlen pointer to variable to store the number of read bytes
725 * @param buf the databuffer to put data
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100726 *
Thomas Gleixner9223a452006-05-23 17:21:03 +0200727 * Read with ecc
728*/
729static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
730 size_t *retlen, u_char *buf)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100731{
732 struct onenand_chip *this = mtd->priv;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900733 struct mtd_ecc_stats stats;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100734 int read = 0, column;
735 int thislen;
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200736 int ret = 0, boundary = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100737
Thomas Gleixner9223a452006-05-23 17:21:03 +0200738 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100739
740 /* Do not allow reads past end of device */
741 if ((from + len) > mtd->size) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900742 printk(KERN_ERR "onenand_read: Attempt read beyond end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100743 *retlen = 0;
744 return -EINVAL;
745 }
746
747 /* Grab the lock and see if the device is available */
748 onenand_get_device(mtd, FL_READING);
749
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900750 stats = mtd->ecc_stats;
Artem Bityutskiy61a7e192006-12-26 16:41:24 +0900751
Adrian Huntera8de85d2007-01-04 09:51:26 +0200752 /* Read-while-load method */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100753
Adrian Huntera8de85d2007-01-04 09:51:26 +0200754 /* Do first load to bufferRAM */
755 if (read < len) {
756 if (!onenand_check_bufferram(mtd, from)) {
757 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
758 ret = this->wait(mtd, FL_READING);
759 onenand_update_bufferram(mtd, from, !ret);
760 }
761 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100762
Adrian Huntera8de85d2007-01-04 09:51:26 +0200763 thislen = min_t(int, mtd->writesize, len - read);
764 column = from & (mtd->writesize - 1);
765 if (column + thislen > mtd->writesize)
766 thislen = mtd->writesize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100767
Adrian Huntera8de85d2007-01-04 09:51:26 +0200768 while (!ret) {
769 /* If there is more to load then start next load */
770 from += thislen;
771 if (read + thislen < len) {
772 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200773 /*
774 * Chip boundary handling in DDP
775 * Now we issued chip 1 read and pointed chip 1
776 * bufferam so we have to point chip 0 bufferam.
777 */
Kyungmin Park738d61f2007-01-15 17:09:14 +0900778 if (ONENAND_IS_DDP(this) &&
779 unlikely(from == (this->chipsize >> 1))) {
780 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200781 boundary = 1;
782 } else
783 boundary = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +0200784 ONENAND_SET_PREV_BUFFERRAM(this);
785 }
786 /* While load is going, read from last bufferRAM */
787 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
788 /* See if we are done */
789 read += thislen;
790 if (read == len)
791 break;
792 /* Set up for next read from bufferRAM */
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200793 if (unlikely(boundary))
Kyungmin Park738d61f2007-01-15 17:09:14 +0900794 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Huntera8de85d2007-01-04 09:51:26 +0200795 ONENAND_SET_NEXT_BUFFERRAM(this);
796 buf += thislen;
797 thislen = min_t(int, mtd->writesize, len - read);
798 column = 0;
799 cond_resched();
800 /* Now wait for load */
801 ret = this->wait(mtd, FL_READING);
802 onenand_update_bufferram(mtd, from, !ret);
803 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100804
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100805 /* Deselect and wake up anyone waiting on the device */
806 onenand_release_device(mtd);
807
808 /*
809 * Return success, if no ECC failures, else -EBADMSG
810 * fs driver will take care of that, because
811 * retlen == desired len and result == -EBADMSG
812 */
813 *retlen = read;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900814
815 if (mtd->ecc_stats.failed - stats.failed)
816 return -EBADMSG;
817
Adrian Huntera8de85d2007-01-04 09:51:26 +0200818 if (ret)
819 return ret;
820
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900821 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100822}
823
824/**
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200825 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
826 * @param mtd MTD device structure
827 * @param buf destination address
828 * @param column oob offset to read from
829 * @param thislen oob length to read
830 */
831static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
832 int thislen)
833{
834 struct onenand_chip *this = mtd->priv;
835 struct nand_oobfree *free;
836 int readcol = column;
837 int readend = column + thislen;
838 int lastgap = 0;
839 uint8_t *oob_buf = this->page_buf + mtd->writesize;
840
841 for (free = this->ecclayout->oobfree; free->length; ++free) {
842 if (readcol >= lastgap)
843 readcol += free->offset - lastgap;
844 if (readend >= lastgap)
845 readend += free->offset - lastgap;
846 lastgap = free->offset + free->length;
847 }
Kyungmin Park4f4fad22007-02-02 09:22:21 +0900848 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200849 for (free = this->ecclayout->oobfree; free->length; ++free) {
850 int free_end = free->offset + free->length;
851 if (free->offset < readend && free_end > readcol) {
852 int st = max_t(int,free->offset,readcol);
853 int ed = min_t(int,free_end,readend);
854 int n = ed - st;
855 memcpy(buf, oob_buf + st, n);
856 buf += n;
857 }
858 }
859 return 0;
860}
861
862/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200863 * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100864 * @param mtd MTD device structure
865 * @param from offset to read from
866 * @param len number of bytes to read
867 * @param retlen pointer to variable to store the number of read bytes
868 * @param buf the databuffer to put data
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200869 * @param mode operation mode
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100870 *
871 * OneNAND read out-of-band data from the spare area
872 */
Kyungmin Park211ac752007-02-07 12:15:01 +0900873static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200874 size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100875{
876 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200877 int read = 0, thislen, column, oobsize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100878 int ret = 0;
879
880 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
881
882 /* Initialize return length value */
883 *retlen = 0;
884
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200885 if (mode == MTD_OOB_AUTO)
886 oobsize = this->ecclayout->oobavail;
887 else
888 oobsize = mtd->oobsize;
889
890 column = from & (mtd->oobsize - 1);
891
892 if (unlikely(column >= oobsize)) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900893 printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200894 return -EINVAL;
895 }
896
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100897 /* Do not allow reads past end of device */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200898 if (unlikely(from >= mtd->size ||
899 column + len > ((mtd->size >> this->page_shift) -
900 (from >> this->page_shift)) * oobsize)) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900901 printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100902 return -EINVAL;
903 }
904
905 /* Grab the lock and see if the device is available */
906 onenand_get_device(mtd, FL_READING);
907
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100908 while (read < len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +0900909 cond_resched();
910
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200911 thislen = oobsize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100912 thislen = min_t(int, thislen, len);
913
914 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
915
916 onenand_update_bufferram(mtd, from, 0);
917
918 ret = this->wait(mtd, FL_READING);
919 /* First copy data and check return value for ECC handling */
920
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200921 if (mode == MTD_OOB_AUTO)
922 onenand_transfer_auto_oob(mtd, buf, column, thislen);
923 else
924 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100925
Kyungmin Parkf6272482006-12-22 16:02:50 +0900926 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900927 printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +0900928 break;
Kyungmin Parkf6272482006-12-22 16:02:50 +0900929 }
930
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100931 read += thislen;
932
933 if (read == len)
934 break;
935
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100936 buf += thislen;
937
938 /* Read more? */
939 if (read < len) {
940 /* Page size */
Joern Engel28318772006-05-22 23:18:05 +0200941 from += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100942 column = 0;
943 }
944 }
945
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100946 /* Deselect and wake up anyone waiting on the device */
947 onenand_release_device(mtd);
948
949 *retlen = read;
950 return ret;
951}
952
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200953/**
954 * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +0900955 * @param mtd: MTD device structure
956 * @param from: offset to read from
957 * @param ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200958 */
959static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
960 struct mtd_oob_ops *ops)
961{
Kyungmin Park4f4fad22007-02-02 09:22:21 +0900962 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200963 case MTD_OOB_PLACE:
964 case MTD_OOB_AUTO:
965 break;
966 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +0900967 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200968 default:
969 return -EINVAL;
970 }
Kyungmin Park66a1e422006-12-11 01:34:23 +0000971 return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200972 &ops->oobretlen, ops->oobbuf, ops->mode);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200973}
974
Kyungmin Park211ac752007-02-07 12:15:01 +0900975/**
976 * onenand_bbt_wait - [DEFAULT] wait until the command is done
977 * @param mtd MTD device structure
978 * @param state state to select the max. timeout value
979 *
980 * Wait for command done.
981 */
982static int onenand_bbt_wait(struct mtd_info *mtd, int state)
983{
984 struct onenand_chip *this = mtd->priv;
985 unsigned long timeout;
986 unsigned int interrupt;
987 unsigned int ctrl;
988
989 /* The 20 msec is enough */
990 timeout = jiffies + msecs_to_jiffies(20);
991 while (time_before(jiffies, timeout)) {
992 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
993 if (interrupt & ONENAND_INT_MASTER)
994 break;
995 }
996 /* To get correct interrupt status in timeout case */
997 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
998 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
999
1000 if (ctrl & ONENAND_CTRL_ERROR) {
1001 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1002 /* Initial bad block case */
1003 if (ctrl & ONENAND_CTRL_LOAD)
1004 return ONENAND_BBT_READ_ERROR;
1005 return ONENAND_BBT_READ_FATAL_ERROR;
1006 }
1007
1008 if (interrupt & ONENAND_INT_READ) {
1009 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1010 if (ecc & ONENAND_ECC_2BIT_ALL)
1011 return ONENAND_BBT_READ_ERROR;
1012 } else {
1013 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1014 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1015 return ONENAND_BBT_READ_FATAL_ERROR;
1016 }
1017
1018 return 0;
1019}
1020
1021/**
1022 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1023 * @param mtd MTD device structure
1024 * @param from offset to read from
Kyungmin Parke3da8062007-02-15 09:36:39 +09001025 * @param ops oob operation description structure
Kyungmin Park211ac752007-02-07 12:15:01 +09001026 *
1027 * OneNAND read out-of-band data from the spare area for bbt scan
1028 */
1029int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1030 struct mtd_oob_ops *ops)
1031{
1032 struct onenand_chip *this = mtd->priv;
1033 int read = 0, thislen, column;
1034 int ret = 0;
1035 size_t len = ops->ooblen;
1036 u_char *buf = ops->oobbuf;
1037
Andrew Morton5785bdd2007-02-17 16:02:05 -08001038 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park211ac752007-02-07 12:15:01 +09001039
1040 /* Initialize return value */
1041 ops->oobretlen = 0;
1042
1043 /* Do not allow reads past end of device */
1044 if (unlikely((from + len) > mtd->size)) {
1045 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1046 return ONENAND_BBT_READ_FATAL_ERROR;
1047 }
1048
1049 /* Grab the lock and see if the device is available */
1050 onenand_get_device(mtd, FL_READING);
1051
1052 column = from & (mtd->oobsize - 1);
1053
1054 while (read < len) {
1055 cond_resched();
1056
1057 thislen = mtd->oobsize - column;
1058 thislen = min_t(int, thislen, len);
1059
1060 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1061
1062 onenand_update_bufferram(mtd, from, 0);
1063
1064 ret = onenand_bbt_wait(mtd, FL_READING);
1065 if (ret)
1066 break;
1067
1068 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1069 read += thislen;
1070 if (read == len)
1071 break;
1072
1073 buf += thislen;
1074
1075 /* Read more? */
1076 if (read < len) {
1077 /* Update Page size */
1078 from += mtd->writesize;
1079 column = 0;
1080 }
1081 }
1082
1083 /* Deselect and wake up anyone waiting on the device */
1084 onenand_release_device(mtd);
1085
1086 ops->oobretlen = read;
1087 return ret;
1088}
1089
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001090#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1091/**
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001092 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1093 * @param mtd MTD device structure
1094 * @param buf the databuffer to verify
1095 * @param to offset to read from
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001096 *
1097 */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001098static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001099{
1100 struct onenand_chip *this = mtd->priv;
Kyungmin Park91014e92007-02-12 10:34:39 +09001101 char oobbuf[64];
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001102 int status, i;
1103
1104 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1105 onenand_update_bufferram(mtd, to, 0);
1106 status = this->wait(mtd, FL_READING);
1107 if (status)
1108 return status;
1109
Kyungmin Park91014e92007-02-12 10:34:39 +09001110 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1111 for (i = 0; i < mtd->oobsize; i++)
1112 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001113 return -EBADMSG;
1114
1115 return 0;
1116}
1117
1118/**
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001119 * onenand_verify - [GENERIC] verify the chip contents after a write
1120 * @param mtd MTD device structure
1121 * @param buf the databuffer to verify
1122 * @param addr offset to read from
1123 * @param len number of bytes to read and compare
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001124 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001125 */
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001126static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001127{
1128 struct onenand_chip *this = mtd->priv;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001129 void __iomem *dataram;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001130 int ret = 0;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001131 int thislen, column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001132
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001133 while (len != 0) {
1134 thislen = min_t(int, mtd->writesize, len);
1135 column = addr & (mtd->writesize - 1);
1136 if (column + thislen > mtd->writesize)
1137 thislen = mtd->writesize - column;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001138
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001139 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001140
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001141 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001142
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001143 ret = this->wait(mtd, FL_READING);
1144 if (ret)
1145 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001146
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001147 onenand_update_bufferram(mtd, addr, 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001148
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001149 dataram = this->base + ONENAND_DATARAM;
1150 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1151
1152 if (memcmp(buf, dataram + column, thislen))
1153 return -EBADMSG;
1154
1155 len -= thislen;
1156 buf += thislen;
1157 addr += thislen;
1158 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001159
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001160 return 0;
1161}
1162#else
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001163#define onenand_verify(...) (0)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001164#define onenand_verify_oob(...) (0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001165#endif
1166
Kyungmin Park60d84f92006-12-22 16:21:54 +09001167#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001168
1169/**
Thomas Gleixner9223a452006-05-23 17:21:03 +02001170 * onenand_write - [MTD Interface] write buffer to FLASH
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001171 * @param mtd MTD device structure
1172 * @param to offset to write to
1173 * @param len number of bytes to write
1174 * @param retlen pointer to variable to store the number of written bytes
1175 * @param buf the data to write
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001176 *
Thomas Gleixner9223a452006-05-23 17:21:03 +02001177 * Write with ECC
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001178 */
Thomas Gleixner9223a452006-05-23 17:21:03 +02001179static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1180 size_t *retlen, const u_char *buf)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001181{
1182 struct onenand_chip *this = mtd->priv;
1183 int written = 0;
1184 int ret = 0;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001185 int column, subpage;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001186
Thomas Gleixner9223a452006-05-23 17:21:03 +02001187 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001188
1189 /* Initialize retlen, in case of early exit */
1190 *retlen = 0;
1191
1192 /* Do not allow writes past end of device */
1193 if (unlikely((to + len) > mtd->size)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001194 printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001195 return -EINVAL;
1196 }
1197
1198 /* Reject writes, which are not page aligned */
1199 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001200 printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001201 return -EINVAL;
1202 }
1203
Kyungmin Park60d84f92006-12-22 16:21:54 +09001204 column = to & (mtd->writesize - 1);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001205
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001206 /* Grab the lock and see if the device is available */
1207 onenand_get_device(mtd, FL_WRITING);
1208
1209 /* Loop until all data write */
1210 while (written < len) {
Adrian Hunter81f38e12007-02-07 10:55:23 +09001211 int thislen = min_t(int, mtd->writesize - column, len - written);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001212 u_char *wbuf = (u_char *) buf;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001213
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001214 cond_resched();
1215
Adrian Hunter81f38e12007-02-07 10:55:23 +09001216 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001217
Kyungmin Park60d84f92006-12-22 16:21:54 +09001218 /* Partial page write */
Adrian Hunter81f38e12007-02-07 10:55:23 +09001219 subpage = thislen < mtd->writesize;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001220 if (subpage) {
Kyungmin Park60d84f92006-12-22 16:21:54 +09001221 memset(this->page_buf, 0xff, mtd->writesize);
Adrian Hunter81f38e12007-02-07 10:55:23 +09001222 memcpy(this->page_buf + column, buf, thislen);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001223 wbuf = this->page_buf;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001224 }
1225
Adrian Hunter81f38e12007-02-07 10:55:23 +09001226 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001227 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1228
Joern Engel28318772006-05-22 23:18:05 +02001229 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001230
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001231 ret = this->wait(mtd, FL_WRITING);
Adrian Hunter81f38e12007-02-07 10:55:23 +09001232
1233 /* In partial page write we don't update bufferram */
1234 onenand_update_bufferram(mtd, to, !ret && !subpage);
1235
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001236 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001237 printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001238 break;
1239 }
1240
1241 /* Only check verify write turn on */
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001242 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001243 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001244 printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001245 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001246 }
1247
1248 written += thislen;
Adrian Hunter81f38e12007-02-07 10:55:23 +09001249
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001250 if (written == len)
1251 break;
1252
Kyungmin Park60d84f92006-12-22 16:21:54 +09001253 column = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001254 to += thislen;
1255 buf += thislen;
1256 }
1257
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001258 /* Deselect and wake up anyone waiting on the device */
1259 onenand_release_device(mtd);
1260
1261 *retlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001262
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001263 return ret;
1264}
1265
1266/**
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001267 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1268 * @param mtd MTD device structure
1269 * @param oob_buf oob buffer
1270 * @param buf source address
1271 * @param column oob offset to write to
1272 * @param thislen oob length to write
1273 */
1274static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1275 const u_char *buf, int column, int thislen)
1276{
1277 struct onenand_chip *this = mtd->priv;
1278 struct nand_oobfree *free;
1279 int writecol = column;
1280 int writeend = column + thislen;
1281 int lastgap = 0;
1282
1283 for (free = this->ecclayout->oobfree; free->length; ++free) {
1284 if (writecol >= lastgap)
1285 writecol += free->offset - lastgap;
1286 if (writeend >= lastgap)
1287 writeend += free->offset - lastgap;
1288 lastgap = free->offset + free->length;
1289 }
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001290 for (free = this->ecclayout->oobfree; free->length; ++free) {
1291 int free_end = free->offset + free->length;
1292 if (free->offset < writeend && free_end > writecol) {
1293 int st = max_t(int,free->offset,writecol);
1294 int ed = min_t(int,free_end,writeend);
1295 int n = ed - st;
1296 memcpy(oob_buf + st, buf, n);
1297 buf += n;
1298 }
1299 }
1300 return 0;
1301}
1302
1303/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001304 * onenand_do_write_oob - [Internal] OneNAND write out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001305 * @param mtd MTD device structure
1306 * @param to offset to write to
1307 * @param len number of bytes to write
1308 * @param retlen pointer to variable to store the number of written bytes
1309 * @param buf the data to write
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001310 * @param mode operation mode
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001311 *
1312 * OneNAND write out-of-band
1313 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001314static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001315 size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001316{
1317 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001318 int column, ret = 0, oobsize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001319 int written = 0;
Kyungmin Park91014e92007-02-12 10:34:39 +09001320 u_char *oobbuf;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001321
1322 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1323
1324 /* Initialize retlen, in case of early exit */
1325 *retlen = 0;
1326
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001327 if (mode == MTD_OOB_AUTO)
1328 oobsize = this->ecclayout->oobavail;
1329 else
1330 oobsize = mtd->oobsize;
1331
1332 column = to & (mtd->oobsize - 1);
1333
1334 if (unlikely(column >= oobsize)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001335 printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001336 return -EINVAL;
1337 }
1338
Adrian Hunter52e42002007-02-06 09:15:39 +09001339 /* For compatibility with NAND: Do not allow write past end of page */
Kyungmin Park91014e92007-02-12 10:34:39 +09001340 if (unlikely(column + len > oobsize)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001341 printk(KERN_ERR "onenand_write_oob: "
Adrian Hunter52e42002007-02-06 09:15:39 +09001342 "Attempt to write past end of page\n");
1343 return -EINVAL;
1344 }
1345
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001346 /* Do not allow reads past end of device */
1347 if (unlikely(to >= mtd->size ||
1348 column + len > ((mtd->size >> this->page_shift) -
1349 (to >> this->page_shift)) * oobsize)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001350 printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001351 return -EINVAL;
1352 }
1353
1354 /* Grab the lock and see if the device is available */
1355 onenand_get_device(mtd, FL_WRITING);
1356
Kyungmin Park91014e92007-02-12 10:34:39 +09001357 oobbuf = this->page_buf + mtd->writesize;
1358
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001359 /* Loop until all data write */
1360 while (written < len) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001361 int thislen = min_t(int, oobsize, len - written);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001362
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001363 cond_resched();
1364
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001365 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1366
Kyungmin Park34c10602006-05-12 17:02:46 +03001367 /* We send data to spare ram with oobsize
1368 * to prevent byte access */
Kyungmin Park91014e92007-02-12 10:34:39 +09001369 memset(oobbuf, 0xff, mtd->oobsize);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001370 if (mode == MTD_OOB_AUTO)
Kyungmin Park91014e92007-02-12 10:34:39 +09001371 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001372 else
Kyungmin Park91014e92007-02-12 10:34:39 +09001373 memcpy(oobbuf + column, buf, thislen);
1374 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001375
1376 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1377
1378 onenand_update_bufferram(mtd, to, 0);
1379
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001380 ret = this->wait(mtd, FL_WRITING);
1381 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001382 printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09001383 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001384 }
1385
Kyungmin Park91014e92007-02-12 10:34:39 +09001386 ret = onenand_verify_oob(mtd, oobbuf, to);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001387 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001388 printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09001389 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001390 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001391
1392 written += thislen;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001393 if (written == len)
1394 break;
1395
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001396 to += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001397 buf += thislen;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001398 column = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001399 }
1400
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001401 /* Deselect and wake up anyone waiting on the device */
1402 onenand_release_device(mtd);
1403
1404 *retlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001405
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001406 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001407}
1408
1409/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001410 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09001411 * @param mtd: MTD device structure
1412 * @param to: offset to write
1413 * @param ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001414 */
1415static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1416 struct mtd_oob_ops *ops)
1417{
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001418 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001419 case MTD_OOB_PLACE:
1420 case MTD_OOB_AUTO:
1421 break;
1422 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001423 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001424 default:
1425 return -EINVAL;
1426 }
Kyungmin Park66a1e422006-12-11 01:34:23 +00001427 return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001428 &ops->oobretlen, ops->oobbuf, ops->mode);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001429}
1430
1431/**
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001432 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1433 * @param mtd MTD device structure
1434 * @param ofs offset from device start
1435 * @param getchip 0, if the chip is already selected
1436 * @param allowbbt 1, if its allowed to access the bbt area
1437 *
1438 * Check, if the block is bad. Either by reading the bad block table or
1439 * calling of the scan function.
1440 */
1441static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1442{
1443 struct onenand_chip *this = mtd->priv;
1444 struct bbm_info *bbm = this->bbm;
1445
1446 /* Return info from the table */
1447 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1448}
1449
1450/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001451 * onenand_erase - [MTD Interface] erase block(s)
1452 * @param mtd MTD device structure
1453 * @param instr erase instruction
1454 *
1455 * Erase one ore more blocks
1456 */
1457static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1458{
1459 struct onenand_chip *this = mtd->priv;
1460 unsigned int block_size;
1461 loff_t addr;
1462 int len;
1463 int ret = 0;
1464
1465 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1466
1467 block_size = (1 << this->erase_shift);
1468
1469 /* Start address must align on block boundary */
1470 if (unlikely(instr->addr & (block_size - 1))) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001471 printk(KERN_ERR "onenand_erase: Unaligned address\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001472 return -EINVAL;
1473 }
1474
1475 /* Length must align on block boundary */
1476 if (unlikely(instr->len & (block_size - 1))) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001477 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001478 return -EINVAL;
1479 }
1480
1481 /* Do not allow erase past end of device */
1482 if (unlikely((instr->len + instr->addr) > mtd->size)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001483 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001484 return -EINVAL;
1485 }
1486
1487 instr->fail_addr = 0xffffffff;
1488
1489 /* Grab the lock and see if the device is available */
1490 onenand_get_device(mtd, FL_ERASING);
1491
1492 /* Loop throught the pages */
1493 len = instr->len;
1494 addr = instr->addr;
1495
1496 instr->state = MTD_ERASING;
1497
1498 while (len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001499 cond_resched();
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001500
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001501 /* Check if we have a bad block, we do not erase bad blocks */
1502 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1503 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1504 instr->state = MTD_ERASE_FAILED;
1505 goto erase_exit;
1506 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001507
1508 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1509
Adrian Hunter480b9df2007-02-07 13:55:19 +02001510 onenand_invalidate_bufferram(mtd, addr, block_size);
1511
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001512 ret = this->wait(mtd, FL_ERASING);
1513 /* Check, if it is write protected */
1514 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001515 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001516 instr->state = MTD_ERASE_FAILED;
1517 instr->fail_addr = addr;
1518 goto erase_exit;
1519 }
1520
1521 len -= block_size;
1522 addr += block_size;
1523 }
1524
1525 instr->state = MTD_ERASE_DONE;
1526
1527erase_exit:
1528
1529 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1530 /* Do call back function */
1531 if (!ret)
1532 mtd_erase_callback(instr);
1533
1534 /* Deselect and wake up anyone waiting on the device */
1535 onenand_release_device(mtd);
1536
1537 return ret;
1538}
1539
1540/**
1541 * onenand_sync - [MTD Interface] sync
1542 * @param mtd MTD device structure
1543 *
1544 * Sync is actually a wait for chip ready function
1545 */
1546static void onenand_sync(struct mtd_info *mtd)
1547{
1548 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1549
1550 /* Grab the lock and see if the device is available */
1551 onenand_get_device(mtd, FL_SYNCING);
1552
1553 /* Release it and go back */
1554 onenand_release_device(mtd);
1555}
1556
1557/**
1558 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1559 * @param mtd MTD device structure
1560 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001561 *
1562 * Check whether the block is bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001563 */
1564static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1565{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001566 /* Check for invalid offset */
1567 if (ofs > mtd->size)
1568 return -EINVAL;
1569
1570 return onenand_block_checkbad(mtd, ofs, 1, 0);
1571}
1572
1573/**
1574 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1575 * @param mtd MTD device structure
1576 * @param ofs offset from device start
1577 *
1578 * This is the default implementation, which can be overridden by
1579 * a hardware specific driver.
1580 */
1581static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1582{
1583 struct onenand_chip *this = mtd->priv;
1584 struct bbm_info *bbm = this->bbm;
1585 u_char buf[2] = {0, 0};
1586 size_t retlen;
1587 int block;
1588
1589 /* Get block number */
1590 block = ((int) ofs) >> bbm->bbt_erase_shift;
1591 if (bbm->bbt)
1592 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1593
1594 /* We write two bytes, so we dont have to mess with 16 bit access */
1595 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001596 return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001597}
1598
1599/**
1600 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1601 * @param mtd MTD device structure
1602 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001603 *
1604 * Mark the block as bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001605 */
1606static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1607{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001608 struct onenand_chip *this = mtd->priv;
1609 int ret;
1610
1611 ret = onenand_block_isbad(mtd, ofs);
1612 if (ret) {
1613 /* If it was bad already, return success and do nothing */
1614 if (ret > 0)
1615 return 0;
1616 return ret;
1617 }
1618
1619 return this->block_markbad(mtd, ofs);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001620}
1621
1622/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09001623 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001624 * @param mtd MTD device structure
1625 * @param ofs offset relative to mtd start
Kyungmin Park08f782b2006-11-16 11:29:39 +09001626 * @param len number of bytes to lock or unlock
Kyungmin Parke3da8062007-02-15 09:36:39 +09001627 * @param cmd lock or unlock command
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001628 *
Kyungmin Park08f782b2006-11-16 11:29:39 +09001629 * Lock or unlock one or more blocks
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001630 */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001631static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001632{
1633 struct onenand_chip *this = mtd->priv;
1634 int start, end, block, value, status;
Kyungmin Park08f782b2006-11-16 11:29:39 +09001635 int wp_status_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001636
1637 start = ofs >> this->erase_shift;
1638 end = len >> this->erase_shift;
1639
Kyungmin Park08f782b2006-11-16 11:29:39 +09001640 if (cmd == ONENAND_CMD_LOCK)
1641 wp_status_mask = ONENAND_WP_LS;
1642 else
1643 wp_status_mask = ONENAND_WP_US;
1644
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001645 /* Continuous lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001646 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001647 /* Set start block address */
1648 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1649 /* Set end block address */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001650 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001651 /* Write lock command */
1652 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001653
1654 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001655 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001656
1657 /* Sanity check */
1658 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1659 & ONENAND_CTRL_ONGO)
1660 continue;
1661
1662 /* Check lock status */
1663 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001664 if (!(status & wp_status_mask))
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001665 printk(KERN_ERR "wp status = 0x%x\n", status);
1666
1667 return 0;
1668 }
1669
1670 /* Block lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001671 for (block = start; block < start + end; block++) {
Kyungmin Park20ba89a2005-12-16 11:17:29 +09001672 /* Set block address */
1673 value = onenand_block_address(this, block);
1674 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1675 /* Select DataRAM for DDP */
1676 value = onenand_bufferram_address(this, block);
1677 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001678 /* Set start block address */
1679 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001680 /* Write lock command */
1681 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001682
1683 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001684 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001685
1686 /* Sanity check */
1687 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1688 & ONENAND_CTRL_ONGO)
1689 continue;
1690
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001691 /* Check lock status */
1692 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001693 if (!(status & wp_status_mask))
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001694 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1695 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001696
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001697 return 0;
1698}
1699
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001700/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09001701 * onenand_lock - [MTD Interface] Lock block(s)
1702 * @param mtd MTD device structure
1703 * @param ofs offset relative to mtd start
1704 * @param len number of bytes to unlock
1705 *
1706 * Lock one or more blocks
1707 */
1708static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1709{
1710 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1711}
1712
Kyungmin Park08f782b2006-11-16 11:29:39 +09001713/**
1714 * onenand_unlock - [MTD Interface] Unlock block(s)
1715 * @param mtd MTD device structure
1716 * @param ofs offset relative to mtd start
1717 * @param len number of bytes to unlock
1718 *
1719 * Unlock one or more blocks
1720 */
1721static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1722{
1723 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1724}
1725
1726/**
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001727 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1728 * @param this onenand chip data structure
1729 *
1730 * Check lock status
1731 */
1732static void onenand_check_lock_status(struct onenand_chip *this)
1733{
1734 unsigned int value, block, status;
1735 unsigned int end;
1736
1737 end = this->chipsize >> this->erase_shift;
1738 for (block = 0; block < end; block++) {
1739 /* Set block address */
1740 value = onenand_block_address(this, block);
1741 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1742 /* Select DataRAM for DDP */
1743 value = onenand_bufferram_address(this, block);
1744 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1745 /* Set start block address */
1746 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1747
1748 /* Check lock status */
1749 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1750 if (!(status & ONENAND_WP_US))
1751 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1752 }
1753}
1754
1755/**
1756 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1757 * @param mtd MTD device structure
1758 *
1759 * Unlock all blocks
1760 */
1761static int onenand_unlock_all(struct mtd_info *mtd)
1762{
1763 struct onenand_chip *this = mtd->priv;
1764
1765 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09001766 /* Set start block address */
1767 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001768 /* Write unlock command */
1769 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1770
1771 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001772 this->wait(mtd, FL_LOCKING);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001773
1774 /* Sanity check */
1775 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1776 & ONENAND_CTRL_ONGO)
1777 continue;
1778
1779 /* Workaround for all block unlock in DDP */
Kyungmin Park738d61f2007-01-15 17:09:14 +09001780 if (ONENAND_IS_DDP(this)) {
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001781 /* 1st block on another chip */
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09001782 loff_t ofs = this->chipsize >> 1;
1783 size_t len = mtd->erasesize;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001784
1785 onenand_unlock(mtd, ofs, len);
1786 }
1787
1788 onenand_check_lock_status(this);
1789
1790 return 0;
1791 }
1792
Kyungmin Park08f782b2006-11-16 11:29:39 +09001793 onenand_unlock(mtd, 0x0, this->chipsize);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001794
1795 return 0;
1796}
1797
Kyungmin Park493c6462006-05-12 17:03:07 +03001798#ifdef CONFIG_MTD_ONENAND_OTP
1799
1800/* Interal OTP operation */
1801typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1802 size_t *retlen, u_char *buf);
1803
1804/**
1805 * do_otp_read - [DEFAULT] Read OTP block area
1806 * @param mtd MTD device structure
1807 * @param from The offset to read
1808 * @param len number of bytes to read
1809 * @param retlen pointer to variable to store the number of readbytes
1810 * @param buf the databuffer to put/get data
1811 *
1812 * Read OTP block area.
1813 */
1814static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1815 size_t *retlen, u_char *buf)
1816{
1817 struct onenand_chip *this = mtd->priv;
1818 int ret;
1819
1820 /* Enter OTP access mode */
1821 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1822 this->wait(mtd, FL_OTPING);
1823
1824 ret = mtd->read(mtd, from, len, retlen, buf);
1825
1826 /* Exit OTP access mode */
1827 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1828 this->wait(mtd, FL_RESETING);
1829
1830 return ret;
1831}
1832
1833/**
1834 * do_otp_write - [DEFAULT] Write OTP block area
1835 * @param mtd MTD device structure
1836 * @param from The offset to write
1837 * @param len number of bytes to write
1838 * @param retlen pointer to variable to store the number of write bytes
1839 * @param buf the databuffer to put/get data
1840 *
1841 * Write OTP block area.
1842 */
1843static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1844 size_t *retlen, u_char *buf)
1845{
1846 struct onenand_chip *this = mtd->priv;
1847 unsigned char *pbuf = buf;
1848 int ret;
1849
1850 /* Force buffer page aligned */
Joern Engel28318772006-05-22 23:18:05 +02001851 if (len < mtd->writesize) {
Kyungmin Park493c6462006-05-12 17:03:07 +03001852 memcpy(this->page_buf, buf, len);
Joern Engel28318772006-05-22 23:18:05 +02001853 memset(this->page_buf + len, 0xff, mtd->writesize - len);
Kyungmin Park493c6462006-05-12 17:03:07 +03001854 pbuf = this->page_buf;
Joern Engel28318772006-05-22 23:18:05 +02001855 len = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03001856 }
1857
1858 /* Enter OTP access mode */
1859 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1860 this->wait(mtd, FL_OTPING);
1861
1862 ret = mtd->write(mtd, from, len, retlen, pbuf);
1863
1864 /* Exit OTP access mode */
1865 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1866 this->wait(mtd, FL_RESETING);
1867
1868 return ret;
1869}
1870
1871/**
1872 * do_otp_lock - [DEFAULT] Lock OTP block area
1873 * @param mtd MTD device structure
1874 * @param from The offset to lock
1875 * @param len number of bytes to lock
1876 * @param retlen pointer to variable to store the number of lock bytes
1877 * @param buf the databuffer to put/get data
1878 *
1879 * Lock OTP block area.
1880 */
1881static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1882 size_t *retlen, u_char *buf)
1883{
1884 struct onenand_chip *this = mtd->priv;
1885 int ret;
1886
1887 /* Enter OTP access mode */
1888 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1889 this->wait(mtd, FL_OTPING);
1890
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001891 ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
Kyungmin Park493c6462006-05-12 17:03:07 +03001892
1893 /* Exit OTP access mode */
1894 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1895 this->wait(mtd, FL_RESETING);
1896
1897 return ret;
1898}
1899
1900/**
1901 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1902 * @param mtd MTD device structure
1903 * @param from The offset to read/write
1904 * @param len number of bytes to read/write
1905 * @param retlen pointer to variable to store the number of read bytes
1906 * @param buf the databuffer to put/get data
1907 * @param action do given action
1908 * @param mode specify user and factory
1909 *
1910 * Handle OTP operation.
1911 */
1912static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1913 size_t *retlen, u_char *buf,
1914 otp_op_t action, int mode)
1915{
1916 struct onenand_chip *this = mtd->priv;
1917 int otp_pages;
1918 int density;
1919 int ret = 0;
1920
1921 *retlen = 0;
1922
1923 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1924 if (density < ONENAND_DEVICE_DENSITY_512Mb)
1925 otp_pages = 20;
1926 else
1927 otp_pages = 10;
1928
1929 if (mode == MTD_OTP_FACTORY) {
Joern Engel28318772006-05-22 23:18:05 +02001930 from += mtd->writesize * otp_pages;
Kyungmin Park493c6462006-05-12 17:03:07 +03001931 otp_pages = 64 - otp_pages;
1932 }
1933
1934 /* Check User/Factory boundary */
Joern Engel28318772006-05-22 23:18:05 +02001935 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
Kyungmin Park493c6462006-05-12 17:03:07 +03001936 return 0;
1937
1938 while (len > 0 && otp_pages > 0) {
1939 if (!action) { /* OTP Info functions */
1940 struct otp_info *otpinfo;
1941
1942 len -= sizeof(struct otp_info);
1943 if (len <= 0)
1944 return -ENOSPC;
1945
1946 otpinfo = (struct otp_info *) buf;
1947 otpinfo->start = from;
Joern Engel28318772006-05-22 23:18:05 +02001948 otpinfo->length = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03001949 otpinfo->locked = 0;
1950
Joern Engel28318772006-05-22 23:18:05 +02001951 from += mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03001952 buf += sizeof(struct otp_info);
1953 *retlen += sizeof(struct otp_info);
1954 } else {
1955 size_t tmp_retlen;
1956 int size = len;
1957
1958 ret = action(mtd, from, len, &tmp_retlen, buf);
1959
1960 buf += size;
1961 len -= size;
1962 *retlen += size;
1963
1964 if (ret < 0)
1965 return ret;
1966 }
1967 otp_pages--;
1968 }
1969
1970 return 0;
1971}
1972
1973/**
1974 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1975 * @param mtd MTD device structure
1976 * @param buf the databuffer to put/get data
1977 * @param len number of bytes to read
1978 *
1979 * Read factory OTP info.
1980 */
1981static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1982 struct otp_info *buf, size_t len)
1983{
1984 size_t retlen;
1985 int ret;
1986
1987 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1988
1989 return ret ? : retlen;
1990}
1991
1992/**
1993 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1994 * @param mtd MTD device structure
1995 * @param from The offset to read
1996 * @param len number of bytes to read
1997 * @param retlen pointer to variable to store the number of read bytes
1998 * @param buf the databuffer to put/get data
1999 *
2000 * Read factory OTP area.
2001 */
2002static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2003 size_t len, size_t *retlen, u_char *buf)
2004{
2005 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2006}
2007
2008/**
2009 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2010 * @param mtd MTD device structure
2011 * @param buf the databuffer to put/get data
2012 * @param len number of bytes to read
2013 *
2014 * Read user OTP info.
2015 */
2016static int onenand_get_user_prot_info(struct mtd_info *mtd,
2017 struct otp_info *buf, size_t len)
2018{
2019 size_t retlen;
2020 int ret;
2021
2022 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2023
2024 return ret ? : retlen;
2025}
2026
2027/**
2028 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2029 * @param mtd MTD device structure
2030 * @param from The offset to read
2031 * @param len number of bytes to read
2032 * @param retlen pointer to variable to store the number of read bytes
2033 * @param buf the databuffer to put/get data
2034 *
2035 * Read user OTP area.
2036 */
2037static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2038 size_t len, size_t *retlen, u_char *buf)
2039{
2040 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2041}
2042
2043/**
2044 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2045 * @param mtd MTD device structure
2046 * @param from The offset to write
2047 * @param len number of bytes to write
2048 * @param retlen pointer to variable to store the number of write bytes
2049 * @param buf the databuffer to put/get data
2050 *
2051 * Write user OTP area.
2052 */
2053static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2054 size_t len, size_t *retlen, u_char *buf)
2055{
2056 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2057}
2058
2059/**
2060 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2061 * @param mtd MTD device structure
2062 * @param from The offset to lock
2063 * @param len number of bytes to unlock
2064 *
2065 * Write lock mark on spare area in page 0 in OTP block
2066 */
2067static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2068 size_t len)
2069{
2070 unsigned char oob_buf[64];
2071 size_t retlen;
2072 int ret;
2073
2074 memset(oob_buf, 0xff, mtd->oobsize);
2075 /*
2076 * Note: OTP lock operation
2077 * OTP block : 0xXXFC
2078 * 1st block : 0xXXF3 (If chip support)
2079 * Both : 0xXXF0 (If chip support)
2080 */
2081 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2082
2083 /*
2084 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2085 * We write 16 bytes spare area instead of 2 bytes.
2086 */
2087 from = 0;
2088 len = 16;
2089
2090 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2091
2092 return ret ? : retlen;
2093}
2094#endif /* CONFIG_MTD_ONENAND_OTP */
2095
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002096/**
Kyungmin Park75384b02007-01-18 11:10:57 +09002097 * onenand_check_features - Check and set OneNAND features
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002098 * @param mtd MTD data structure
2099 *
Kyungmin Park75384b02007-01-18 11:10:57 +09002100 * Check and set OneNAND features
2101 * - lock scheme
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002102 */
Kyungmin Park75384b02007-01-18 11:10:57 +09002103static void onenand_check_features(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002104{
2105 struct onenand_chip *this = mtd->priv;
2106 unsigned int density, process;
2107
2108 /* Lock scheme depends on density and process */
2109 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2110 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2111
2112 /* Lock scheme */
2113 if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
2114 /* A-Die has all block unlock */
2115 if (process) {
2116 printk(KERN_DEBUG "Chip support all block unlock\n");
2117 this->options |= ONENAND_HAS_UNLOCK_ALL;
2118 }
2119 } else {
2120 /* Some OneNAND has continues lock scheme */
2121 if (!process) {
2122 printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
2123 this->options |= ONENAND_HAS_CONT_LOCK;
2124 }
2125 }
2126}
2127
2128/**
Kyungmin Parke3da8062007-02-15 09:36:39 +09002129 * onenand_print_device_info - Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002130 * @param device device ID
Kyungmin Parke3da8062007-02-15 09:36:39 +09002131 * @param version version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002132 *
Kyungmin Parke3da8062007-02-15 09:36:39 +09002133 * Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002134 */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002135static void onenand_print_device_info(int device, int version)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002136{
2137 int vcc, demuxed, ddp, density;
2138
2139 vcc = device & ONENAND_DEVICE_VCC_MASK;
2140 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2141 ddp = device & ONENAND_DEVICE_IS_DDP;
2142 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2143 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2144 demuxed ? "" : "Muxed ",
2145 ddp ? "(DDP)" : "",
2146 (16 << density),
2147 vcc ? "2.65/3.3" : "1.8",
2148 device);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002149 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002150}
2151
2152static const struct onenand_manufacturers onenand_manuf_ids[] = {
2153 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002154};
2155
2156/**
2157 * onenand_check_maf - Check manufacturer ID
2158 * @param manuf manufacturer ID
2159 *
2160 * Check manufacturer ID
2161 */
2162static int onenand_check_maf(int manuf)
2163{
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002164 int size = ARRAY_SIZE(onenand_manuf_ids);
2165 char *name;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002166 int i;
2167
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002168 for (i = 0; i < size; i++)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002169 if (manuf == onenand_manuf_ids[i].id)
2170 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002171
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002172 if (i < size)
2173 name = onenand_manuf_ids[i].name;
2174 else
2175 name = "Unknown";
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002176
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002177 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2178
2179 return (i == size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002180}
2181
2182/**
2183 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2184 * @param mtd MTD device structure
2185 *
2186 * OneNAND detection method:
2187 * Compare the the values from command with ones from register
2188 */
2189static int onenand_probe(struct mtd_info *mtd)
2190{
2191 struct onenand_chip *this = mtd->priv;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002192 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002193 int density;
Kyungmin Park47e777e2006-09-25 23:53:28 +00002194 int syscfg;
2195
2196 /* Save system configuration 1 */
2197 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2198 /* Clear Sync. Burst Read mode to read BootRAM */
2199 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002200
2201 /* Send the command for reading device ID from BootRAM */
2202 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2203
2204 /* Read manufacturer and device IDs from BootRAM */
2205 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2206 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2207
Kyungmin Park47e777e2006-09-25 23:53:28 +00002208 /* Reset OneNAND to read default register values */
2209 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2210 /* Wait reset */
2211 this->wait(mtd, FL_RESETING);
2212
2213 /* Restore system configuration 1 */
2214 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2215
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002216 /* Check manufacturer ID */
2217 if (onenand_check_maf(bram_maf_id))
2218 return -ENXIO;
2219
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002220 /* Read manufacturer and device IDs from Register */
2221 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2222 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09002223 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002224
2225 /* Check OneNAND device */
2226 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2227 return -ENXIO;
2228
2229 /* Flash device information */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002230 onenand_print_device_info(dev_id, ver_id);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002231 this->device_id = dev_id;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002232 this->version_id = ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002233
2234 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2235 this->chipsize = (16 << density) << 20;
Kyungmin Park83a36832005-09-29 04:53:16 +01002236 /* Set density mask. it is used for DDP */
Kyungmin Park738d61f2007-01-15 17:09:14 +09002237 if (ONENAND_IS_DDP(this))
2238 this->density_mask = (1 << (density + 6));
2239 else
2240 this->density_mask = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002241
2242 /* OneNAND page size & block size */
2243 /* The data buffer size is equal to page size */
Joern Engel28318772006-05-22 23:18:05 +02002244 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2245 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09002246 /* Pages per a block are always 64 in OneNAND */
Joern Engel28318772006-05-22 23:18:05 +02002247 mtd->erasesize = mtd->writesize << 6;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002248
2249 this->erase_shift = ffs(mtd->erasesize) - 1;
Joern Engel28318772006-05-22 23:18:05 +02002250 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09002251 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002252
2253 /* REVIST: Multichip handling */
2254
2255 mtd->size = this->chipsize;
2256
Kyungmin Park75384b02007-01-18 11:10:57 +09002257 /* Check OneNAND features */
2258 onenand_check_features(mtd);
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002259
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002260 return 0;
2261}
2262
Kyungmin Parka41371e2005-09-29 03:55:31 +01002263/**
2264 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2265 * @param mtd MTD device structure
2266 */
2267static int onenand_suspend(struct mtd_info *mtd)
2268{
2269 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2270}
2271
2272/**
2273 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2274 * @param mtd MTD device structure
2275 */
2276static void onenand_resume(struct mtd_info *mtd)
2277{
2278 struct onenand_chip *this = mtd->priv;
2279
2280 if (this->state == FL_PM_SUSPENDED)
2281 onenand_release_device(mtd);
2282 else
2283 printk(KERN_ERR "resume() called for the chip which is not"
2284 "in suspended state\n");
2285}
2286
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002287/**
2288 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2289 * @param mtd MTD device structure
2290 * @param maxchips Number of chips to scan for
2291 *
2292 * This fills out all the not initialized function pointers
2293 * with the defaults.
2294 * The flash ID is read and the mtd/chip structures are
2295 * filled with the appropriate values.
2296 */
2297int onenand_scan(struct mtd_info *mtd, int maxchips)
2298{
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002299 int i;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002300 struct onenand_chip *this = mtd->priv;
2301
2302 if (!this->read_word)
2303 this->read_word = onenand_readw;
2304 if (!this->write_word)
2305 this->write_word = onenand_writew;
2306
2307 if (!this->command)
2308 this->command = onenand_command;
2309 if (!this->wait)
Kyungmin Park2c221202006-11-16 11:23:48 +09002310 onenand_setup_wait(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002311
2312 if (!this->read_bufferram)
2313 this->read_bufferram = onenand_read_bufferram;
2314 if (!this->write_bufferram)
2315 this->write_bufferram = onenand_write_bufferram;
2316
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002317 if (!this->block_markbad)
2318 this->block_markbad = onenand_default_block_markbad;
2319 if (!this->scan_bbt)
2320 this->scan_bbt = onenand_default_bbt;
2321
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002322 if (onenand_probe(mtd))
2323 return -ENXIO;
2324
Kyungmin Park52b0eea2005-09-03 07:07:19 +01002325 /* Set Sync. Burst Read after probing */
2326 if (this->mmcontrol) {
2327 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2328 this->read_bufferram = onenand_sync_read_bufferram;
2329 }
2330
Kyungmin Park532a37c2005-12-16 11:17:29 +09002331 /* Allocate buffers, if necessary */
2332 if (!this->page_buf) {
2333 size_t len;
Joern Engel28318772006-05-22 23:18:05 +02002334 len = mtd->writesize + mtd->oobsize;
Kyungmin Park532a37c2005-12-16 11:17:29 +09002335 this->page_buf = kmalloc(len, GFP_KERNEL);
2336 if (!this->page_buf) {
2337 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2338 return -ENOMEM;
2339 }
2340 this->options |= ONENAND_PAGEBUF_ALLOC;
2341 }
2342
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002343 this->state = FL_READY;
2344 init_waitqueue_head(&this->wq);
2345 spin_lock_init(&this->chip_lock);
2346
Kyungmin Park60d84f92006-12-22 16:21:54 +09002347 /*
2348 * Allow subpage writes up to oobsize.
2349 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002350 switch (mtd->oobsize) {
2351 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002352 this->ecclayout = &onenand_oob_64;
Kyungmin Park60d84f92006-12-22 16:21:54 +09002353 mtd->subpage_sft = 2;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002354 break;
2355
2356 case 32:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002357 this->ecclayout = &onenand_oob_32;
Kyungmin Park60d84f92006-12-22 16:21:54 +09002358 mtd->subpage_sft = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002359 break;
2360
2361 default:
2362 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2363 mtd->oobsize);
Kyungmin Park60d84f92006-12-22 16:21:54 +09002364 mtd->subpage_sft = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002365 /* To prevent kernel oops */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002366 this->ecclayout = &onenand_oob_32;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002367 break;
2368 }
2369
Kyungmin Park60d84f92006-12-22 16:21:54 +09002370 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002371
2372 /*
2373 * The number of bytes available for a client to place data into
2374 * the out of band area
2375 */
2376 this->ecclayout->oobavail = 0;
2377 for (i = 0; this->ecclayout->oobfree[i].length; i++)
2378 this->ecclayout->oobavail +=
2379 this->ecclayout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002380 mtd->oobavail = this->ecclayout->oobavail;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002381
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002382 mtd->ecclayout = this->ecclayout;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002383
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002384 /* Fill in remaining MTD driver data */
2385 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002386 mtd->flags = MTD_CAP_NANDFLASH;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002387 mtd->erase = onenand_erase;
2388 mtd->point = NULL;
2389 mtd->unpoint = NULL;
2390 mtd->read = onenand_read;
2391 mtd->write = onenand_write;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002392 mtd->read_oob = onenand_read_oob;
2393 mtd->write_oob = onenand_write_oob;
Kyungmin Park493c6462006-05-12 17:03:07 +03002394#ifdef CONFIG_MTD_ONENAND_OTP
2395 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2396 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2397 mtd->get_user_prot_info = onenand_get_user_prot_info;
2398 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2399 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2400 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2401#endif
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002402 mtd->sync = onenand_sync;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002403 mtd->lock = onenand_lock;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002404 mtd->unlock = onenand_unlock;
Kyungmin Parka41371e2005-09-29 03:55:31 +01002405 mtd->suspend = onenand_suspend;
2406 mtd->resume = onenand_resume;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002407 mtd->block_isbad = onenand_block_isbad;
2408 mtd->block_markbad = onenand_block_markbad;
2409 mtd->owner = THIS_MODULE;
2410
2411 /* Unlock whole block */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002412 onenand_unlock_all(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002413
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002414 return this->scan_bbt(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002415}
2416
2417/**
2418 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2419 * @param mtd MTD device structure
2420 */
2421void onenand_release(struct mtd_info *mtd)
2422{
Kyungmin Park532a37c2005-12-16 11:17:29 +09002423 struct onenand_chip *this = mtd->priv;
2424
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002425#ifdef CONFIG_MTD_PARTITIONS
2426 /* Deregister partitions */
2427 del_mtd_partitions (mtd);
2428#endif
2429 /* Deregister the device */
2430 del_mtd_device (mtd);
Kyungmin Park532a37c2005-12-16 11:17:29 +09002431
2432 /* Free bad block table memory, if allocated */
Adrian Hunterf00b0042007-01-22 17:01:01 +09002433 if (this->bbm) {
2434 struct bbm_info *bbm = this->bbm;
2435 kfree(bbm->bbt);
Kyungmin Park532a37c2005-12-16 11:17:29 +09002436 kfree(this->bbm);
Adrian Hunterf00b0042007-01-22 17:01:01 +09002437 }
Kyungmin Park532a37c2005-12-16 11:17:29 +09002438 /* Buffer allocated by onenand_scan */
2439 if (this->options & ONENAND_PAGEBUF_ALLOC)
2440 kfree(this->page_buf);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002441}
2442
2443EXPORT_SYMBOL_GPL(onenand_scan);
2444EXPORT_SYMBOL_GPL(onenand_release);
2445
2446MODULE_LICENSE("GPL");
2447MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2448MODULE_DESCRIPTION("Generic OneNAND flash driver code");