blob: f62b1aa25a64d3d04311d342a021d2f40e90baea [file] [log] [blame]
Kyungmin Park916527f2007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +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 Park916527f2007-09-10 17:13:49 +090012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <common.h>
Kyungmin Park916527f2007-09-10 17:13:49 +090018#include <linux/mtd/compat.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/onenand.h>
21
22#include <asm/io.h>
23#include <asm/errno.h>
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +020024#include <malloc.h>
Kyungmin Park916527f2007-09-10 17:13:49 +090025
Kyungmin Park77e475c2008-03-31 10:40:36 +090026/* It should access 16-bit instead of 8-bit */
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +020027static inline void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park77e475c2008-03-31 10:40:36 +090028{
29 void *ret = dst;
30 short *d = dst;
31 const short *s = src;
32
33 len >>= 1;
34 while (len-- > 0)
35 *d++ = *s++;
36 return ret;
37}
38
Kyungmin Park916527f2007-09-10 17:13:49 +090039static const unsigned char ffchars[] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
48};
49
50/**
51 * onenand_readw - [OneNAND Interface] Read OneNAND register
52 * @param addr address to read
53 *
54 * Read OneNAND register
55 */
56static unsigned short onenand_readw(void __iomem * addr)
57{
58 return readw(addr);
59}
60
61/**
62 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
63 * @param value value to write
64 * @param addr address to write
65 *
66 * Write OneNAND register with value
67 */
68static void onenand_writew(unsigned short value, void __iomem * addr)
69{
70 writew(value, addr);
71}
72
73/**
74 * onenand_block_address - [DEFAULT] Get block address
75 * @param device the device id
76 * @param block the block
77 * @return translated block address if DDP, otherwise same
78 *
79 * Setup Start Address 1 Register (F100h)
80 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +090081static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park916527f2007-09-10 17:13:49 +090082{
Kyungmin Parkef0921d2008-11-04 09:24:07 +090083 /* Device Flash Core select, NAND Flash Block Address */
84 if (block & this->density_mask)
85 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park916527f2007-09-10 17:13:49 +090086
87 return block;
88}
89
90/**
91 * onenand_bufferram_address - [DEFAULT] Get bufferram address
92 * @param device the device id
93 * @param block the block
94 * @return set DBS value if DDP, otherwise 0
95 *
96 * Setup Start Address 2 Register (F101h) for DDP
97 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +090098static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park916527f2007-09-10 17:13:49 +090099{
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900100 /* Device BufferRAM Select */
101 if (block & this->density_mask)
102 return ONENAND_DDP_CHIP1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900103
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900104 return ONENAND_DDP_CHIP0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900105}
106
107/**
108 * onenand_page_address - [DEFAULT] Get page address
109 * @param page the page address
110 * @param sector the sector address
111 * @return combined page and sector address
112 *
113 * Setup Start Address 8 Register (F107h)
114 */
115static int onenand_page_address(int page, int sector)
116{
117 /* Flash Page Address, Flash Sector Address */
118 int fpa, fsa;
119
120 fpa = page & ONENAND_FPA_MASK;
121 fsa = sector & ONENAND_FSA_MASK;
122
123 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
124}
125
126/**
127 * onenand_buffer_address - [DEFAULT] Get buffer address
128 * @param dataram1 DataRAM index
129 * @param sectors the sector address
130 * @param count the number of sectors
131 * @return the start buffer value
132 *
133 * Setup Start Buffer Register (F200h)
134 */
135static int onenand_buffer_address(int dataram1, int sectors, int count)
136{
137 int bsa, bsc;
138
139 /* BufferRAM Sector Address */
140 bsa = sectors & ONENAND_BSA_MASK;
141
142 if (dataram1)
143 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
144 else
145 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
146
147 /* BufferRAM Sector Count */
148 bsc = count & ONENAND_BSC_MASK;
149
150 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
151}
152
153/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900154 * onenand_get_density - [DEFAULT] Get OneNAND density
155 * @param dev_id OneNAND device ID
156 *
157 * Get OneNAND density from device ID
158 */
159static inline int onenand_get_density(int dev_id)
160{
161 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
162 return (density & ONENAND_DEVICE_DENSITY_MASK);
163}
164
165/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900166 * onenand_command - [DEFAULT] Send command to OneNAND device
167 * @param mtd MTD device structure
168 * @param cmd the command to be sent
169 * @param addr offset to read from or write to
170 * @param len number of bytes to read or write
171 *
172 * Send command to OneNAND device. This function is used for middle/large page
173 * devices (1KB/2KB Bytes per page)
174 */
175static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
176 size_t len)
177{
178 struct onenand_chip *this = mtd->priv;
179 int value, readcmd = 0;
180 int block, page;
181 /* Now we use page size operation */
182 int sectors = 4, count = 4;
183
184 /* Address translation */
185 switch (cmd) {
186 case ONENAND_CMD_UNLOCK:
187 case ONENAND_CMD_LOCK:
188 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900189 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park916527f2007-09-10 17:13:49 +0900190 block = -1;
191 page = -1;
192 break;
193
194 case ONENAND_CMD_ERASE:
195 case ONENAND_CMD_BUFFERRAM:
196 block = (int)(addr >> this->erase_shift);
197 page = -1;
198 break;
199
200 default:
201 block = (int)(addr >> this->erase_shift);
202 page = (int)(addr >> this->page_shift);
203 page &= this->page_mask;
204 break;
205 }
206
207 /* NOTE: The setting order of the registers is very important! */
208 if (cmd == ONENAND_CMD_BUFFERRAM) {
209 /* Select DataRAM for DDP */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900210 value = onenand_bufferram_address(this, block);
Kyungmin Park916527f2007-09-10 17:13:49 +0900211 this->write_word(value,
212 this->base + ONENAND_REG_START_ADDRESS2);
213
214 /* Switch to the next data buffer */
215 ONENAND_SET_NEXT_BUFFERRAM(this);
216
217 return 0;
218 }
219
220 if (block != -1) {
221 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900222 value = onenand_block_address(this, block);
Kyungmin Park916527f2007-09-10 17:13:49 +0900223 this->write_word(value,
224 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900225
226 /* Write 'DFS, FBA' of Flash */
227 value = onenand_bufferram_address(this, block);
228 this->write_word(value,
229 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park916527f2007-09-10 17:13:49 +0900230 }
231
232 if (page != -1) {
233 int dataram;
234
235 switch (cmd) {
236 case ONENAND_CMD_READ:
237 case ONENAND_CMD_READOOB:
238 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
239 readcmd = 1;
240 break;
241
242 default:
243 dataram = ONENAND_CURRENT_BUFFERRAM(this);
244 break;
245 }
246
247 /* Write 'FPA, FSA' of Flash */
248 value = onenand_page_address(page, sectors);
249 this->write_word(value,
250 this->base + ONENAND_REG_START_ADDRESS8);
251
252 /* Write 'BSA, BSC' of DataRAM */
253 value = onenand_buffer_address(dataram, sectors, count);
254 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park916527f2007-09-10 17:13:49 +0900255 }
256
257 /* Interrupt clear */
258 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
259 /* Write command */
260 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
261
262 return 0;
263}
264
265/**
266 * onenand_wait - [DEFAULT] wait until the command is done
267 * @param mtd MTD device structure
268 * @param state state to select the max. timeout value
269 *
270 * Wait for command done. This applies to all OneNAND command
271 * Read can take up to 30us, erase up to 2ms and program up to 350us
272 * according to general OneNAND specs
273 */
274static int onenand_wait(struct mtd_info *mtd, int state)
275{
276 struct onenand_chip *this = mtd->priv;
277 unsigned int flags = ONENAND_INT_MASTER;
278 unsigned int interrupt = 0;
279 unsigned int ctrl, ecc;
280
281 while (1) {
282 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
283 if (interrupt & flags)
284 break;
285 }
286
287 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
288
289 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900290 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
291 if (ctrl & ONENAND_CTRL_LOCK)
292 printk("onenand_wait: it's locked error = 0x%04x\n",
293 ctrl);
Kyungmin Park916527f2007-09-10 17:13:49 +0900294
Kyungmin Park916527f2007-09-10 17:13:49 +0900295 return -EIO;
296 }
297
298 if (interrupt & ONENAND_INT_READ) {
299 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
300 if (ecc & ONENAND_ECC_2BIT_ALL) {
Scott Wood3167c532008-06-20 12:38:57 -0500301 MTDDEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200302 "onenand_wait: ECC error = 0x%04x\n", ecc);
Kyungmin Park916527f2007-09-10 17:13:49 +0900303 return -EBADMSG;
304 }
305 }
306
307 return 0;
308}
309
310/**
311 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
312 * @param mtd MTD data structure
313 * @param area BufferRAM area
314 * @return offset given area
315 *
316 * Return BufferRAM offset given area
317 */
318static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
319{
320 struct onenand_chip *this = mtd->priv;
321
322 if (ONENAND_CURRENT_BUFFERRAM(this)) {
323 if (area == ONENAND_DATARAM)
Kyungmin Parkd438d502008-08-13 09:11:02 +0900324 return mtd->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +0900325 if (area == ONENAND_SPARERAM)
326 return mtd->oobsize;
327 }
328
329 return 0;
330}
331
332/**
333 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
334 * @param mtd MTD data structure
335 * @param area BufferRAM area
336 * @param buffer the databuffer to put/get data
337 * @param offset offset to read from or write to
338 * @param count number of bytes to read/write
339 *
340 * Read the BufferRAM area
341 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900342static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900343 unsigned char *buffer, int offset,
344 size_t count)
345{
346 struct onenand_chip *this = mtd->priv;
347 void __iomem *bufferram;
348
349 bufferram = this->base + area;
350 bufferram += onenand_bufferram_offset(mtd, area);
351
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200352 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900353
354 return 0;
355}
356
357/**
358 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
359 * @param mtd MTD data structure
360 * @param area BufferRAM area
361 * @param buffer the databuffer to put/get data
362 * @param offset offset to read from or write to
363 * @param count number of bytes to read/write
364 *
365 * Read the BufferRAM area with Sync. Burst Mode
366 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900367static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900368 unsigned char *buffer, int offset,
369 size_t count)
370{
371 struct onenand_chip *this = mtd->priv;
372 void __iomem *bufferram;
373
374 bufferram = this->base + area;
375 bufferram += onenand_bufferram_offset(mtd, area);
376
377 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
378
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200379 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900380
381 this->mmcontrol(mtd, 0);
382
383 return 0;
384}
385
386/**
387 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
388 * @param mtd MTD data structure
389 * @param area BufferRAM area
390 * @param buffer the databuffer to put/get data
391 * @param offset offset to read from or write to
392 * @param count number of bytes to read/write
393 *
394 * Write the BufferRAM area
395 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900396static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900397 const unsigned char *buffer, int offset,
398 size_t count)
399{
400 struct onenand_chip *this = mtd->priv;
401 void __iomem *bufferram;
402
403 bufferram = this->base + area;
404 bufferram += onenand_bufferram_offset(mtd, area);
405
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200406 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900407
408 return 0;
409}
410
411/**
Stefan Roese4fca3312008-11-11 10:28:53 +0100412 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
413 * @param mtd MTD data structure
414 * @param addr address to check
415 * @return blockpage address
416 *
417 * Get blockpage address at 2x program mode
418 */
419static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
420{
421 struct onenand_chip *this = mtd->priv;
422 int blockpage, block, page;
423
424 /* Calculate the even block number */
425 block = (int) (addr >> this->erase_shift) & ~1;
426 /* Is it the odd plane? */
427 if (addr & this->writesize)
428 block++;
429 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
430 blockpage = (block << 7) | page;
431
432 return blockpage;
433}
434
435/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900436 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
437 * @param mtd MTD data structure
438 * @param addr address to check
439 * @return 1 if there are valid data, otherwise 0
440 *
441 * Check bufferram if there is data we required
442 */
443static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
444{
445 struct onenand_chip *this = mtd->priv;
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900446 int blockpage, found = 0;
447 unsigned int i;
Kyungmin Park916527f2007-09-10 17:13:49 +0900448
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900449#ifdef CONFIG_S3C64XX
450 return 0;
451#endif
Kyungmin Park916527f2007-09-10 17:13:49 +0900452
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900453 if (ONENAND_IS_2PLANE(this))
454 blockpage = onenand_get_2x_blockpage(mtd, addr);
455 else
456 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park916527f2007-09-10 17:13:49 +0900457
458 /* Is there valid data? */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900459 i = ONENAND_CURRENT_BUFFERRAM(this);
460 if (this->bufferram[i].blockpage == blockpage)
461 found = 1;
462 else {
463 /* Check another BufferRAM */
464 i = ONENAND_NEXT_BUFFERRAM(this);
465 if (this->bufferram[i].blockpage == blockpage) {
466 ONENAND_SET_NEXT_BUFFERRAM(this);
467 found = 1;
468 }
469 }
Kyungmin Park916527f2007-09-10 17:13:49 +0900470
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900471 if (found && ONENAND_IS_DDP(this)) {
472 /* Select DataRAM for DDP */
473 int block = (int) (addr >> this->erase_shift);
474 int value = onenand_bufferram_address(this, block);
475 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
476 }
477
478 return found;
Kyungmin Park916527f2007-09-10 17:13:49 +0900479}
480
481/**
482 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
483 * @param mtd MTD data structure
484 * @param addr address to update
485 * @param valid valid flag
486 *
487 * Update BufferRAM information
488 */
489static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
490 int valid)
491{
492 struct onenand_chip *this = mtd->priv;
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900493 int blockpage;
494 unsigned int i;
Kyungmin Park916527f2007-09-10 17:13:49 +0900495
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900496 if (ONENAND_IS_2PLANE(this))
497 blockpage = onenand_get_2x_blockpage(mtd, addr);
498 else
499 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park916527f2007-09-10 17:13:49 +0900500
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900501 /* Invalidate another BufferRAM */
502 i = ONENAND_NEXT_BUFFERRAM(this);
503 if (this->bufferram[i].blockpage == blockpage)
504 this->bufferram[i].blockpage = -1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900505
506 /* Update BufferRAM */
507 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900508 if (valid)
509 this->bufferram[i].blockpage = blockpage;
510 else
511 this->bufferram[i].blockpage = -1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900512
513 return 0;
514}
515
516/**
Kyungmin Parkd438d502008-08-13 09:11:02 +0900517 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
518 * @param mtd MTD data structure
519 * @param addr start address to invalidate
520 * @param len length to invalidate
521 *
522 * Invalidate BufferRAM information
523 */
524static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200525 unsigned int len)
Kyungmin Parkd438d502008-08-13 09:11:02 +0900526{
527 struct onenand_chip *this = mtd->priv;
528 int i;
529 loff_t end_addr = addr + len;
530
531 /* Invalidate BufferRAM */
532 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900533 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Parkd438d502008-08-13 09:11:02 +0900534
535 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900536 this->bufferram[i].blockpage = -1;
Kyungmin Parkd438d502008-08-13 09:11:02 +0900537 }
538}
539
540/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900541 * onenand_get_device - [GENERIC] Get chip for selected access
542 * @param mtd MTD device structure
543 * @param new_state the state which is requested
544 *
545 * Get the device and lock it for exclusive access
546 */
547static void onenand_get_device(struct mtd_info *mtd, int new_state)
548{
549 /* Do nothing */
550}
551
552/**
553 * onenand_release_device - [GENERIC] release chip
554 * @param mtd MTD device structure
555 *
556 * Deselect, release chip lock and wake up anyone waiting on the device
557 */
558static void onenand_release_device(struct mtd_info *mtd)
559{
560 /* Do nothing */
561}
562
563/**
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900564 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
Kyungmin Park916527f2007-09-10 17:13:49 +0900565 * @param mtd MTD device structure
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900566 * @param buf destination address
567 * @param column oob offset to read from
568 * @param thislen oob length to read
Kyungmin Park916527f2007-09-10 17:13:49 +0900569 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900570static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
571 int column, int thislen)
Kyungmin Park916527f2007-09-10 17:13:49 +0900572{
573 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900574 struct nand_oobfree *free;
575 int readcol = column;
576 int readend = column + thislen;
577 int lastgap = 0;
578 unsigned int i;
579 uint8_t *oob_buf = this->oob_buf;
Kyungmin Park916527f2007-09-10 17:13:49 +0900580
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900581 free = this->ecclayout->oobfree;
582 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
583 if (readcol >= lastgap)
584 readcol += free->offset - lastgap;
585 if (readend >= lastgap)
586 readend += free->offset - lastgap;
587 lastgap = free->offset + free->length;
588 }
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900589 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900590 free = this->ecclayout->oobfree;
591 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
592 int free_end = free->offset + free->length;
593 if (free->offset < readend && free_end > readcol) {
594 int st = max_t(int,free->offset,readcol);
595 int ed = min_t(int,free_end,readend);
596 int n = ed - st;
597 memcpy(buf, oob_buf + st, n);
598 buf += n;
599 } else if (column == 0)
600 break;
601 }
602 return 0;
603}
604
605/**
606 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
607 * @param mtd MTD device structure
608 * @param from offset to read from
609 * @param ops oob operation description structure
610 *
611 * OneNAND read main and/or out-of-band data
612 */
613static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
614 struct mtd_oob_ops *ops)
615{
616 struct onenand_chip *this = mtd->priv;
617 struct mtd_ecc_stats stats;
618 size_t len = ops->len;
619 size_t ooblen = ops->ooblen;
620 u_char *buf = ops->datbuf;
621 u_char *oobbuf = ops->oobbuf;
622 int read = 0, column, thislen;
623 int oobread = 0, oobcolumn, thisooblen, oobsize;
624 int ret = 0, boundary = 0;
625 int writesize = this->writesize;
626
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900627 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900628
629 if (ops->mode == MTD_OOB_AUTO)
630 oobsize = this->ecclayout->oobavail;
631 else
632 oobsize = mtd->oobsize;
633
634 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park916527f2007-09-10 17:13:49 +0900635
636 /* Do not allow reads past end of device */
637 if ((from + len) > mtd->size) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900638 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
639 ops->retlen = 0;
640 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900641 return -EINVAL;
642 }
643
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900644 stats = mtd->ecc_stats;
Kyungmin Park916527f2007-09-10 17:13:49 +0900645
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900646 /* Read-while-load method */
Kyungmin Park916527f2007-09-10 17:13:49 +0900647
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900648 /* Do first load to bufferRAM */
649 if (read < len) {
Kyungmin Park916527f2007-09-10 17:13:49 +0900650 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900651 this->main_buf = buf;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900652 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +0900653 ret = this->wait(mtd, FL_READING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900654 onenand_update_bufferram(mtd, from, !ret);
655 if (ret == -EBADMSG)
656 ret = 0;
657 }
658 }
659
660 thislen = min_t(int, writesize, len - read);
661 column = from & (writesize - 1);
662 if (column + thislen > writesize)
663 thislen = writesize - column;
664
665 while (!ret) {
666 /* If there is more to load then start next load */
667 from += thislen;
668 if (read + thislen < len) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900669 this->main_buf = buf + thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900670 this->command(mtd, ONENAND_CMD_READ, from, writesize);
671 /*
672 * Chip boundary handling in DDP
673 * Now we issued chip 1 read and pointed chip 1
674 * bufferam so we have to point chip 0 bufferam.
675 */
676 if (ONENAND_IS_DDP(this) &&
677 unlikely(from == (this->chipsize >> 1))) {
678 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
679 boundary = 1;
680 } else
681 boundary = 0;
682 ONENAND_SET_PREV_BUFFERRAM(this);
Kyungmin Park916527f2007-09-10 17:13:49 +0900683 }
684
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900685 /* While load is going, read from last bufferRAM */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900686 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park916527f2007-09-10 17:13:49 +0900687
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900688 /* Read oob area if needed */
689 if (oobbuf) {
690 thisooblen = oobsize - oobcolumn;
691 thisooblen = min_t(int, thisooblen, ooblen - oobread);
692
693 if (ops->mode == MTD_OOB_AUTO)
694 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
695 else
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900696 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900697 oobread += thisooblen;
698 oobbuf += thisooblen;
699 oobcolumn = 0;
700 }
701
702 /* See if we are done */
Kyungmin Park916527f2007-09-10 17:13:49 +0900703 read += thislen;
704 if (read == len)
705 break;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900706 /* Set up for next read from bufferRAM */
707 if (unlikely(boundary))
708 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
709 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park916527f2007-09-10 17:13:49 +0900710 buf += thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900711 thislen = min_t(int, writesize, len - read);
712 column = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900713
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900714 /* Now wait for load */
715 ret = this->wait(mtd, FL_READING);
716 onenand_update_bufferram(mtd, from, !ret);
717 if (ret == -EBADMSG)
718 ret = 0;
719 }
Kyungmin Park916527f2007-09-10 17:13:49 +0900720
721 /*
722 * Return success, if no ECC failures, else -EBADMSG
723 * fs driver will take care of that, because
724 * retlen == desired len and result == -EBADMSG
725 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900726 ops->retlen = read;
727 ops->oobretlen = oobread;
728
729 if (ret)
730 return ret;
731
732 if (mtd->ecc_stats.failed - stats.failed)
733 return -EBADMSG;
734
735 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
736}
737
738/**
739 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
740 * @param mtd MTD device structure
741 * @param from offset to read from
742 * @param ops oob operation description structure
743 *
744 * OneNAND read out-of-band data from the spare area
745 */
746static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
747 struct mtd_oob_ops *ops)
748{
749 struct onenand_chip *this = mtd->priv;
750 struct mtd_ecc_stats stats;
751 int read = 0, thislen, column, oobsize;
752 size_t len = ops->ooblen;
753 mtd_oob_mode_t mode = ops->mode;
754 u_char *buf = ops->oobbuf;
755 int ret = 0;
756
757 from += ops->ooboffs;
758
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900759 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900760
761 /* Initialize return length value */
762 ops->oobretlen = 0;
763
764 if (mode == MTD_OOB_AUTO)
765 oobsize = this->ecclayout->oobavail;
766 else
767 oobsize = mtd->oobsize;
768
769 column = from & (mtd->oobsize - 1);
770
771 if (unlikely(column >= oobsize)) {
772 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
773 return -EINVAL;
774 }
775
776 /* Do not allow reads past end of device */
777 if (unlikely(from >= mtd->size ||
778 column + len > ((mtd->size >> this->page_shift) -
779 (from >> this->page_shift)) * oobsize)) {
780 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
781 return -EINVAL;
782 }
783
784 stats = mtd->ecc_stats;
785
786 while (read < len) {
787 thislen = oobsize - column;
788 thislen = min_t(int, thislen, len);
789
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900790 this->spare_buf = buf;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900791 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
792
793 onenand_update_bufferram(mtd, from, 0);
794
795 ret = this->wait(mtd, FL_READING);
796 if (ret && ret != -EBADMSG) {
797 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
798 break;
799 }
800
801 if (mode == MTD_OOB_AUTO)
802 onenand_transfer_auto_oob(mtd, buf, column, thislen);
803 else
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900804 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900805
806 read += thislen;
807
808 if (read == len)
809 break;
810
811 buf += thislen;
812
813 /* Read more? */
814 if (read < len) {
815 /* Page size */
816 from += mtd->writesize;
817 column = 0;
818 }
819 }
820
821 ops->oobretlen = read;
822
823 if (ret)
824 return ret;
825
826 if (mtd->ecc_stats.failed - stats.failed)
827 return -EBADMSG;
828
829 return 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900830}
831
832/**
833 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
834 * @param mtd MTD device structure
835 * @param from offset to read from
836 * @param len number of bytes to read
837 * @param retlen pointer to variable to store the number of read bytes
838 * @param buf the databuffer to put data
839 *
840 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
841*/
842int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
843 size_t * retlen, u_char * buf)
844{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900845 struct mtd_oob_ops ops = {
846 .len = len,
847 .ooblen = 0,
848 .datbuf = buf,
849 .oobbuf = NULL,
850 };
851 int ret;
852
853 onenand_get_device(mtd, FL_READING);
854 ret = onenand_read_ops_nolock(mtd, from, &ops);
855 onenand_release_device(mtd);
856
857 *retlen = ops.retlen;
858 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +0900859}
860
861/**
862 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
863 * @param mtd MTD device structure
864 * @param from offset to read from
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900865 * @param ops oob operations description structure
Kyungmin Park916527f2007-09-10 17:13:49 +0900866 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900867 * OneNAND main and/or out-of-band
Kyungmin Park916527f2007-09-10 17:13:49 +0900868 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900869int onenand_read_oob(struct mtd_info *mtd, loff_t from,
870 struct mtd_oob_ops *ops)
871{
872 int ret;
873
874 switch (ops->mode) {
875 case MTD_OOB_PLACE:
876 case MTD_OOB_AUTO:
877 break;
878 case MTD_OOB_RAW:
879 /* Not implemented yet */
880 default:
881 return -EINVAL;
882 }
883
884 onenand_get_device(mtd, FL_READING);
885 if (ops->datbuf)
886 ret = onenand_read_ops_nolock(mtd, from, ops);
887 else
888 ret = onenand_read_oob_nolock(mtd, from, ops);
889 onenand_release_device(mtd);
890
891 return ret;
892}
893
894/**
895 * onenand_bbt_wait - [DEFAULT] wait until the command is done
896 * @param mtd MTD device structure
897 * @param state state to select the max. timeout value
898 *
899 * Wait for command done.
900 */
901static int onenand_bbt_wait(struct mtd_info *mtd, int state)
902{
903 struct onenand_chip *this = mtd->priv;
904 unsigned int flags = ONENAND_INT_MASTER;
905 unsigned int interrupt;
906 unsigned int ctrl;
907
908 while (1) {
909 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
910 if (interrupt & flags)
911 break;
912 }
913
914 /* To get correct interrupt status in timeout case */
915 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
916 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
917
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900918 if (interrupt & ONENAND_INT_READ) {
919 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
920 if (ecc & ONENAND_ECC_2BIT_ALL)
921 return ONENAND_BBT_READ_ERROR;
922 } else {
923 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
924 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
925 return ONENAND_BBT_READ_FATAL_ERROR;
926 }
927
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900928 /* Initial bad block case: 0x2400 or 0x0400 */
929 if (ctrl & ONENAND_CTRL_ERROR) {
930 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
931 return ONENAND_BBT_READ_ERROR;
932 }
933
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900934 return 0;
935}
936
937/**
938 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
939 * @param mtd MTD device structure
940 * @param from offset to read from
941 * @param ops oob operation description structure
942 *
943 * OneNAND read out-of-band data from the spare area for bbt scan
944 */
945int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
946 struct mtd_oob_ops *ops)
Kyungmin Park916527f2007-09-10 17:13:49 +0900947{
948 struct onenand_chip *this = mtd->priv;
949 int read = 0, thislen, column;
950 int ret = 0;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900951 size_t len = ops->ooblen;
952 u_char *buf = ops->oobbuf;
Kyungmin Park916527f2007-09-10 17:13:49 +0900953
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900954 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park916527f2007-09-10 17:13:49 +0900955
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900956 /* Initialize return value */
957 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900958
959 /* Do not allow reads past end of device */
960 if (unlikely((from + len) > mtd->size)) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900961 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
962 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park916527f2007-09-10 17:13:49 +0900963 }
964
965 /* Grab the lock and see if the device is available */
966 onenand_get_device(mtd, FL_READING);
967
968 column = from & (mtd->oobsize - 1);
969
970 while (read < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900971
Kyungmin Park916527f2007-09-10 17:13:49 +0900972 thislen = mtd->oobsize - column;
973 thislen = min_t(int, thislen, len);
974
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900975 this->spare_buf = buf;
Kyungmin Park916527f2007-09-10 17:13:49 +0900976 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
977
978 onenand_update_bufferram(mtd, from, 0);
979
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900980 ret = this->bbt_wait(mtd, FL_READING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900981 if (ret)
982 break;
Kyungmin Park916527f2007-09-10 17:13:49 +0900983
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900984 this->read_spareram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park916527f2007-09-10 17:13:49 +0900985 read += thislen;
986 if (read == len)
987 break;
988
Kyungmin Park916527f2007-09-10 17:13:49 +0900989 buf += thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900990
Kyungmin Park916527f2007-09-10 17:13:49 +0900991 /* Read more? */
992 if (read < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900993 /* Update Page size */
994 from += this->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +0900995 column = 0;
996 }
997 }
998
999 /* Deselect and wake up anyone waiting on the device */
1000 onenand_release_device(mtd);
1001
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001002 ops->oobretlen = read;
Kyungmin Park916527f2007-09-10 17:13:49 +09001003 return ret;
1004}
1005
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001006
Kyungmin Park916527f2007-09-10 17:13:49 +09001007#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1008/**
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001009 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1010 * @param mtd MTD device structure
1011 * @param buf the databuffer to verify
1012 * @param to offset to read from
Kyungmin Park916527f2007-09-10 17:13:49 +09001013 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001014static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park916527f2007-09-10 17:13:49 +09001015{
1016 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001017 u_char *oob_buf = this->oob_buf;
1018 int status, i;
1019
1020 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1021 onenand_update_bufferram(mtd, to, 0);
1022 status = this->wait(mtd, FL_READING);
1023 if (status)
1024 return status;
1025
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001026 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001027 for (i = 0; i < mtd->oobsize; i++)
1028 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1029 return -EBADMSG;
1030
1031 return 0;
1032}
1033
1034/**
1035 * onenand_verify - [GENERIC] verify the chip contents after a write
1036 * @param mtd MTD device structure
1037 * @param buf the databuffer to verify
1038 * @param addr offset to read from
1039 * @param len number of bytes to read and compare
1040 */
1041static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1042{
1043 struct onenand_chip *this = mtd->priv;
1044 void __iomem *dataram;
Kyungmin Park916527f2007-09-10 17:13:49 +09001045 int ret = 0;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001046 int thislen, column;
Kyungmin Park916527f2007-09-10 17:13:49 +09001047
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001048 while (len != 0) {
1049 thislen = min_t(int, this->writesize, len);
1050 column = addr & (this->writesize - 1);
1051 if (column + thislen > this->writesize)
1052 thislen = this->writesize - column;
Kyungmin Park916527f2007-09-10 17:13:49 +09001053
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001054 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001055
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001056 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park916527f2007-09-10 17:13:49 +09001057
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001058 ret = this->wait(mtd, FL_READING);
1059 if (ret)
1060 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001061
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001062 onenand_update_bufferram(mtd, addr, 1);
1063
1064 dataram = this->base + ONENAND_DATARAM;
1065 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1066
1067 if (memcmp(buf, dataram + column, thislen))
1068 return -EBADMSG;
1069
1070 len -= thislen;
1071 buf += thislen;
1072 addr += thislen;
1073 }
Kyungmin Park916527f2007-09-10 17:13:49 +09001074
1075 return 0;
1076}
1077#else
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001078#define onenand_verify(...) (0)
1079#define onenand_verify_oob(...) (0)
Kyungmin Park916527f2007-09-10 17:13:49 +09001080#endif
1081
Kyungmin Parkd438d502008-08-13 09:11:02 +09001082#define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
Kyungmin Park916527f2007-09-10 17:13:49 +09001083
1084/**
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001085 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1086 * @param mtd MTD device structure
1087 * @param oob_buf oob buffer
1088 * @param buf source address
1089 * @param column oob offset to write to
1090 * @param thislen oob length to write
Kyungmin Park916527f2007-09-10 17:13:49 +09001091 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001092static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1093 const u_char *buf, int column, int thislen)
Kyungmin Park916527f2007-09-10 17:13:49 +09001094{
1095 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001096 struct nand_oobfree *free;
1097 int writecol = column;
1098 int writeend = column + thislen;
1099 int lastgap = 0;
1100 unsigned int i;
1101
1102 free = this->ecclayout->oobfree;
1103 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1104 if (writecol >= lastgap)
1105 writecol += free->offset - lastgap;
1106 if (writeend >= lastgap)
1107 writeend += free->offset - lastgap;
1108 lastgap = free->offset + free->length;
1109 }
1110 free = this->ecclayout->oobfree;
1111 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1112 int free_end = free->offset + free->length;
1113 if (free->offset < writeend && free_end > writecol) {
1114 int st = max_t(int,free->offset,writecol);
1115 int ed = min_t(int,free_end,writeend);
1116 int n = ed - st;
1117 memcpy(oob_buf + st, buf, n);
1118 buf += n;
1119 } else if (column == 0)
1120 break;
1121 }
1122 return 0;
1123}
1124
1125/**
1126 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1127 * @param mtd MTD device structure
1128 * @param to offset to write to
1129 * @param ops oob operation description structure
1130 *
1131 * Write main and/or oob with ECC
1132 */
1133static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1134 struct mtd_oob_ops *ops)
1135{
1136 struct onenand_chip *this = mtd->priv;
1137 int written = 0, column, thislen, subpage;
1138 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1139 size_t len = ops->len;
1140 size_t ooblen = ops->ooblen;
1141 const u_char *buf = ops->datbuf;
1142 const u_char *oob = ops->oobbuf;
1143 u_char *oobbuf;
Kyungmin Park916527f2007-09-10 17:13:49 +09001144 int ret = 0;
1145
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001146 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001147
1148 /* Initialize retlen, in case of early exit */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001149 ops->retlen = 0;
1150 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001151
1152 /* Do not allow writes past end of device */
1153 if (unlikely((to + len) > mtd->size)) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001154 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001155 return -EINVAL;
1156 }
1157
1158 /* Reject writes, which are not page aligned */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001159 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1160 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001161 return -EINVAL;
1162 }
1163
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001164 if (ops->mode == MTD_OOB_AUTO)
1165 oobsize = this->ecclayout->oobavail;
1166 else
1167 oobsize = mtd->oobsize;
1168
1169 oobcolumn = to & (mtd->oobsize - 1);
1170
1171 column = to & (mtd->writesize - 1);
Kyungmin Park916527f2007-09-10 17:13:49 +09001172
1173 /* Loop until all data write */
1174 while (written < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001175 u_char *wbuf = (u_char *) buf;
Kyungmin Park916527f2007-09-10 17:13:49 +09001176
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001177 thislen = min_t(int, mtd->writesize - column, len - written);
1178 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park916527f2007-09-10 17:13:49 +09001179
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001180 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1181
1182 /* Partial page write */
1183 subpage = thislen < mtd->writesize;
1184 if (subpage) {
1185 memset(this->page_buf, 0xff, mtd->writesize);
1186 memcpy(this->page_buf + column, buf, thislen);
1187 wbuf = this->page_buf;
1188 }
1189
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001190 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001191
1192 if (oob) {
1193 oobbuf = this->oob_buf;
1194
1195 /* We send data to spare ram with oobsize
1196 * * to prevent byte access */
1197 memset(oobbuf, 0xff, mtd->oobsize);
1198 if (ops->mode == MTD_OOB_AUTO)
1199 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1200 else
1201 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1202
1203 oobwritten += thisooblen;
1204 oob += thisooblen;
1205 oobcolumn = 0;
1206 } else
1207 oobbuf = (u_char *) ffchars;
1208
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001209 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001210
Kyungmin Parkd438d502008-08-13 09:11:02 +09001211 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001212
Kyungmin Park916527f2007-09-10 17:13:49 +09001213 ret = this->wait(mtd, FL_WRITING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001214
1215 /* In partial page write we don't update bufferram */
1216 onenand_update_bufferram(mtd, to, !ret && !subpage);
1217 if (ONENAND_IS_2PLANE(this)) {
1218 ONENAND_SET_BUFFERRAM1(this);
1219 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1220 }
1221
Kyungmin Park916527f2007-09-10 17:13:49 +09001222 if (ret) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001223 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1224 break;
1225 }
1226
1227 /* Only check verify write turn on */
1228 ret = onenand_verify(mtd, buf, to, thislen);
1229 if (ret) {
1230 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +09001231 break;
1232 }
1233
1234 written += thislen;
1235
Kyungmin Park916527f2007-09-10 17:13:49 +09001236 if (written == len)
1237 break;
1238
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001239 column = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001240 to += thislen;
1241 buf += thislen;
1242 }
1243
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001244 ops->retlen = written;
Kyungmin Park916527f2007-09-10 17:13:49 +09001245
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001246 return ret;
1247}
1248
1249/**
1250 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1251 * @param mtd MTD device structure
1252 * @param to offset to write to
1253 * @param len number of bytes to write
1254 * @param retlen pointer to variable to store the number of written bytes
1255 * @param buf the data to write
1256 * @param mode operation mode
1257 *
1258 * OneNAND write out-of-band
1259 */
1260static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1261 struct mtd_oob_ops *ops)
1262{
1263 struct onenand_chip *this = mtd->priv;
1264 int column, ret = 0, oobsize;
1265 int written = 0;
1266 u_char *oobbuf;
1267 size_t len = ops->ooblen;
1268 const u_char *buf = ops->oobbuf;
1269 mtd_oob_mode_t mode = ops->mode;
1270
1271 to += ops->ooboffs;
1272
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001273 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001274
1275 /* Initialize retlen, in case of early exit */
1276 ops->oobretlen = 0;
1277
1278 if (mode == MTD_OOB_AUTO)
1279 oobsize = this->ecclayout->oobavail;
1280 else
1281 oobsize = mtd->oobsize;
1282
1283 column = to & (mtd->oobsize - 1);
1284
1285 if (unlikely(column >= oobsize)) {
1286 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1287 return -EINVAL;
1288 }
1289
1290 /* For compatibility with NAND: Do not allow write past end of page */
1291 if (unlikely(column + len > oobsize)) {
1292 printk(KERN_ERR "onenand_write_oob_nolock: "
1293 "Attempt to write past end of page\n");
1294 return -EINVAL;
1295 }
1296
1297 /* Do not allow reads past end of device */
1298 if (unlikely(to >= mtd->size ||
1299 column + len > ((mtd->size >> this->page_shift) -
1300 (to >> this->page_shift)) * oobsize)) {
1301 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1302 return -EINVAL;
1303 }
1304
1305 oobbuf = this->oob_buf;
1306
1307 /* Loop until all data write */
1308 while (written < len) {
1309 int thislen = min_t(int, oobsize, len - written);
1310
1311 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1312
1313 /* We send data to spare ram with oobsize
1314 * to prevent byte access */
1315 memset(oobbuf, 0xff, mtd->oobsize);
1316 if (mode == MTD_OOB_AUTO)
1317 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1318 else
1319 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001320 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001321
1322 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1323
1324 onenand_update_bufferram(mtd, to, 0);
1325 if (ONENAND_IS_2PLANE(this)) {
1326 ONENAND_SET_BUFFERRAM1(this);
1327 onenand_update_bufferram(mtd, to + this->writesize, 0);
1328 }
1329
1330 ret = this->wait(mtd, FL_WRITING);
1331 if (ret) {
1332 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1333 break;
1334 }
1335
1336 ret = onenand_verify_oob(mtd, oobbuf, to);
1337 if (ret) {
1338 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1339 break;
1340 }
1341
1342 written += thislen;
1343 if (written == len)
1344 break;
1345
1346 to += mtd->writesize;
1347 buf += thislen;
1348 column = 0;
1349 }
1350
1351 ops->oobretlen = written;
Kyungmin Park916527f2007-09-10 17:13:49 +09001352
1353 return ret;
1354}
1355
1356/**
1357 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1358 * @param mtd MTD device structure
1359 * @param to offset to write to
1360 * @param len number of bytes to write
1361 * @param retlen pointer to variable to store the number of written bytes
1362 * @param buf the data to write
1363 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001364 * Write with ECC
Kyungmin Park916527f2007-09-10 17:13:49 +09001365 */
1366int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1367 size_t * retlen, const u_char * buf)
1368{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001369 struct mtd_oob_ops ops = {
1370 .len = len,
1371 .ooblen = 0,
1372 .datbuf = (u_char *) buf,
1373 .oobbuf = NULL,
1374 };
1375 int ret;
1376
1377 onenand_get_device(mtd, FL_WRITING);
1378 ret = onenand_write_ops_nolock(mtd, to, &ops);
1379 onenand_release_device(mtd);
1380
1381 *retlen = ops.retlen;
1382 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001383}
1384
1385/**
1386 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1387 * @param mtd MTD device structure
1388 * @param to offset to write to
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001389 * @param ops oob operation description structure
Kyungmin Park916527f2007-09-10 17:13:49 +09001390 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001391 * OneNAND write main and/or out-of-band
Kyungmin Park916527f2007-09-10 17:13:49 +09001392 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001393int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1394 struct mtd_oob_ops *ops)
Kyungmin Park916527f2007-09-10 17:13:49 +09001395{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001396 int ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001397
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001398 switch (ops->mode) {
1399 case MTD_OOB_PLACE:
1400 case MTD_OOB_AUTO:
1401 break;
1402 case MTD_OOB_RAW:
1403 /* Not implemented yet */
1404 default:
Kyungmin Park916527f2007-09-10 17:13:49 +09001405 return -EINVAL;
1406 }
1407
Kyungmin Park916527f2007-09-10 17:13:49 +09001408 onenand_get_device(mtd, FL_WRITING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001409 if (ops->datbuf)
1410 ret = onenand_write_ops_nolock(mtd, to, ops);
1411 else
1412 ret = onenand_write_oob_nolock(mtd, to, ops);
Kyungmin Park916527f2007-09-10 17:13:49 +09001413 onenand_release_device(mtd);
1414
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001415 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001416
Kyungmin Park916527f2007-09-10 17:13:49 +09001417}
1418
1419/**
Kyungmin Parkd438d502008-08-13 09:11:02 +09001420 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1421 * @param mtd MTD device structure
1422 * @param ofs offset from device start
1423 * @param allowbbt 1, if its allowed to access the bbt area
1424 *
1425 * Check, if the block is bad, Either by reading the bad block table or
1426 * calling of the scan function.
1427 */
1428static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1429{
1430 struct onenand_chip *this = mtd->priv;
1431 struct bbm_info *bbm = this->bbm;
1432
1433 /* Return info from the table */
1434 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1435}
1436
1437
1438/**
Kyungmin Park916527f2007-09-10 17:13:49 +09001439 * onenand_erase - [MTD Interface] erase block(s)
1440 * @param mtd MTD device structure
1441 * @param instr erase instruction
1442 *
1443 * Erase one ore more blocks
1444 */
1445int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1446{
1447 struct onenand_chip *this = mtd->priv;
1448 unsigned int block_size;
1449 loff_t addr;
1450 int len;
1451 int ret = 0;
1452
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001453 MTDDEBUG (MTD_DEBUG_LEVEL3,
1454 "onenand_erase: start = 0x%08x, len = %i\n",
Scott Woodc45912d2008-10-24 16:20:43 -05001455 (unsigned int)instr->addr, (unsigned int)instr->len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001456
1457 block_size = (1 << this->erase_shift);
1458
1459 /* Start address must align on block boundary */
1460 if (unlikely(instr->addr & (block_size - 1))) {
Scott Wood3167c532008-06-20 12:38:57 -05001461 MTDDEBUG (MTD_DEBUG_LEVEL0,
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001462 "onenand_erase: Unaligned address\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001463 return -EINVAL;
1464 }
1465
1466 /* Length must align on block boundary */
1467 if (unlikely(instr->len & (block_size - 1))) {
Scott Wood3167c532008-06-20 12:38:57 -05001468 MTDDEBUG (MTD_DEBUG_LEVEL0,
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001469 "onenand_erase: Length not block aligned\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001470 return -EINVAL;
1471 }
1472
1473 /* Do not allow erase past end of device */
1474 if (unlikely((instr->len + instr->addr) > mtd->size)) {
Scott Wood3167c532008-06-20 12:38:57 -05001475 MTDDEBUG (MTD_DEBUG_LEVEL0,
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001476 "onenand_erase: Erase past end of device\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001477 return -EINVAL;
1478 }
1479
1480 instr->fail_addr = 0xffffffff;
1481
1482 /* Grab the lock and see if the device is available */
1483 onenand_get_device(mtd, FL_ERASING);
1484
1485 /* Loop throught the pages */
1486 len = instr->len;
1487 addr = instr->addr;
1488
1489 instr->state = MTD_ERASING;
1490
1491 while (len) {
1492
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001493 /* Check if we have a bad block, we do not erase bad blocks */
1494 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1495 printk(KERN_WARNING "onenand_erase: attempt to erase"
1496 " a bad block at addr 0x%08x\n",
1497 (unsigned int) addr);
1498 instr->state = MTD_ERASE_FAILED;
1499 goto erase_exit;
1500 }
Kyungmin Park916527f2007-09-10 17:13:49 +09001501
1502 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1503
Kyungmin Parkd438d502008-08-13 09:11:02 +09001504 onenand_invalidate_bufferram(mtd, addr, block_size);
1505
Kyungmin Park916527f2007-09-10 17:13:49 +09001506 ret = this->wait(mtd, FL_ERASING);
1507 /* Check, if it is write protected */
1508 if (ret) {
1509 if (ret == -EPERM)
Scott Wood3167c532008-06-20 12:38:57 -05001510 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk4b070802008-08-14 14:41:06 +02001511 "Device is write protected!!!\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001512 else
Scott Wood3167c532008-06-20 12:38:57 -05001513 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk4b070802008-08-14 14:41:06 +02001514 "Failed erase, block %d\n",
1515 (unsigned)(addr >> this->erase_shift));
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001516 if (ret == -EPERM)
1517 printk("onenand_erase: "
1518 "Device is write protected!!!\n");
1519 else
1520 printk("onenand_erase: "
1521 "Failed erase, block %d\n",
1522 (unsigned)(addr >> this->erase_shift));
Kyungmin Park916527f2007-09-10 17:13:49 +09001523 instr->state = MTD_ERASE_FAILED;
1524 instr->fail_addr = addr;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001525
Kyungmin Park916527f2007-09-10 17:13:49 +09001526 goto erase_exit;
1527 }
1528
1529 len -= block_size;
1530 addr += block_size;
1531 }
1532
1533 instr->state = MTD_ERASE_DONE;
1534
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001535erase_exit:
Kyungmin Park916527f2007-09-10 17:13:49 +09001536
1537 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1538 /* Do call back function */
1539 if (!ret)
1540 mtd_erase_callback(instr);
1541
1542 /* Deselect and wake up anyone waiting on the device */
1543 onenand_release_device(mtd);
1544
1545 return ret;
1546}
1547
1548/**
1549 * onenand_sync - [MTD Interface] sync
1550 * @param mtd MTD device structure
1551 *
1552 * Sync is actually a wait for chip ready function
1553 */
1554void onenand_sync(struct mtd_info *mtd)
1555{
Scott Wood3167c532008-06-20 12:38:57 -05001556 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001557
1558 /* Grab the lock and see if the device is available */
1559 onenand_get_device(mtd, FL_SYNCING);
1560
1561 /* Release it and go back */
1562 onenand_release_device(mtd);
1563}
1564
1565/**
1566 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1567 * @param mtd MTD device structure
1568 * @param ofs offset relative to mtd start
Kyungmin Parkd438d502008-08-13 09:11:02 +09001569 *
1570 * Check whether the block is bad
Kyungmin Park916527f2007-09-10 17:13:49 +09001571 */
1572int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1573{
Kyungmin Parkd438d502008-08-13 09:11:02 +09001574 int ret;
1575
1576 /* Check for invalid offset */
1577 if (ofs > mtd->size)
1578 return -EINVAL;
1579
1580 onenand_get_device(mtd, FL_READING);
1581 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1582 onenand_release_device(mtd);
1583 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001584}
1585
1586/**
1587 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1588 * @param mtd MTD device structure
1589 * @param ofs offset relative to mtd start
Kyungmin Parkd438d502008-08-13 09:11:02 +09001590 *
1591 * Mark the block as bad
Kyungmin Park916527f2007-09-10 17:13:49 +09001592 */
1593int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1594{
Kyungmin Parkd438d502008-08-13 09:11:02 +09001595 struct onenand_chip *this = mtd->priv;
1596 int ret;
1597
1598 ret = onenand_block_isbad(mtd, ofs);
1599 if (ret) {
1600 /* If it was bad already, return success and do nothing */
1601 if (ret > 0)
1602 return 0;
1603 return ret;
1604 }
1605
1606 ret = this->block_markbad(mtd, ofs);
1607 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001608}
1609
1610/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001611 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1612 * @param mtd MTD device structure
1613 * @param ofs offset relative to mtd start
1614 * @param len number of bytes to lock or unlock
1615 * @param cmd lock or unlock command
Kyungmin Park916527f2007-09-10 17:13:49 +09001616 *
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001617 * Lock or unlock one or more blocks
Kyungmin Park916527f2007-09-10 17:13:49 +09001618 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001619static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park916527f2007-09-10 17:13:49 +09001620{
1621 struct onenand_chip *this = mtd->priv;
1622 int start, end, block, value, status;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001623 int wp_status_mask;
Kyungmin Park916527f2007-09-10 17:13:49 +09001624
1625 start = ofs >> this->erase_shift;
1626 end = len >> this->erase_shift;
1627
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001628 if (cmd == ONENAND_CMD_LOCK)
1629 wp_status_mask = ONENAND_WP_LS;
1630 else
1631 wp_status_mask = ONENAND_WP_US;
1632
Kyungmin Park916527f2007-09-10 17:13:49 +09001633 /* Continuous lock scheme */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001634 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park916527f2007-09-10 17:13:49 +09001635 /* Set start block address */
1636 this->write_word(start,
1637 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1638 /* Set end block address */
1639 this->write_word(end - 1,
1640 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1641 /* Write unlock command */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001642 this->command(mtd, cmd, 0, 0);
Kyungmin Park916527f2007-09-10 17:13:49 +09001643
1644 /* There's no return value */
1645 this->wait(mtd, FL_UNLOCKING);
1646
1647 /* Sanity check */
1648 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1649 & ONENAND_CTRL_ONGO)
1650 continue;
1651
1652 /* Check lock status */
1653 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1654 if (!(status & ONENAND_WP_US))
1655 printk(KERN_ERR "wp status = 0x%x\n", status);
1656
1657 return 0;
1658 }
1659
1660 /* Block lock scheme */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001661 for (block = start; block < start + end; block++) {
1662 /* Set block address */
1663 value = onenand_block_address(this, block);
1664 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1665 /* Select DataRAM for DDP */
1666 value = onenand_bufferram_address(this, block);
1667 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1668
Kyungmin Park916527f2007-09-10 17:13:49 +09001669 /* Set start block address */
1670 this->write_word(block,
1671 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1672 /* Write unlock command */
1673 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1674
1675 /* There's no return value */
1676 this->wait(mtd, FL_UNLOCKING);
1677
1678 /* Sanity check */
1679 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1680 & ONENAND_CTRL_ONGO)
1681 continue;
1682
Kyungmin Park916527f2007-09-10 17:13:49 +09001683 /* Check lock status */
1684 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1685 if (!(status & ONENAND_WP_US))
1686 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1687 block, status);
1688 }
1689
1690 return 0;
1691}
1692
Stefan Roese4fca3312008-11-11 10:28:53 +01001693#ifdef ONENAND_LINUX
Kyungmin Park916527f2007-09-10 17:13:49 +09001694/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001695 * onenand_lock - [MTD Interface] Lock block(s)
1696 * @param mtd MTD device structure
1697 * @param ofs offset relative to mtd start
1698 * @param len number of bytes to unlock
1699 *
1700 * Lock one or more blocks
1701 */
1702static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1703{
1704 int ret;
1705
1706 onenand_get_device(mtd, FL_LOCKING);
1707 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1708 onenand_release_device(mtd);
1709 return ret;
1710}
1711
1712/**
1713 * onenand_unlock - [MTD Interface] Unlock block(s)
1714 * @param mtd MTD device structure
1715 * @param ofs offset relative to mtd start
1716 * @param len number of bytes to unlock
1717 *
1718 * Unlock one or more blocks
1719 */
1720static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1721{
1722 int ret;
1723
1724 onenand_get_device(mtd, FL_LOCKING);
1725 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1726 onenand_release_device(mtd);
1727 return ret;
1728}
Stefan Roese4fca3312008-11-11 10:28:53 +01001729#endif
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001730
1731/**
1732 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1733 * @param this onenand chip data structure
1734 *
1735 * Check lock status
1736 */
1737static int onenand_check_lock_status(struct onenand_chip *this)
1738{
1739 unsigned int value, block, status;
1740 unsigned int end;
1741
1742 end = this->chipsize >> this->erase_shift;
1743 for (block = 0; block < end; block++) {
1744 /* Set block address */
1745 value = onenand_block_address(this, block);
1746 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1747 /* Select DataRAM for DDP */
1748 value = onenand_bufferram_address(this, block);
1749 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1750 /* Set start block address */
1751 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1752
1753 /* Check lock status */
1754 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1755 if (!(status & ONENAND_WP_US)) {
1756 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1757 return 0;
1758 }
1759 }
1760
1761 return 1;
1762}
1763
1764/**
1765 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1766 * @param mtd MTD device structure
1767 *
1768 * Unlock all blocks
1769 */
1770static void onenand_unlock_all(struct mtd_info *mtd)
1771{
1772 struct onenand_chip *this = mtd->priv;
1773 loff_t ofs = 0;
1774 size_t len = this->chipsize;
1775
1776 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1777 /* Set start block address */
1778 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1779 /* Write unlock command */
1780 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1781
1782 /* There's no return value */
1783 this->wait(mtd, FL_LOCKING);
1784
1785 /* Sanity check */
1786 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1787 & ONENAND_CTRL_ONGO)
1788 continue;
1789
1790 return;
1791
1792 /* Check lock status */
1793 if (onenand_check_lock_status(this))
1794 return;
1795
1796 /* Workaround for all block unlock in DDP */
1797 if (ONENAND_IS_DDP(this)) {
1798 /* All blocks on another chip */
1799 ofs = this->chipsize >> 1;
1800 len = this->chipsize >> 1;
1801 }
1802 }
1803
1804 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1805}
1806
1807
1808/**
1809 * onenand_check_features - Check and set OneNAND features
1810 * @param mtd MTD data structure
1811 *
1812 * Check and set OneNAND features
1813 * - lock scheme
1814 * - two plane
1815 */
1816static void onenand_check_features(struct mtd_info *mtd)
1817{
1818 struct onenand_chip *this = mtd->priv;
1819 unsigned int density, process;
1820
1821 /* Lock scheme depends on density and process */
1822 density = onenand_get_density(this->device_id);
1823 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
1824
1825 /* Lock scheme */
1826 switch (density) {
1827 case ONENAND_DEVICE_DENSITY_4Gb:
1828 this->options |= ONENAND_HAS_2PLANE;
1829
1830 case ONENAND_DEVICE_DENSITY_2Gb:
1831 /* 2Gb DDP don't have 2 plane */
1832 if (!ONENAND_IS_DDP(this))
1833 this->options |= ONENAND_HAS_2PLANE;
1834 this->options |= ONENAND_HAS_UNLOCK_ALL;
1835
1836 case ONENAND_DEVICE_DENSITY_1Gb:
1837 /* A-Die has all block unlock */
1838 if (process)
1839 this->options |= ONENAND_HAS_UNLOCK_ALL;
1840 break;
1841
1842 default:
1843 /* Some OneNAND has continuous lock scheme */
1844 if (!process)
1845 this->options |= ONENAND_HAS_CONT_LOCK;
1846 break;
1847 }
1848
1849 if (this->options & ONENAND_HAS_CONT_LOCK)
1850 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
1851 if (this->options & ONENAND_HAS_UNLOCK_ALL)
1852 printk(KERN_DEBUG "Chip support all block unlock\n");
1853 if (this->options & ONENAND_HAS_2PLANE)
1854 printk(KERN_DEBUG "Chip has 2 plane\n");
1855}
1856
1857/**
Kyungmin Park916527f2007-09-10 17:13:49 +09001858 * onenand_print_device_info - Print device ID
1859 * @param device device ID
1860 *
1861 * Print device ID
1862 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001863char *onenand_print_device_info(int device, int version)
Kyungmin Park916527f2007-09-10 17:13:49 +09001864{
1865 int vcc, demuxed, ddp, density;
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02001866 char *dev_info = malloc(80);
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001867 char *p = dev_info;
Kyungmin Park916527f2007-09-10 17:13:49 +09001868
1869 vcc = device & ONENAND_DEVICE_VCC_MASK;
1870 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1871 ddp = device & ONENAND_DEVICE_IS_DDP;
1872 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001873 p += sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park916527f2007-09-10 17:13:49 +09001874 demuxed ? "" : "Muxed ",
1875 ddp ? "(DDP)" : "",
1876 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02001877
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001878 sprintf(p, "\nOneNAND version = 0x%04x", version);
1879 printk("%s\n", dev_info);
1880
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02001881 return dev_info;
Kyungmin Park916527f2007-09-10 17:13:49 +09001882}
1883
1884static const struct onenand_manufacturers onenand_manuf_ids[] = {
1885 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park916527f2007-09-10 17:13:49 +09001886};
1887
1888/**
1889 * onenand_check_maf - Check manufacturer ID
1890 * @param manuf manufacturer ID
1891 *
1892 * Check manufacturer ID
1893 */
1894static int onenand_check_maf(int manuf)
1895{
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001896 int size = ARRAY_SIZE(onenand_manuf_ids);
1897 char *name;
Kyungmin Park916527f2007-09-10 17:13:49 +09001898 int i;
1899
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001900 for (i = 0; size; i++)
Kyungmin Park916527f2007-09-10 17:13:49 +09001901 if (manuf == onenand_manuf_ids[i].id)
1902 break;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001903
1904 if (i < size)
1905 name = onenand_manuf_ids[i].name;
1906 else
1907 name = "Unknown";
Kyungmin Park916527f2007-09-10 17:13:49 +09001908
1909#ifdef ONENAND_DEBUG
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001910 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park916527f2007-09-10 17:13:49 +09001911#endif
1912
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001913 return i == size;
Kyungmin Park916527f2007-09-10 17:13:49 +09001914}
1915
1916/**
1917 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1918 * @param mtd MTD device structure
1919 *
1920 * OneNAND detection method:
1921 * Compare the the values from command with ones from register
1922 */
1923static int onenand_probe(struct mtd_info *mtd)
1924{
1925 struct onenand_chip *this = mtd->priv;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001926 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
Kyungmin Park916527f2007-09-10 17:13:49 +09001927 int density;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001928 int syscfg;
1929
1930 /* Save system configuration 1 */
1931 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
1932 /* Clear Sync. Burst Read mode to read BootRAM */
1933 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park916527f2007-09-10 17:13:49 +09001934
1935 /* Send the command for reading device ID from BootRAM */
1936 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1937
1938 /* Read manufacturer and device IDs from BootRAM */
1939 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1940 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1941
Kyungmin Park916527f2007-09-10 17:13:49 +09001942 /* Reset OneNAND to read default register values */
1943 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1944
Kyungmin Parkd438d502008-08-13 09:11:02 +09001945 /* Wait reset */
1946 this->wait(mtd, FL_RESETING);
Kyungmin Park916527f2007-09-10 17:13:49 +09001947
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001948 /* Restore system configuration 1 */
1949 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
1950
1951 /* Check manufacturer ID */
1952 if (onenand_check_maf(bram_maf_id))
1953 return -ENXIO;
1954
Kyungmin Park916527f2007-09-10 17:13:49 +09001955 /* Read manufacturer and device IDs from Register */
1956 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1957 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001958 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
Kyungmin Park916527f2007-09-10 17:13:49 +09001959
1960 /* Check OneNAND device */
1961 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1962 return -ENXIO;
1963
Kyungmin Park1bb707c2008-03-17 08:54:06 +09001964 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1965 if (dev_id & (1 << 9)) {
1966 printk("Not yet support Flex-OneNAND\n");
1967 return -ENXIO;
1968 }
1969
Kyungmin Park916527f2007-09-10 17:13:49 +09001970 /* Flash device information */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001971 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park916527f2007-09-10 17:13:49 +09001972 this->device_id = dev_id;
Stefan Roese8cf11f32008-11-11 10:29:09 +01001973 this->version_id = ver_id;
Kyungmin Park916527f2007-09-10 17:13:49 +09001974
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001975 density = onenand_get_density(dev_id);
Kyungmin Park916527f2007-09-10 17:13:49 +09001976 this->chipsize = (16 << density) << 20;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001977 /* Set density mask. it is used for DDP */
1978 if (ONENAND_IS_DDP(this))
1979 this->density_mask = (1 << (density + 6));
1980 else
1981 this->density_mask = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001982
1983 /* OneNAND page size & block size */
1984 /* The data buffer size is equal to page size */
Kyungmin Parkd438d502008-08-13 09:11:02 +09001985 mtd->writesize =
Kyungmin Park916527f2007-09-10 17:13:49 +09001986 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Kyungmin Parkd438d502008-08-13 09:11:02 +09001987 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park916527f2007-09-10 17:13:49 +09001988 /* Pagers per block is always 64 in OneNAND */
Kyungmin Parkd438d502008-08-13 09:11:02 +09001989 mtd->erasesize = mtd->writesize << 6;
Kyungmin Park916527f2007-09-10 17:13:49 +09001990
1991 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Parkd438d502008-08-13 09:11:02 +09001992 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park916527f2007-09-10 17:13:49 +09001993 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Parkd438d502008-08-13 09:11:02 +09001994 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001995 /* It's real page size */
1996 this->writesize = mtd->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +09001997
1998 /* REVIST: Multichip handling */
1999
2000 mtd->size = this->chipsize;
2001
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002002 /* Check OneNAND features */
2003 onenand_check_features(mtd);
Kyungmin Park916527f2007-09-10 17:13:49 +09002004
Kyungmin Parkd438d502008-08-13 09:11:02 +09002005 mtd->flags = MTD_CAP_NANDFLASH;
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002006 mtd->erase = onenand_erase;
2007 mtd->read = onenand_read;
2008 mtd->write = onenand_write;
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002009 mtd->read_oob = onenand_read_oob;
2010 mtd->write_oob = onenand_write_oob;
2011 mtd->sync = onenand_sync;
2012 mtd->block_isbad = onenand_block_isbad;
2013 mtd->block_markbad = onenand_block_markbad;
2014
Kyungmin Park916527f2007-09-10 17:13:49 +09002015 return 0;
2016}
2017
2018/**
2019 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2020 * @param mtd MTD device structure
2021 * @param maxchips Number of chips to scan for
2022 *
2023 * This fills out all the not initialized function pointers
2024 * with the defaults.
2025 * The flash ID is read and the mtd/chip structures are
2026 * filled with the appropriate values.
2027 */
2028int onenand_scan(struct mtd_info *mtd, int maxchips)
2029{
2030 struct onenand_chip *this = mtd->priv;
2031
2032 if (!this->read_word)
2033 this->read_word = onenand_readw;
2034 if (!this->write_word)
2035 this->write_word = onenand_writew;
2036
2037 if (!this->command)
2038 this->command = onenand_command;
2039 if (!this->wait)
2040 this->wait = onenand_wait;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002041 if (!this->bbt_wait)
2042 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park916527f2007-09-10 17:13:49 +09002043
2044 if (!this->read_bufferram)
2045 this->read_bufferram = onenand_read_bufferram;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002046 if (!this->read_spareram)
2047 this->read_spareram = onenand_read_bufferram;
Kyungmin Park916527f2007-09-10 17:13:49 +09002048 if (!this->write_bufferram)
2049 this->write_bufferram = onenand_write_bufferram;
2050
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002051 if (!this->scan_bbt)
2052 this->scan_bbt = onenand_default_bbt;
2053
Kyungmin Park916527f2007-09-10 17:13:49 +09002054 if (onenand_probe(mtd))
2055 return -ENXIO;
2056
2057 /* Set Sync. Burst Read after probing */
2058 if (this->mmcontrol) {
2059 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2060 this->read_bufferram = onenand_sync_read_bufferram;
2061 }
2062
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09002063 /* Allocate buffers, if necessary */
2064 if (!this->page_buf) {
2065 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2066 if (!this->page_buf) {
2067 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2068 return -ENOMEM;
2069 }
2070 this->options |= ONENAND_PAGEBUF_ALLOC;
2071 }
2072 if (!this->oob_buf) {
2073 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2074 if (!this->oob_buf) {
2075 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2076 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2077 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2078 kfree(this->page_buf);
2079 }
2080 return -ENOMEM;
2081 }
2082 this->options |= ONENAND_OOBBUF_ALLOC;
2083 }
2084
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002085 /* Unlock whole block */
2086 onenand_unlock_all(mtd);
Kyungmin Park916527f2007-09-10 17:13:49 +09002087
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002088 return this->scan_bbt(mtd);
Kyungmin Park916527f2007-09-10 17:13:49 +09002089}
2090
2091/**
2092 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2093 * @param mtd MTD device structure
2094 */
2095void onenand_release(struct mtd_info *mtd)
2096{
2097}