blob: 894ebadc3e692c4180605be35432bf2aeeac3084 [file] [log] [blame]
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05304 * Copyright © 2005-2009 Samsung Electronics
5 * Copyright © 2007 Nokia Corporation
6 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01007 * Kyungmin Park <kyungmin.park@samsung.com>
8 *
Adrian Hunter81280d52007-02-15 09:47:29 +09009 * Credits:
10 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
11 * auto-placement support, read-while load support, various fixes
Adrian Hunter81280d52007-02-15 09:47:29 +090012 *
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070013 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
14 * Flex-OneNAND support
Amul Kumar Saha3cf60252009-10-21 17:00:05 +053015 * Amul Kumar Saha <amul.saha at samsung.com>
16 * OTP support
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070017 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010018 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
Amul Sahac90173f2009-06-16 11:24:01 +053025#include <linux/moduleparam.h>
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010026#include <linux/init.h>
Andrew Morton015953d2005-11-08 21:34:28 -080027#include <linux/sched.h>
Richard Purdie6c77fd62008-02-06 10:18:22 +000028#include <linux/delay.h>
Kyungmin Park2c221202006-11-16 11:23:48 +090029#include <linux/interrupt.h>
Andrew Morton015953d2005-11-08 21:34:28 -080030#include <linux/jiffies.h>
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010031#include <linux/mtd/mtd.h>
32#include <linux/mtd/onenand.h>
33#include <linux/mtd/partitions.h>
34
35#include <asm/io.h>
36
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070037/* Default Flex-OneNAND boundary and lock respectively */
38static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
39
Amul Sahac90173f2009-06-16 11:24:01 +053040module_param_array(flex_bdry, int, NULL, 0400);
41MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
42 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
43 "DIE_BDRY: SLC boundary of the die"
44 "LOCK: Locking information for SLC boundary"
45 " : 0->Set boundary in unlocked status"
46 " : 1->Set boundary in locked status");
47
Amul Kumar Saha3cf60252009-10-21 17:00:05 +053048/* Default OneNAND/Flex-OneNAND OTP options*/
49static int otp;
50
51module_param(otp, int, 0400);
52MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
53 "Syntax : otp=LOCK_TYPE"
54 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
55 " : 0 -> Default (No Blocks Locked)"
56 " : 1 -> OTP Block lock"
57 " : 2 -> 1st Block lock"
58 " : 3 -> BOTH OTP Block and 1st Block lock");
59
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070060/**
61 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
62 * For now, we expose only 64 out of 80 ecc bytes
63 */
64static struct nand_ecclayout onenand_oob_128 = {
65 .eccbytes = 64,
66 .eccpos = {
67 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
68 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
69 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
70 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
71 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
72 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
73 102, 103, 104, 105
74 },
75 .oobfree = {
76 {2, 4}, {18, 4}, {34, 4}, {50, 4},
77 {66, 4}, {82, 4}, {98, 4}, {114, 4}
78 }
79};
80
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010081/**
82 * onenand_oob_64 - oob info for large (2KB) page
83 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020084static struct nand_ecclayout onenand_oob_64 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010085 .eccbytes = 20,
86 .eccpos = {
87 8, 9, 10, 11, 12,
88 24, 25, 26, 27, 28,
89 40, 41, 42, 43, 44,
90 56, 57, 58, 59, 60,
91 },
92 .oobfree = {
93 {2, 3}, {14, 2}, {18, 3}, {30, 2},
Jarkko Lavinend9777f12006-05-12 17:02:35 +030094 {34, 3}, {46, 2}, {50, 3}, {62, 2}
95 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010096};
97
98/**
99 * onenand_oob_32 - oob info for middle (1KB) page
100 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200101static struct nand_ecclayout onenand_oob_32 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100102 .eccbytes = 10,
103 .eccpos = {
104 8, 9, 10, 11, 12,
105 24, 25, 26, 27, 28,
106 },
107 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
108};
109
110static const unsigned char ffchars[] = {
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100127};
128
129/**
130 * onenand_readw - [OneNAND Interface] Read OneNAND register
131 * @param addr address to read
132 *
133 * Read OneNAND register
134 */
135static unsigned short onenand_readw(void __iomem *addr)
136{
137 return readw(addr);
138}
139
140/**
141 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
142 * @param value value to write
143 * @param addr address to write
144 *
145 * Write OneNAND register with value
146 */
147static void onenand_writew(unsigned short value, void __iomem *addr)
148{
149 writew(value, addr);
150}
151
152/**
153 * onenand_block_address - [DEFAULT] Get block address
Kyungmin Park83a36832005-09-29 04:53:16 +0100154 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100155 * @param block the block
156 * @return translated block address if DDP, otherwise same
157 *
158 * Setup Start Address 1 Register (F100h)
159 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100160static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100161{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900162 /* Device Flash Core select, NAND Flash Block Address */
163 if (block & this->density_mask)
164 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100165
166 return block;
167}
168
169/**
170 * onenand_bufferram_address - [DEFAULT] Get bufferram address
Kyungmin Park83a36832005-09-29 04:53:16 +0100171 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100172 * @param block the block
173 * @return set DBS value if DDP, otherwise 0
174 *
175 * Setup Start Address 2 Register (F101h) for DDP
176 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100177static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100178{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900179 /* Device BufferRAM Select */
180 if (block & this->density_mask)
181 return ONENAND_DDP_CHIP1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100182
Kyungmin Park738d61f2007-01-15 17:09:14 +0900183 return ONENAND_DDP_CHIP0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100184}
185
186/**
187 * onenand_page_address - [DEFAULT] Get page address
188 * @param page the page address
189 * @param sector the sector address
190 * @return combined page and sector address
191 *
192 * Setup Start Address 8 Register (F107h)
193 */
194static int onenand_page_address(int page, int sector)
195{
196 /* Flash Page Address, Flash Sector Address */
197 int fpa, fsa;
198
199 fpa = page & ONENAND_FPA_MASK;
200 fsa = sector & ONENAND_FSA_MASK;
201
202 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
203}
204
205/**
206 * onenand_buffer_address - [DEFAULT] Get buffer address
207 * @param dataram1 DataRAM index
208 * @param sectors the sector address
209 * @param count the number of sectors
210 * @return the start buffer value
211 *
212 * Setup Start Buffer Register (F200h)
213 */
214static int onenand_buffer_address(int dataram1, int sectors, int count)
215{
216 int bsa, bsc;
217
218 /* BufferRAM Sector Address */
219 bsa = sectors & ONENAND_BSA_MASK;
220
221 if (dataram1)
222 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
223 else
224 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
225
226 /* BufferRAM Sector Count */
227 bsc = count & ONENAND_BSC_MASK;
228
229 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
230}
231
232/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700233 * flexonenand_block- For given address return block number
234 * @param this - OneNAND device structure
235 * @param addr - Address for which block number is needed
236 */
237static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
238{
239 unsigned boundary, blk, die = 0;
240
241 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
242 die = 1;
243 addr -= this->diesize[0];
244 }
245
246 boundary = this->boundary[die];
247
248 blk = addr >> (this->erase_shift - 1);
249 if (blk > boundary)
250 blk = (blk + boundary + 1) >> 1;
251
252 blk += die ? this->density_mask : 0;
253 return blk;
254}
255
256inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
257{
258 if (!FLEXONENAND(this))
259 return addr >> this->erase_shift;
260 return flexonenand_block(this, addr);
261}
262
263/**
264 * flexonenand_addr - Return address of the block
265 * @this: OneNAND device structure
266 * @block: Block number on Flex-OneNAND
267 *
268 * Return address of the block
269 */
270static loff_t flexonenand_addr(struct onenand_chip *this, int block)
271{
272 loff_t ofs = 0;
273 int die = 0, boundary;
274
275 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
276 block -= this->density_mask;
277 die = 1;
278 ofs = this->diesize[0];
279 }
280
281 boundary = this->boundary[die];
282 ofs += (loff_t)block << (this->erase_shift - 1);
283 if (block > (boundary + 1))
284 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
285 return ofs;
286}
287
288loff_t onenand_addr(struct onenand_chip *this, int block)
289{
290 if (!FLEXONENAND(this))
291 return (loff_t)block << this->erase_shift;
292 return flexonenand_addr(this, block);
293}
294EXPORT_SYMBOL(onenand_addr);
295
296/**
Kyungmin Parke71f04f2007-12-11 11:23:45 +0900297 * onenand_get_density - [DEFAULT] Get OneNAND density
298 * @param dev_id OneNAND device ID
299 *
300 * Get OneNAND density from device ID
301 */
302static inline int onenand_get_density(int dev_id)
303{
304 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
305 return (density & ONENAND_DEVICE_DENSITY_MASK);
306}
307
308/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700309 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
310 * @param mtd MTD device structure
311 * @param addr address whose erase region needs to be identified
312 */
313int flexonenand_region(struct mtd_info *mtd, loff_t addr)
314{
315 int i;
316
317 for (i = 0; i < mtd->numeraseregions; i++)
318 if (addr < mtd->eraseregions[i].offset)
319 break;
320 return i - 1;
321}
322EXPORT_SYMBOL(flexonenand_region);
323
324/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100325 * onenand_command - [DEFAULT] Send command to OneNAND device
326 * @param mtd MTD device structure
327 * @param cmd the command to be sent
328 * @param addr offset to read from or write to
329 * @param len number of bytes to read or write
330 *
331 * Send command to OneNAND device. This function is used for middle/large page
332 * devices (1KB/2KB Bytes per page)
333 */
334static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
335{
336 struct onenand_chip *this = mtd->priv;
Kyungmin Parkb21b72c2007-12-11 11:13:18 +0900337 int value, block, page;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100338
339 /* Address translation */
340 switch (cmd) {
341 case ONENAND_CMD_UNLOCK:
342 case ONENAND_CMD_LOCK:
343 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Park28b79ff2006-09-26 09:45:28 +0000344 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100345 block = -1;
346 page = -1;
347 break;
348
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700349 case FLEXONENAND_CMD_PI_ACCESS:
350 /* addr contains die index */
351 block = addr * this->density_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100352 page = -1;
353 break;
354
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700355 case ONENAND_CMD_ERASE:
356 case ONENAND_CMD_BUFFERRAM:
357 case ONENAND_CMD_OTP_ACCESS:
358 block = onenand_block(this, addr);
359 page = -1;
360 break;
361
362 case FLEXONENAND_CMD_READ_PI:
363 cmd = ONENAND_CMD_READ;
364 block = addr * this->density_mask;
365 page = 0;
366 break;
367
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100368 default:
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700369 block = onenand_block(this, addr);
370 page = (int) (addr - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900371
372 if (ONENAND_IS_2PLANE(this)) {
373 /* Make the even block number */
374 block &= ~1;
375 /* Is it the odd plane? */
376 if (addr & this->writesize)
377 block++;
378 page >>= 1;
379 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100380 page &= this->page_mask;
381 break;
382 }
383
384 /* NOTE: The setting order of the registers is very important! */
385 if (cmd == ONENAND_CMD_BUFFERRAM) {
386 /* Select DataRAM for DDP */
Kyungmin Park83a36832005-09-29 04:53:16 +0100387 value = onenand_bufferram_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100388 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
389
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700390 if (ONENAND_IS_MLC(this) || ONENAND_IS_2PLANE(this))
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900391 /* It is always BufferRAM0 */
392 ONENAND_SET_BUFFERRAM0(this);
393 else
394 /* Switch to the next data buffer */
395 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100396
397 return 0;
398 }
399
400 if (block != -1) {
401 /* Write 'DFS, FBA' of Flash */
Kyungmin Park83a36832005-09-29 04:53:16 +0100402 value = onenand_block_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100403 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Park3cecf692006-05-12 17:02:51 +0300404
Kyungmin Parkb21b72c2007-12-11 11:13:18 +0900405 /* Select DataRAM for DDP */
406 value = onenand_bufferram_address(this, block);
407 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100408 }
409
410 if (page != -1) {
Kyungmin Park60d84f92006-12-22 16:21:54 +0900411 /* Now we use page size operation */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700412 int sectors = 0, count = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100413 int dataram;
414
415 switch (cmd) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700416 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100417 case ONENAND_CMD_READ:
418 case ONENAND_CMD_READOOB:
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700419 if (ONENAND_IS_MLC(this))
420 /* It is always BufferRAM0 */
421 dataram = ONENAND_SET_BUFFERRAM0(this);
422 else
423 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100424 break;
425
426 default:
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900427 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
428 cmd = ONENAND_CMD_2X_PROG;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100429 dataram = ONENAND_CURRENT_BUFFERRAM(this);
430 break;
431 }
432
433 /* Write 'FPA, FSA' of Flash */
434 value = onenand_page_address(page, sectors);
435 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
436
437 /* Write 'BSA, BSC' of DataRAM */
438 value = onenand_buffer_address(dataram, sectors, count);
439 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100440 }
441
442 /* Interrupt clear */
443 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
444
445 /* Write command */
446 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
447
448 return 0;
449}
450
451/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700452 * onenand_read_ecc - return ecc status
453 * @param this onenand chip structure
454 */
455static inline int onenand_read_ecc(struct onenand_chip *this)
456{
457 int ecc, i, result = 0;
458
459 if (!FLEXONENAND(this))
460 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
461
462 for (i = 0; i < 4; i++) {
463 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i);
464 if (likely(!ecc))
465 continue;
466 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
467 return ONENAND_ECC_2BIT_ALL;
468 else
469 result = ONENAND_ECC_1BIT_ALL;
470 }
471
472 return result;
473}
474
475/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100476 * onenand_wait - [DEFAULT] wait until the command is done
477 * @param mtd MTD device structure
478 * @param state state to select the max. timeout value
479 *
480 * Wait for command done. This applies to all OneNAND command
481 * Read can take up to 30us, erase up to 2ms and program up to 350us
482 * according to general OneNAND specs
483 */
484static int onenand_wait(struct mtd_info *mtd, int state)
485{
486 struct onenand_chip * this = mtd->priv;
487 unsigned long timeout;
488 unsigned int flags = ONENAND_INT_MASTER;
489 unsigned int interrupt = 0;
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900490 unsigned int ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100491
492 /* The 20 msec is enough */
493 timeout = jiffies + msecs_to_jiffies(20);
494 while (time_before(jiffies, timeout)) {
495 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
496
497 if (interrupt & flags)
498 break;
499
500 if (state != FL_READING)
501 cond_resched();
502 }
503 /* To get correct interrupt status in timeout case */
504 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
505
506 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
507
Kyungmin Park83973b82008-05-29 14:52:40 +0900508 /*
509 * In the Spec. it checks the controller status first
510 * However if you get the correct information in case of
511 * power off recovery (POR) test, it should read ECC status first
512 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100513 if (interrupt & ONENAND_INT_READ) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700514 int ecc = onenand_read_ecc(this);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900515 if (ecc) {
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900516 if (ecc & ONENAND_ECC_2BIT_ALL) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530517 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
518 __func__, ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900519 mtd->ecc_stats.failed++;
Adrian Hunter30a7eb22007-10-12 10:19:38 +0300520 return -EBADMSG;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300521 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530522 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
523 __func__, ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900524 mtd->ecc_stats.corrected++;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300525 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100526 }
Adrian Hunter9d032802007-01-10 07:51:26 +0200527 } else if (state == FL_READING) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530528 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
529 __func__, ctrl, interrupt);
Adrian Hunter9d032802007-01-10 07:51:26 +0200530 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100531 }
532
Kyungmin Park83973b82008-05-29 14:52:40 +0900533 /* If there's controller error, it's a real error */
534 if (ctrl & ONENAND_CTRL_ERROR) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530535 printk(KERN_ERR "%s: controller error = 0x%04x\n",
536 __func__, ctrl);
Kyungmin Park83973b82008-05-29 14:52:40 +0900537 if (ctrl & ONENAND_CTRL_LOCK)
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530538 printk(KERN_ERR "%s: it's locked error.\n", __func__);
Kyungmin Park83973b82008-05-29 14:52:40 +0900539 return -EIO;
540 }
541
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100542 return 0;
543}
544
Kyungmin Park2c221202006-11-16 11:23:48 +0900545/*
546 * onenand_interrupt - [DEFAULT] onenand interrupt handler
547 * @param irq onenand interrupt number
548 * @param dev_id interrupt data
549 *
550 * complete the work
551 */
552static irqreturn_t onenand_interrupt(int irq, void *data)
553{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400554 struct onenand_chip *this = data;
Kyungmin Park2c221202006-11-16 11:23:48 +0900555
556 /* To handle shared interrupt */
557 if (!this->complete.done)
558 complete(&this->complete);
559
560 return IRQ_HANDLED;
561}
562
563/*
564 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
565 * @param mtd MTD device structure
566 * @param state state to select the max. timeout value
567 *
568 * Wait for command done.
569 */
570static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
571{
572 struct onenand_chip *this = mtd->priv;
573
Kyungmin Park2c221202006-11-16 11:23:48 +0900574 wait_for_completion(&this->complete);
575
576 return onenand_wait(mtd, state);
577}
578
579/*
580 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
581 * @param mtd MTD device structure
582 * @param state state to select the max. timeout value
583 *
584 * Try interrupt based wait (It is used one-time)
585 */
586static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
587{
588 struct onenand_chip *this = mtd->priv;
589 unsigned long remain, timeout;
590
591 /* We use interrupt wait first */
592 this->wait = onenand_interrupt_wait;
593
Kyungmin Park2c221202006-11-16 11:23:48 +0900594 timeout = msecs_to_jiffies(100);
595 remain = wait_for_completion_timeout(&this->complete, timeout);
596 if (!remain) {
597 printk(KERN_INFO "OneNAND: There's no interrupt. "
598 "We use the normal wait\n");
599
600 /* Release the irq */
601 free_irq(this->irq, this);
David Woodhousec9ac5972006-11-30 08:17:38 +0000602
Kyungmin Park2c221202006-11-16 11:23:48 +0900603 this->wait = onenand_wait;
604 }
605
606 return onenand_wait(mtd, state);
607}
608
609/*
610 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
611 * @param mtd MTD device structure
612 *
613 * There's two method to wait onenand work
614 * 1. polling - read interrupt status register
615 * 2. interrupt - use the kernel interrupt method
616 */
617static void onenand_setup_wait(struct mtd_info *mtd)
618{
619 struct onenand_chip *this = mtd->priv;
620 int syscfg;
621
622 init_completion(&this->complete);
623
624 if (this->irq <= 0) {
625 this->wait = onenand_wait;
626 return;
627 }
628
629 if (request_irq(this->irq, &onenand_interrupt,
630 IRQF_SHARED, "onenand", this)) {
631 /* If we can't get irq, use the normal wait */
632 this->wait = onenand_wait;
633 return;
634 }
635
636 /* Enable interrupt */
637 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
638 syscfg |= ONENAND_SYS_CFG1_IOBE;
639 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
640
641 this->wait = onenand_try_interrupt_wait;
642}
643
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100644/**
645 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
646 * @param mtd MTD data structure
647 * @param area BufferRAM area
648 * @return offset given area
649 *
650 * Return BufferRAM offset given area
651 */
652static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
653{
654 struct onenand_chip *this = mtd->priv;
655
656 if (ONENAND_CURRENT_BUFFERRAM(this)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900657 /* Note: the 'this->writesize' is a real page size */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100658 if (area == ONENAND_DATARAM)
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900659 return this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100660 if (area == ONENAND_SPARERAM)
661 return mtd->oobsize;
662 }
663
664 return 0;
665}
666
667/**
668 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
669 * @param mtd MTD data structure
670 * @param area BufferRAM area
671 * @param buffer the databuffer to put/get data
672 * @param offset offset to read from or write to
673 * @param count number of bytes to read/write
674 *
675 * Read the BufferRAM area
676 */
677static int onenand_read_bufferram(struct mtd_info *mtd, int area,
678 unsigned char *buffer, int offset, size_t count)
679{
680 struct onenand_chip *this = mtd->priv;
681 void __iomem *bufferram;
682
683 bufferram = this->base + area;
684
685 bufferram += onenand_bufferram_offset(mtd, area);
686
Kyungmin Park9c01f872006-05-12 17:02:31 +0300687 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
688 unsigned short word;
689
690 /* Align with word(16-bit) size */
691 count--;
692
693 /* Read word and save byte */
694 word = this->read_word(bufferram + offset + count);
695 buffer[count] = (word & 0xff);
696 }
697
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100698 memcpy(buffer, bufferram + offset, count);
699
700 return 0;
701}
702
703/**
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100704 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
705 * @param mtd MTD data structure
706 * @param area BufferRAM area
707 * @param buffer the databuffer to put/get data
708 * @param offset offset to read from or write to
709 * @param count number of bytes to read/write
710 *
711 * Read the BufferRAM area with Sync. Burst Mode
712 */
713static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
714 unsigned char *buffer, int offset, size_t count)
715{
716 struct onenand_chip *this = mtd->priv;
717 void __iomem *bufferram;
718
719 bufferram = this->base + area;
720
721 bufferram += onenand_bufferram_offset(mtd, area);
722
723 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
724
Kyungmin Park9c01f872006-05-12 17:02:31 +0300725 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
726 unsigned short word;
727
728 /* Align with word(16-bit) size */
729 count--;
730
731 /* Read word and save byte */
732 word = this->read_word(bufferram + offset + count);
733 buffer[count] = (word & 0xff);
734 }
735
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100736 memcpy(buffer, bufferram + offset, count);
737
738 this->mmcontrol(mtd, 0);
739
740 return 0;
741}
742
743/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100744 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
745 * @param mtd MTD data structure
746 * @param area BufferRAM area
747 * @param buffer the databuffer to put/get data
748 * @param offset offset to read from or write to
749 * @param count number of bytes to read/write
750 *
751 * Write the BufferRAM area
752 */
753static int onenand_write_bufferram(struct mtd_info *mtd, int area,
754 const unsigned char *buffer, int offset, size_t count)
755{
756 struct onenand_chip *this = mtd->priv;
757 void __iomem *bufferram;
758
759 bufferram = this->base + area;
760
761 bufferram += onenand_bufferram_offset(mtd, area);
762
Kyungmin Park9c01f872006-05-12 17:02:31 +0300763 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
764 unsigned short word;
765 int byte_offset;
766
767 /* Align with word(16-bit) size */
768 count--;
769
770 /* Calculate byte access offset */
771 byte_offset = offset + count;
772
773 /* Read word and save byte */
774 word = this->read_word(bufferram + byte_offset);
775 word = (word & ~0xff) | buffer[count];
776 this->write_word(word, bufferram + byte_offset);
777 }
778
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100779 memcpy(bufferram + offset, buffer, count);
780
781 return 0;
782}
783
784/**
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900785 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
786 * @param mtd MTD data structure
787 * @param addr address to check
788 * @return blockpage address
789 *
790 * Get blockpage address at 2x program mode
791 */
792static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
793{
794 struct onenand_chip *this = mtd->priv;
795 int blockpage, block, page;
796
797 /* Calculate the even block number */
798 block = (int) (addr >> this->erase_shift) & ~1;
799 /* Is it the odd plane? */
800 if (addr & this->writesize)
801 block++;
802 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
803 blockpage = (block << 7) | page;
804
805 return blockpage;
806}
807
808/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100809 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
810 * @param mtd MTD data structure
811 * @param addr address to check
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000812 * @return 1 if there are valid data, otherwise 0
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100813 *
814 * Check bufferram if there is data we required
815 */
816static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
817{
818 struct onenand_chip *this = mtd->priv;
Adrian Huntercde36b32007-02-08 10:28:08 +0200819 int blockpage, found = 0;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900820 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000821
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900822 if (ONENAND_IS_2PLANE(this))
823 blockpage = onenand_get_2x_blockpage(mtd, addr);
824 else
825 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100826
827 /* Is there valid data? */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900828 i = ONENAND_CURRENT_BUFFERRAM(this);
829 if (this->bufferram[i].blockpage == blockpage)
Adrian Huntercde36b32007-02-08 10:28:08 +0200830 found = 1;
831 else {
832 /* Check another BufferRAM */
833 i = ONENAND_NEXT_BUFFERRAM(this);
834 if (this->bufferram[i].blockpage == blockpage) {
835 ONENAND_SET_NEXT_BUFFERRAM(this);
836 found = 1;
837 }
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900838 }
839
Adrian Huntercde36b32007-02-08 10:28:08 +0200840 if (found && ONENAND_IS_DDP(this)) {
841 /* Select DataRAM for DDP */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700842 int block = onenand_block(this, addr);
Adrian Huntercde36b32007-02-08 10:28:08 +0200843 int value = onenand_bufferram_address(this, block);
844 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
845 }
846
847 return found;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100848}
849
850/**
851 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
852 * @param mtd MTD data structure
853 * @param addr address to update
854 * @param valid valid flag
855 *
856 * Update BufferRAM information
857 */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900858static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100859 int valid)
860{
861 struct onenand_chip *this = mtd->priv;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900862 int blockpage;
863 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000864
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900865 if (ONENAND_IS_2PLANE(this))
866 blockpage = onenand_get_2x_blockpage(mtd, addr);
867 else
868 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100869
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900870 /* Invalidate another BufferRAM */
871 i = ONENAND_NEXT_BUFFERRAM(this);
Kyungmin Park5b4246f2007-02-02 09:39:21 +0900872 if (this->bufferram[i].blockpage == blockpage)
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900873 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100874
875 /* Update BufferRAM */
876 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900877 if (valid)
878 this->bufferram[i].blockpage = blockpage;
879 else
880 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100881}
882
883/**
Adrian Hunter480b9df2007-02-07 13:55:19 +0200884 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
885 * @param mtd MTD data structure
886 * @param addr start address to invalidate
887 * @param len length to invalidate
888 *
889 * Invalidate BufferRAM information
890 */
891static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
892 unsigned int len)
893{
894 struct onenand_chip *this = mtd->priv;
895 int i;
896 loff_t end_addr = addr + len;
897
898 /* Invalidate BufferRAM */
899 for (i = 0; i < MAX_BUFFERRAM; i++) {
900 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
901 if (buf_addr >= addr && buf_addr < end_addr)
902 this->bufferram[i].blockpage = -1;
903 }
904}
905
906/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100907 * onenand_get_device - [GENERIC] Get chip for selected access
908 * @param mtd MTD device structure
909 * @param new_state the state which is requested
910 *
911 * Get the device and lock it for exclusive access
912 */
Kyungmin Parka41371e2005-09-29 03:55:31 +0100913static int onenand_get_device(struct mtd_info *mtd, int new_state)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100914{
915 struct onenand_chip *this = mtd->priv;
916 DECLARE_WAITQUEUE(wait, current);
917
918 /*
919 * Grab the lock and see if the device is available
920 */
921 while (1) {
922 spin_lock(&this->chip_lock);
923 if (this->state == FL_READY) {
924 this->state = new_state;
925 spin_unlock(&this->chip_lock);
926 break;
927 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100928 if (new_state == FL_PM_SUSPENDED) {
929 spin_unlock(&this->chip_lock);
930 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
931 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100932 set_current_state(TASK_UNINTERRUPTIBLE);
933 add_wait_queue(&this->wq, &wait);
934 spin_unlock(&this->chip_lock);
935 schedule();
936 remove_wait_queue(&this->wq, &wait);
937 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100938
939 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100940}
941
942/**
943 * onenand_release_device - [GENERIC] release chip
944 * @param mtd MTD device structure
945 *
946 * Deselect, release chip lock and wake up anyone waiting on the device
947 */
948static void onenand_release_device(struct mtd_info *mtd)
949{
950 struct onenand_chip *this = mtd->priv;
951
952 /* Release the chip */
953 spin_lock(&this->chip_lock);
954 this->state = FL_READY;
955 wake_up(&this->wq);
956 spin_unlock(&this->chip_lock);
957}
958
959/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900960 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
961 * @param mtd MTD device structure
962 * @param buf destination address
963 * @param column oob offset to read from
964 * @param thislen oob length to read
965 */
966static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
967 int thislen)
968{
969 struct onenand_chip *this = mtd->priv;
970 struct nand_oobfree *free;
971 int readcol = column;
972 int readend = column + thislen;
973 int lastgap = 0;
974 unsigned int i;
975 uint8_t *oob_buf = this->oob_buf;
976
977 free = this->ecclayout->oobfree;
978 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
979 if (readcol >= lastgap)
980 readcol += free->offset - lastgap;
981 if (readend >= lastgap)
982 readend += free->offset - lastgap;
983 lastgap = free->offset + free->length;
984 }
985 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
986 free = this->ecclayout->oobfree;
987 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
988 int free_end = free->offset + free->length;
989 if (free->offset < readend && free_end > readcol) {
990 int st = max_t(int,free->offset,readcol);
991 int ed = min_t(int,free_end,readend);
992 int n = ed - st;
993 memcpy(buf, oob_buf + st, n);
994 buf += n;
995 } else if (column == 0)
996 break;
997 }
998 return 0;
999}
1000
1001/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001002 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1003 * @param mtd MTD device structure
1004 * @param addr address to recover
1005 * @param status return value from onenand_wait / onenand_bbt_wait
1006 *
1007 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1008 * lower page address and MSB page has higher page address in paired pages.
1009 * If power off occurs during MSB page program, the paired LSB page data can
1010 * become corrupt. LSB page recovery read is a way to read LSB page though page
1011 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1012 * read after power up, issue LSB page recovery read.
1013 */
1014static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1015{
1016 struct onenand_chip *this = mtd->priv;
1017 int i;
1018
1019 /* Recovery is only for Flex-OneNAND */
1020 if (!FLEXONENAND(this))
1021 return status;
1022
1023 /* check if we failed due to uncorrectable error */
1024 if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
1025 return status;
1026
1027 /* check if address lies in MLC region */
1028 i = flexonenand_region(mtd, addr);
1029 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1030 return status;
1031
1032 /* We are attempting to reread, so decrement stats.failed
1033 * which was incremented by onenand_wait due to read failure
1034 */
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301035 printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1036 __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001037 mtd->ecc_stats.failed--;
1038
1039 /* Issue the LSB page recovery command */
1040 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1041 return this->wait(mtd, FL_READING);
1042}
1043
1044/**
1045 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1046 * @param mtd MTD device structure
1047 * @param from offset to read from
1048 * @param ops: oob operation description structure
1049 *
1050 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1051 * So, read-while-load is not present.
1052 */
1053static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1054 struct mtd_oob_ops *ops)
1055{
1056 struct onenand_chip *this = mtd->priv;
1057 struct mtd_ecc_stats stats;
1058 size_t len = ops->len;
1059 size_t ooblen = ops->ooblen;
1060 u_char *buf = ops->datbuf;
1061 u_char *oobbuf = ops->oobbuf;
1062 int read = 0, column, thislen;
1063 int oobread = 0, oobcolumn, thisooblen, oobsize;
1064 int ret = 0;
1065 int writesize = this->writesize;
1066
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301067 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
David Woodhouse80327472009-10-05 08:30:04 +01001068 __func__, (unsigned int) from, (int) len);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001069
1070 if (ops->mode == MTD_OOB_AUTO)
1071 oobsize = this->ecclayout->oobavail;
1072 else
1073 oobsize = mtd->oobsize;
1074
1075 oobcolumn = from & (mtd->oobsize - 1);
1076
1077 /* Do not allow reads past end of device */
1078 if (from + len > mtd->size) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301079 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1080 __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001081 ops->retlen = 0;
1082 ops->oobretlen = 0;
1083 return -EINVAL;
1084 }
1085
1086 stats = mtd->ecc_stats;
1087
1088 while (read < len) {
1089 cond_resched();
1090
1091 thislen = min_t(int, writesize, len - read);
1092
1093 column = from & (writesize - 1);
1094 if (column + thislen > writesize)
1095 thislen = writesize - column;
1096
1097 if (!onenand_check_bufferram(mtd, from)) {
1098 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1099
1100 ret = this->wait(mtd, FL_READING);
1101 if (unlikely(ret))
1102 ret = onenand_recover_lsb(mtd, from, ret);
1103 onenand_update_bufferram(mtd, from, !ret);
1104 if (ret == -EBADMSG)
1105 ret = 0;
1106 }
1107
1108 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1109 if (oobbuf) {
1110 thisooblen = oobsize - oobcolumn;
1111 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1112
1113 if (ops->mode == MTD_OOB_AUTO)
1114 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1115 else
1116 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1117 oobread += thisooblen;
1118 oobbuf += thisooblen;
1119 oobcolumn = 0;
1120 }
1121
1122 read += thislen;
1123 if (read == len)
1124 break;
1125
1126 from += thislen;
1127 buf += thislen;
1128 }
1129
1130 /*
1131 * Return success, if no ECC failures, else -EBADMSG
1132 * fs driver will take care of that, because
1133 * retlen == desired len and result == -EBADMSG
1134 */
1135 ops->retlen = read;
1136 ops->oobretlen = oobread;
1137
1138 if (ret)
1139 return ret;
1140
1141 if (mtd->ecc_stats.failed - stats.failed)
1142 return -EBADMSG;
1143
1144 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1145}
1146
1147/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001148 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001149 * @param mtd MTD device structure
1150 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001151 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001152 *
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001153 * OneNAND read main and/or out-of-band data
1154 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001155static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001156 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001157{
1158 struct onenand_chip *this = mtd->priv;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001159 struct mtd_ecc_stats stats;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001160 size_t len = ops->len;
1161 size_t ooblen = ops->ooblen;
1162 u_char *buf = ops->datbuf;
1163 u_char *oobbuf = ops->oobbuf;
1164 int read = 0, column, thislen;
1165 int oobread = 0, oobcolumn, thisooblen, oobsize;
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001166 int ret = 0, boundary = 0;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001167 int writesize = this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001168
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301169 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
1170 __func__, (unsigned int) from, (int) len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001171
1172 if (ops->mode == MTD_OOB_AUTO)
1173 oobsize = this->ecclayout->oobavail;
1174 else
1175 oobsize = mtd->oobsize;
1176
1177 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001178
1179 /* Do not allow reads past end of device */
1180 if ((from + len) > mtd->size) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301181 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1182 __func__);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001183 ops->retlen = 0;
1184 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001185 return -EINVAL;
1186 }
1187
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001188 stats = mtd->ecc_stats;
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001189
Adrian Huntera8de85d2007-01-04 09:51:26 +02001190 /* Read-while-load method */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001191
Adrian Huntera8de85d2007-01-04 09:51:26 +02001192 /* Do first load to bufferRAM */
1193 if (read < len) {
1194 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001195 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001196 ret = this->wait(mtd, FL_READING);
1197 onenand_update_bufferram(mtd, from, !ret);
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001198 if (ret == -EBADMSG)
1199 ret = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001200 }
1201 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001202
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001203 thislen = min_t(int, writesize, len - read);
1204 column = from & (writesize - 1);
1205 if (column + thislen > writesize)
1206 thislen = writesize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001207
Adrian Huntera8de85d2007-01-04 09:51:26 +02001208 while (!ret) {
1209 /* If there is more to load then start next load */
1210 from += thislen;
1211 if (read + thislen < len) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001212 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001213 /*
1214 * Chip boundary handling in DDP
1215 * Now we issued chip 1 read and pointed chip 1
Mika Korhonen492e1502009-06-09 21:52:35 +03001216 * bufferram so we have to point chip 0 bufferram.
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001217 */
Kyungmin Park738d61f2007-01-15 17:09:14 +09001218 if (ONENAND_IS_DDP(this) &&
1219 unlikely(from == (this->chipsize >> 1))) {
1220 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001221 boundary = 1;
1222 } else
1223 boundary = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001224 ONENAND_SET_PREV_BUFFERRAM(this);
1225 }
1226 /* While load is going, read from last bufferRAM */
1227 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001228
1229 /* Read oob area if needed */
1230 if (oobbuf) {
1231 thisooblen = oobsize - oobcolumn;
1232 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1233
1234 if (ops->mode == MTD_OOB_AUTO)
1235 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1236 else
1237 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1238 oobread += thisooblen;
1239 oobbuf += thisooblen;
1240 oobcolumn = 0;
1241 }
1242
Adrian Huntera8de85d2007-01-04 09:51:26 +02001243 /* See if we are done */
1244 read += thislen;
1245 if (read == len)
1246 break;
1247 /* Set up for next read from bufferRAM */
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001248 if (unlikely(boundary))
Kyungmin Park738d61f2007-01-15 17:09:14 +09001249 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001250 ONENAND_SET_NEXT_BUFFERRAM(this);
1251 buf += thislen;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001252 thislen = min_t(int, writesize, len - read);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001253 column = 0;
1254 cond_resched();
1255 /* Now wait for load */
1256 ret = this->wait(mtd, FL_READING);
1257 onenand_update_bufferram(mtd, from, !ret);
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001258 if (ret == -EBADMSG)
1259 ret = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001260 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001261
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001262 /*
1263 * Return success, if no ECC failures, else -EBADMSG
1264 * fs driver will take care of that, because
1265 * retlen == desired len and result == -EBADMSG
1266 */
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001267 ops->retlen = read;
1268 ops->oobretlen = oobread;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001269
Adrian Huntera8de85d2007-01-04 09:51:26 +02001270 if (ret)
1271 return ret;
1272
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001273 if (mtd->ecc_stats.failed - stats.failed)
1274 return -EBADMSG;
1275
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001276 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001277}
1278
1279/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001280 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001281 * @param mtd MTD device structure
1282 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001283 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001284 *
1285 * OneNAND read out-of-band data from the spare area
1286 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001287static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Park12f77c92007-08-30 09:36:05 +09001288 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001289{
1290 struct onenand_chip *this = mtd->priv;
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001291 struct mtd_ecc_stats stats;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001292 int read = 0, thislen, column, oobsize;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001293 size_t len = ops->ooblen;
1294 mtd_oob_mode_t mode = ops->mode;
1295 u_char *buf = ops->oobbuf;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001296 int ret = 0, readcmd;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001297
Kyungmin Park12f77c92007-08-30 09:36:05 +09001298 from += ops->ooboffs;
1299
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301300 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
1301 __func__, (unsigned int) from, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001302
1303 /* Initialize return length value */
Kyungmin Park12f77c92007-08-30 09:36:05 +09001304 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001305
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001306 if (mode == MTD_OOB_AUTO)
1307 oobsize = this->ecclayout->oobavail;
1308 else
1309 oobsize = mtd->oobsize;
1310
1311 column = from & (mtd->oobsize - 1);
1312
1313 if (unlikely(column >= oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301314 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1315 __func__);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001316 return -EINVAL;
1317 }
1318
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001319 /* Do not allow reads past end of device */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001320 if (unlikely(from >= mtd->size ||
1321 column + len > ((mtd->size >> this->page_shift) -
1322 (from >> this->page_shift)) * oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301323 printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
1324 __func__);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001325 return -EINVAL;
1326 }
1327
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001328 stats = mtd->ecc_stats;
1329
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001330 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1331
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001332 while (read < len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001333 cond_resched();
1334
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001335 thislen = oobsize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001336 thislen = min_t(int, thislen, len);
1337
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001338 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001339
1340 onenand_update_bufferram(mtd, from, 0);
1341
1342 ret = this->wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001343 if (unlikely(ret))
1344 ret = onenand_recover_lsb(mtd, from, ret);
1345
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001346 if (ret && ret != -EBADMSG) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301347 printk(KERN_ERR "%s: read failed = 0x%x\n",
1348 __func__, ret);
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001349 break;
1350 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001351
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001352 if (mode == MTD_OOB_AUTO)
1353 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1354 else
1355 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001356
1357 read += thislen;
1358
1359 if (read == len)
1360 break;
1361
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001362 buf += thislen;
1363
1364 /* Read more? */
1365 if (read < len) {
1366 /* Page size */
Joern Engel28318772006-05-22 23:18:05 +02001367 from += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001368 column = 0;
1369 }
1370 }
1371
Kyungmin Park12f77c92007-08-30 09:36:05 +09001372 ops->oobretlen = read;
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001373
1374 if (ret)
1375 return ret;
1376
1377 if (mtd->ecc_stats.failed - stats.failed)
1378 return -EBADMSG;
1379
1380 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001381}
1382
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001383/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001384 * onenand_read - [MTD Interface] Read data from flash
1385 * @param mtd MTD device structure
1386 * @param from offset to read from
1387 * @param len number of bytes to read
1388 * @param retlen pointer to variable to store the number of read bytes
1389 * @param buf the databuffer to put data
1390 *
1391 * Read with ecc
1392*/
1393static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1394 size_t *retlen, u_char *buf)
1395{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001396 struct onenand_chip *this = mtd->priv;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001397 struct mtd_oob_ops ops = {
1398 .len = len,
1399 .ooblen = 0,
1400 .datbuf = buf,
1401 .oobbuf = NULL,
1402 };
1403 int ret;
1404
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001405 onenand_get_device(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001406 ret = ONENAND_IS_MLC(this) ?
1407 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
1408 onenand_read_ops_nolock(mtd, from, &ops);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001409 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001410
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001411 *retlen = ops.retlen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001412 return ret;
1413}
1414
1415/**
1416 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09001417 * @param mtd: MTD device structure
1418 * @param from: offset to read from
1419 * @param ops: oob operation description structure
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001420
1421 * Read main and/or out-of-band
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001422 */
1423static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1424 struct mtd_oob_ops *ops)
1425{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001426 struct onenand_chip *this = mtd->priv;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001427 int ret;
1428
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001429 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001430 case MTD_OOB_PLACE:
1431 case MTD_OOB_AUTO:
1432 break;
1433 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001434 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001435 default:
1436 return -EINVAL;
1437 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001438
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001439 onenand_get_device(mtd, FL_READING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001440 if (ops->datbuf)
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001441 ret = ONENAND_IS_MLC(this) ?
1442 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1443 onenand_read_ops_nolock(mtd, from, ops);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001444 else
1445 ret = onenand_read_oob_nolock(mtd, from, ops);
1446 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001447
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001448 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001449}
1450
Kyungmin Park211ac752007-02-07 12:15:01 +09001451/**
1452 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1453 * @param mtd MTD device structure
1454 * @param state state to select the max. timeout value
1455 *
1456 * Wait for command done.
1457 */
1458static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1459{
1460 struct onenand_chip *this = mtd->priv;
1461 unsigned long timeout;
1462 unsigned int interrupt;
1463 unsigned int ctrl;
1464
1465 /* The 20 msec is enough */
1466 timeout = jiffies + msecs_to_jiffies(20);
1467 while (time_before(jiffies, timeout)) {
1468 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1469 if (interrupt & ONENAND_INT_MASTER)
1470 break;
1471 }
1472 /* To get correct interrupt status in timeout case */
1473 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1474 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1475
Kyungmin Park211ac752007-02-07 12:15:01 +09001476 if (interrupt & ONENAND_INT_READ) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001477 int ecc = onenand_read_ecc(this);
Kyungmin Park83973b82008-05-29 14:52:40 +09001478 if (ecc & ONENAND_ECC_2BIT_ALL) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301479 printk(KERN_WARNING "%s: ecc error = 0x%04x, "
1480 "controller error 0x%04x\n",
1481 __func__, ecc, ctrl);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001482 return ONENAND_BBT_READ_ECC_ERROR;
Kyungmin Park83973b82008-05-29 14:52:40 +09001483 }
Kyungmin Park211ac752007-02-07 12:15:01 +09001484 } else {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301485 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
1486 __func__, ctrl, interrupt);
Kyungmin Park211ac752007-02-07 12:15:01 +09001487 return ONENAND_BBT_READ_FATAL_ERROR;
1488 }
1489
Kyungmin Park83973b82008-05-29 14:52:40 +09001490 /* Initial bad block case: 0x2400 or 0x0400 */
1491 if (ctrl & ONENAND_CTRL_ERROR) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301492 printk(KERN_DEBUG "%s: controller error = 0x%04x\n",
1493 __func__, ctrl);
Kyungmin Park83973b82008-05-29 14:52:40 +09001494 return ONENAND_BBT_READ_ERROR;
1495 }
1496
Kyungmin Park211ac752007-02-07 12:15:01 +09001497 return 0;
1498}
1499
1500/**
1501 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1502 * @param mtd MTD device structure
1503 * @param from offset to read from
Kyungmin Parke3da8062007-02-15 09:36:39 +09001504 * @param ops oob operation description structure
Kyungmin Park211ac752007-02-07 12:15:01 +09001505 *
1506 * OneNAND read out-of-band data from the spare area for bbt scan
1507 */
1508int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1509 struct mtd_oob_ops *ops)
1510{
1511 struct onenand_chip *this = mtd->priv;
1512 int read = 0, thislen, column;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001513 int ret = 0, readcmd;
Kyungmin Park211ac752007-02-07 12:15:01 +09001514 size_t len = ops->ooblen;
1515 u_char *buf = ops->oobbuf;
1516
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301517 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %zi\n",
1518 __func__, (unsigned int) from, len);
Kyungmin Park211ac752007-02-07 12:15:01 +09001519
1520 /* Initialize return value */
1521 ops->oobretlen = 0;
1522
1523 /* Do not allow reads past end of device */
1524 if (unlikely((from + len) > mtd->size)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301525 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1526 __func__);
Kyungmin Park211ac752007-02-07 12:15:01 +09001527 return ONENAND_BBT_READ_FATAL_ERROR;
1528 }
1529
1530 /* Grab the lock and see if the device is available */
1531 onenand_get_device(mtd, FL_READING);
1532
1533 column = from & (mtd->oobsize - 1);
1534
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001535 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1536
Kyungmin Park211ac752007-02-07 12:15:01 +09001537 while (read < len) {
1538 cond_resched();
1539
1540 thislen = mtd->oobsize - column;
1541 thislen = min_t(int, thislen, len);
1542
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001543 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park211ac752007-02-07 12:15:01 +09001544
1545 onenand_update_bufferram(mtd, from, 0);
1546
Kyungmin Park31bb9992009-05-12 13:46:57 -07001547 ret = this->bbt_wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001548 if (unlikely(ret))
1549 ret = onenand_recover_lsb(mtd, from, ret);
1550
Kyungmin Park211ac752007-02-07 12:15:01 +09001551 if (ret)
1552 break;
1553
1554 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1555 read += thislen;
1556 if (read == len)
1557 break;
1558
1559 buf += thislen;
1560
1561 /* Read more? */
1562 if (read < len) {
1563 /* Update Page size */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001564 from += this->writesize;
Kyungmin Park211ac752007-02-07 12:15:01 +09001565 column = 0;
1566 }
1567 }
1568
1569 /* Deselect and wake up anyone waiting on the device */
1570 onenand_release_device(mtd);
1571
1572 ops->oobretlen = read;
1573 return ret;
1574}
1575
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001576#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1577/**
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001578 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1579 * @param mtd MTD device structure
1580 * @param buf the databuffer to verify
1581 * @param to offset to read from
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001582 */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001583static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001584{
1585 struct onenand_chip *this = mtd->priv;
Kyungmin Park69d79182007-12-14 14:47:21 +09001586 u_char *oob_buf = this->oob_buf;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001587 int status, i, readcmd;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001588
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001589 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1590
1591 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001592 onenand_update_bufferram(mtd, to, 0);
1593 status = this->wait(mtd, FL_READING);
1594 if (status)
1595 return status;
1596
Kyungmin Park69d79182007-12-14 14:47:21 +09001597 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park91014e92007-02-12 10:34:39 +09001598 for (i = 0; i < mtd->oobsize; i++)
Kyungmin Park69d79182007-12-14 14:47:21 +09001599 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001600 return -EBADMSG;
1601
1602 return 0;
1603}
1604
1605/**
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001606 * onenand_verify - [GENERIC] verify the chip contents after a write
1607 * @param mtd MTD device structure
1608 * @param buf the databuffer to verify
1609 * @param addr offset to read from
1610 * @param len number of bytes to read and compare
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001611 */
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001612static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001613{
1614 struct onenand_chip *this = mtd->priv;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001615 void __iomem *dataram;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001616 int ret = 0;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001617 int thislen, column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001618
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001619 while (len != 0) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001620 thislen = min_t(int, this->writesize, len);
1621 column = addr & (this->writesize - 1);
1622 if (column + thislen > this->writesize)
1623 thislen = this->writesize - column;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001624
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001625 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001626
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001627 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001628
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001629 ret = this->wait(mtd, FL_READING);
1630 if (ret)
1631 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001632
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001633 onenand_update_bufferram(mtd, addr, 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001634
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001635 dataram = this->base + ONENAND_DATARAM;
1636 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1637
1638 if (memcmp(buf, dataram + column, thislen))
1639 return -EBADMSG;
1640
1641 len -= thislen;
1642 buf += thislen;
1643 addr += thislen;
1644 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001645
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001646 return 0;
1647}
1648#else
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001649#define onenand_verify(...) (0)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001650#define onenand_verify_oob(...) (0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001651#endif
1652
Kyungmin Park60d84f92006-12-22 16:21:54 +09001653#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001654
Richard Purdie6c77fd62008-02-06 10:18:22 +00001655static void onenand_panic_wait(struct mtd_info *mtd)
1656{
1657 struct onenand_chip *this = mtd->priv;
1658 unsigned int interrupt;
1659 int i;
1660
1661 for (i = 0; i < 2000; i++) {
1662 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1663 if (interrupt & ONENAND_INT_MASTER)
1664 break;
1665 udelay(10);
1666 }
1667}
1668
1669/**
1670 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1671 * @param mtd MTD device structure
1672 * @param to offset to write to
1673 * @param len number of bytes to write
1674 * @param retlen pointer to variable to store the number of written bytes
1675 * @param buf the data to write
1676 *
1677 * Write with ECC
1678 */
1679static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1680 size_t *retlen, const u_char *buf)
1681{
1682 struct onenand_chip *this = mtd->priv;
1683 int column, subpage;
1684 int written = 0;
1685 int ret = 0;
1686
1687 if (this->state == FL_PM_SUSPENDED)
1688 return -EBUSY;
1689
1690 /* Wait for any existing operation to clear */
1691 onenand_panic_wait(mtd);
1692
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301693 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
1694 __func__, (unsigned int) to, (int) len);
Richard Purdie6c77fd62008-02-06 10:18:22 +00001695
1696 /* Initialize retlen, in case of early exit */
1697 *retlen = 0;
1698
1699 /* Do not allow writes past end of device */
1700 if (unlikely((to + len) > mtd->size)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301701 printk(KERN_ERR "%s: Attempt write to past end of device\n",
1702 __func__);
Richard Purdie6c77fd62008-02-06 10:18:22 +00001703 return -EINVAL;
1704 }
1705
1706 /* Reject writes, which are not page aligned */
Roel Kluinb73d7e42008-02-16 18:14:35 +01001707 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301708 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1709 __func__);
Richard Purdie6c77fd62008-02-06 10:18:22 +00001710 return -EINVAL;
1711 }
1712
1713 column = to & (mtd->writesize - 1);
1714
1715 /* Loop until all data write */
1716 while (written < len) {
1717 int thislen = min_t(int, mtd->writesize - column, len - written);
1718 u_char *wbuf = (u_char *) buf;
1719
1720 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1721
1722 /* Partial page write */
1723 subpage = thislen < mtd->writesize;
1724 if (subpage) {
1725 memset(this->page_buf, 0xff, mtd->writesize);
1726 memcpy(this->page_buf + column, buf, thislen);
1727 wbuf = this->page_buf;
1728 }
1729
1730 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1731 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1732
1733 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1734
1735 onenand_panic_wait(mtd);
1736
1737 /* In partial page write we don't update bufferram */
1738 onenand_update_bufferram(mtd, to, !ret && !subpage);
1739 if (ONENAND_IS_2PLANE(this)) {
1740 ONENAND_SET_BUFFERRAM1(this);
1741 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1742 }
1743
1744 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301745 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
Richard Purdie6c77fd62008-02-06 10:18:22 +00001746 break;
1747 }
1748
1749 written += thislen;
1750
1751 if (written == len)
1752 break;
1753
1754 column = 0;
1755 to += thislen;
1756 buf += thislen;
1757 }
1758
1759 *retlen = written;
1760 return ret;
1761}
1762
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001763/**
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001764 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1765 * @param mtd MTD device structure
1766 * @param oob_buf oob buffer
1767 * @param buf source address
1768 * @param column oob offset to write to
1769 * @param thislen oob length to write
1770 */
1771static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1772 const u_char *buf, int column, int thislen)
1773{
1774 struct onenand_chip *this = mtd->priv;
1775 struct nand_oobfree *free;
1776 int writecol = column;
1777 int writeend = column + thislen;
1778 int lastgap = 0;
Kyungmin Parkad286342007-03-23 10:19:52 +09001779 unsigned int i;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001780
Kyungmin Parkad286342007-03-23 10:19:52 +09001781 free = this->ecclayout->oobfree;
1782 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001783 if (writecol >= lastgap)
1784 writecol += free->offset - lastgap;
1785 if (writeend >= lastgap)
1786 writeend += free->offset - lastgap;
1787 lastgap = free->offset + free->length;
1788 }
Kyungmin Parkad286342007-03-23 10:19:52 +09001789 free = this->ecclayout->oobfree;
1790 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001791 int free_end = free->offset + free->length;
1792 if (free->offset < writeend && free_end > writecol) {
1793 int st = max_t(int,free->offset,writecol);
1794 int ed = min_t(int,free_end,writeend);
1795 int n = ed - st;
1796 memcpy(oob_buf + st, buf, n);
1797 buf += n;
Adrian Hunterc36c46d2007-03-23 17:16:22 +09001798 } else if (column == 0)
Kyungmin Park5bc399e2007-03-09 09:41:07 +09001799 break;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001800 }
1801 return 0;
1802}
1803
1804/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001805 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001806 * @param mtd MTD device structure
1807 * @param to offset to write to
1808 * @param ops oob operation description structure
1809 *
1810 * Write main and/or oob with ECC
1811 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001812static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001813 struct mtd_oob_ops *ops)
1814{
1815 struct onenand_chip *this = mtd->priv;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001816 int written = 0, column, thislen = 0, subpage = 0;
1817 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001818 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1819 size_t len = ops->len;
1820 size_t ooblen = ops->ooblen;
1821 const u_char *buf = ops->datbuf;
1822 const u_char *oob = ops->oobbuf;
1823 u_char *oobbuf;
1824 int ret = 0;
1825
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301826 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
1827 __func__, (unsigned int) to, (int) len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001828
1829 /* Initialize retlen, in case of early exit */
1830 ops->retlen = 0;
1831 ops->oobretlen = 0;
1832
1833 /* Do not allow writes past end of device */
1834 if (unlikely((to + len) > mtd->size)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301835 printk(KERN_ERR "%s: Attempt write to past end of device\n",
1836 __func__);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001837 return -EINVAL;
1838 }
1839
1840 /* Reject writes, which are not page aligned */
Roel Kluinb73d7e42008-02-16 18:14:35 +01001841 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301842 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1843 __func__);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001844 return -EINVAL;
1845 }
1846
Kyungmin Park9ce96902008-11-17 17:54:28 +09001847 /* Check zero length */
1848 if (!len)
1849 return 0;
1850
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001851 if (ops->mode == MTD_OOB_AUTO)
1852 oobsize = this->ecclayout->oobavail;
1853 else
1854 oobsize = mtd->oobsize;
1855
1856 oobcolumn = to & (mtd->oobsize - 1);
1857
1858 column = to & (mtd->writesize - 1);
1859
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001860 /* Loop until all data write */
Kyungmin Park9ce96902008-11-17 17:54:28 +09001861 while (1) {
1862 if (written < len) {
1863 u_char *wbuf = (u_char *) buf;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001864
Kyungmin Park9ce96902008-11-17 17:54:28 +09001865 thislen = min_t(int, mtd->writesize - column, len - written);
1866 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001867
Kyungmin Park9ce96902008-11-17 17:54:28 +09001868 cond_resched();
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001869
Kyungmin Park9ce96902008-11-17 17:54:28 +09001870 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001871
Kyungmin Park9ce96902008-11-17 17:54:28 +09001872 /* Partial page write */
1873 subpage = thislen < mtd->writesize;
1874 if (subpage) {
1875 memset(this->page_buf, 0xff, mtd->writesize);
1876 memcpy(this->page_buf + column, buf, thislen);
1877 wbuf = this->page_buf;
1878 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001879
Kyungmin Park9ce96902008-11-17 17:54:28 +09001880 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001881
Kyungmin Park9ce96902008-11-17 17:54:28 +09001882 if (oob) {
1883 oobbuf = this->oob_buf;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001884
Kyungmin Park9ce96902008-11-17 17:54:28 +09001885 /* We send data to spare ram with oobsize
1886 * to prevent byte access */
1887 memset(oobbuf, 0xff, mtd->oobsize);
1888 if (ops->mode == MTD_OOB_AUTO)
1889 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1890 else
1891 memcpy(oobbuf + oobcolumn, oob, thisooblen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001892
Kyungmin Park9ce96902008-11-17 17:54:28 +09001893 oobwritten += thisooblen;
1894 oob += thisooblen;
1895 oobcolumn = 0;
1896 } else
1897 oobbuf = (u_char *) ffchars;
1898
1899 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001900 } else
Kyungmin Park9ce96902008-11-17 17:54:28 +09001901 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001902
Kyungmin Park9ce96902008-11-17 17:54:28 +09001903 /*
Mika Korhonen492e1502009-06-09 21:52:35 +03001904 * 2 PLANE, MLC, and Flex-OneNAND do not support
1905 * write-while-program feature.
Kyungmin Park9ce96902008-11-17 17:54:28 +09001906 */
1907 if (!ONENAND_IS_2PLANE(this) && !first) {
1908 ONENAND_SET_PREV_BUFFERRAM(this);
1909
1910 ret = this->wait(mtd, FL_WRITING);
1911
1912 /* In partial page write we don't update bufferram */
1913 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1914 if (ret) {
1915 written -= prevlen;
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301916 printk(KERN_ERR "%s: write failed %d\n",
1917 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001918 break;
1919 }
1920
1921 if (written == len) {
1922 /* Only check verify write turn on */
1923 ret = onenand_verify(mtd, buf - len, to - len, len);
1924 if (ret)
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301925 printk(KERN_ERR "%s: verify failed %d\n",
1926 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001927 break;
1928 }
1929
1930 ONENAND_SET_NEXT_BUFFERRAM(this);
1931 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001932
1933 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1934
Kyungmin Park9ce96902008-11-17 17:54:28 +09001935 /*
1936 * 2 PLANE, MLC, and Flex-OneNAND wait here
1937 */
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001938 if (ONENAND_IS_2PLANE(this)) {
Kyungmin Park9ce96902008-11-17 17:54:28 +09001939 ret = this->wait(mtd, FL_WRITING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001940
Kyungmin Park9ce96902008-11-17 17:54:28 +09001941 /* In partial page write we don't update bufferram */
1942 onenand_update_bufferram(mtd, to, !ret && !subpage);
1943 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301944 printk(KERN_ERR "%s: write failed %d\n",
1945 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001946 break;
1947 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001948
Kyungmin Park9ce96902008-11-17 17:54:28 +09001949 /* Only check verify write turn on */
1950 ret = onenand_verify(mtd, buf, to, thislen);
1951 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301952 printk(KERN_ERR "%s: verify failed %d\n",
1953 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001954 break;
1955 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001956
Kyungmin Park9ce96902008-11-17 17:54:28 +09001957 written += thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001958
Kyungmin Park9ce96902008-11-17 17:54:28 +09001959 if (written == len)
1960 break;
1961
1962 } else
1963 written += thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001964
1965 column = 0;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001966 prev_subpage = subpage;
1967 prev = to;
1968 prevlen = thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001969 to += thislen;
1970 buf += thislen;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001971 first = 0;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001972 }
1973
Kyungmin Park9ce96902008-11-17 17:54:28 +09001974 /* In error case, clear all bufferrams */
1975 if (written != len)
1976 onenand_invalidate_bufferram(mtd, 0, -1);
1977
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001978 ops->retlen = written;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001979 ops->oobretlen = oobwritten;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001980
1981 return ret;
1982}
1983
1984
1985/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001986 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001987 * @param mtd MTD device structure
1988 * @param to offset to write to
1989 * @param len number of bytes to write
1990 * @param retlen pointer to variable to store the number of written bytes
1991 * @param buf the data to write
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001992 * @param mode operation mode
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001993 *
1994 * OneNAND write out-of-band
1995 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001996static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1997 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001998{
1999 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002000 int column, ret = 0, oobsize;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002001 int written = 0, oobcmd;
Kyungmin Park91014e92007-02-12 10:34:39 +09002002 u_char *oobbuf;
Kyungmin Park12f77c92007-08-30 09:36:05 +09002003 size_t len = ops->ooblen;
2004 const u_char *buf = ops->oobbuf;
2005 mtd_oob_mode_t mode = ops->mode;
2006
2007 to += ops->ooboffs;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002008
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302009 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2010 __func__, (unsigned int) to, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002011
2012 /* Initialize retlen, in case of early exit */
Kyungmin Park12f77c92007-08-30 09:36:05 +09002013 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002014
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002015 if (mode == MTD_OOB_AUTO)
2016 oobsize = this->ecclayout->oobavail;
2017 else
2018 oobsize = mtd->oobsize;
2019
2020 column = to & (mtd->oobsize - 1);
2021
2022 if (unlikely(column >= oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302023 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2024 __func__);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002025 return -EINVAL;
2026 }
2027
Adrian Hunter52e42002007-02-06 09:15:39 +09002028 /* For compatibility with NAND: Do not allow write past end of page */
Kyungmin Park91014e92007-02-12 10:34:39 +09002029 if (unlikely(column + len > oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302030 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2031 __func__);
Adrian Hunter52e42002007-02-06 09:15:39 +09002032 return -EINVAL;
2033 }
2034
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002035 /* Do not allow reads past end of device */
2036 if (unlikely(to >= mtd->size ||
2037 column + len > ((mtd->size >> this->page_shift) -
2038 (to >> this->page_shift)) * oobsize)) {
David Woodhouse80327472009-10-05 08:30:04 +01002039 printk(KERN_ERR "%s: Attempted to write past end of device\n",
2040 __func__);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002041 return -EINVAL;
2042 }
2043
Kyungmin Park470bc842007-03-09 10:08:11 +09002044 oobbuf = this->oob_buf;
Kyungmin Park91014e92007-02-12 10:34:39 +09002045
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002046 oobcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
2047
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002048 /* Loop until all data write */
2049 while (written < len) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002050 int thislen = min_t(int, oobsize, len - written);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002051
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09002052 cond_resched();
2053
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002054 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2055
Kyungmin Park34c10602006-05-12 17:02:46 +03002056 /* We send data to spare ram with oobsize
2057 * to prevent byte access */
Kyungmin Park91014e92007-02-12 10:34:39 +09002058 memset(oobbuf, 0xff, mtd->oobsize);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002059 if (mode == MTD_OOB_AUTO)
Kyungmin Park91014e92007-02-12 10:34:39 +09002060 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002061 else
Kyungmin Park91014e92007-02-12 10:34:39 +09002062 memcpy(oobbuf + column, buf, thislen);
2063 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002064
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002065 if (ONENAND_IS_MLC(this)) {
2066 /* Set main area of DataRAM to 0xff*/
2067 memset(this->page_buf, 0xff, mtd->writesize);
2068 this->write_bufferram(mtd, ONENAND_DATARAM,
2069 this->page_buf, 0, mtd->writesize);
2070 }
2071
2072 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002073
2074 onenand_update_bufferram(mtd, to, 0);
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002075 if (ONENAND_IS_2PLANE(this)) {
2076 ONENAND_SET_BUFFERRAM1(this);
2077 onenand_update_bufferram(mtd, to + this->writesize, 0);
2078 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002079
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002080 ret = this->wait(mtd, FL_WRITING);
2081 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302082 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09002083 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002084 }
2085
Kyungmin Park91014e92007-02-12 10:34:39 +09002086 ret = onenand_verify_oob(mtd, oobbuf, to);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002087 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302088 printk(KERN_ERR "%s: verify failed %d\n",
2089 __func__, ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09002090 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002091 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002092
2093 written += thislen;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002094 if (written == len)
2095 break;
2096
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002097 to += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002098 buf += thislen;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002099 column = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002100 }
2101
Kyungmin Park12f77c92007-08-30 09:36:05 +09002102 ops->oobretlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002103
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002104 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002105}
2106
2107/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002108 * onenand_write - [MTD Interface] write buffer to FLASH
2109 * @param mtd MTD device structure
2110 * @param to offset to write to
2111 * @param len number of bytes to write
2112 * @param retlen pointer to variable to store the number of written bytes
2113 * @param buf the data to write
2114 *
2115 * Write with ECC
2116 */
2117static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
2118 size_t *retlen, const u_char *buf)
2119{
2120 struct mtd_oob_ops ops = {
2121 .len = len,
2122 .ooblen = 0,
2123 .datbuf = (u_char *) buf,
2124 .oobbuf = NULL,
2125 };
2126 int ret;
2127
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002128 onenand_get_device(mtd, FL_WRITING);
2129 ret = onenand_write_ops_nolock(mtd, to, &ops);
2130 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002131
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002132 *retlen = ops.retlen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002133 return ret;
2134}
2135
2136/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002137 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09002138 * @param mtd: MTD device structure
2139 * @param to: offset to write
2140 * @param ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002141 */
2142static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2143 struct mtd_oob_ops *ops)
2144{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002145 int ret;
2146
Kyungmin Park4f4fad22007-02-02 09:22:21 +09002147 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002148 case MTD_OOB_PLACE:
2149 case MTD_OOB_AUTO:
2150 break;
2151 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09002152 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002153 default:
2154 return -EINVAL;
2155 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002156
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002157 onenand_get_device(mtd, FL_WRITING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002158 if (ops->datbuf)
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002159 ret = onenand_write_ops_nolock(mtd, to, ops);
2160 else
2161 ret = onenand_write_oob_nolock(mtd, to, ops);
2162 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002163
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002164 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165}
2166
2167/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002168 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002169 * @param mtd MTD device structure
2170 * @param ofs offset from device start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002171 * @param allowbbt 1, if its allowed to access the bbt area
2172 *
2173 * Check, if the block is bad. Either by reading the bad block table or
2174 * calling of the scan function.
2175 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002176static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002177{
2178 struct onenand_chip *this = mtd->priv;
2179 struct bbm_info *bbm = this->bbm;
2180
2181 /* Return info from the table */
2182 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2183}
2184
2185/**
Mika Korhonen73885ae2009-10-23 07:50:42 +02002186 * onenand_block_by_block_erase - [Internal] erase block(s) using regular erase
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002187 * @param mtd MTD device structure
2188 * @param instr erase instruction
Mika Korhonen73885ae2009-10-23 07:50:42 +02002189 * @param region erase region
2190 * @param block_size erase block size
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002191 *
Mika Korhonen73885ae2009-10-23 07:50:42 +02002192 * Erase one or more blocks one block at a time
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002193 */
Mika Korhonen73885ae2009-10-23 07:50:42 +02002194static int onenand_block_by_block_erase(struct mtd_info *mtd,
2195 struct erase_info *instr,
2196 struct mtd_erase_region_info *region,
2197 unsigned int block_size)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002198{
2199 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002200 loff_t addr = instr->addr;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002201 int len = instr->len;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002202 loff_t region_end = 0;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002203 int ret = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002204
Mika Korhonen73885ae2009-10-23 07:50:42 +02002205 if (region) {
2206 /* region is set for Flex-OneNAND */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002207 region_end = region->offset + region->erasesize * region->numblocks;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002208 }
2209
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002210 instr->state = MTD_ERASING;
2211
Mika Korhonen73885ae2009-10-23 07:50:42 +02002212 /* Loop through the blocks */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002213 while (len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09002214 cond_resched();
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002215
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002216 /* Check if we have a bad block, we do not erase bad blocks */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002217 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302218 printk(KERN_WARNING "%s: attempt to erase a bad block "
2219 "at addr 0x%012llx\n",
2220 __func__, (unsigned long long) addr);
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002221 instr->state = MTD_ERASE_FAILED;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002222 return -EIO;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002223 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002224
2225 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2226
Adrian Hunter480b9df2007-02-07 13:55:19 +02002227 onenand_invalidate_bufferram(mtd, addr, block_size);
2228
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002229 ret = this->wait(mtd, FL_ERASING);
2230 /* Check, if it is write protected */
2231 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302232 printk(KERN_ERR "%s: Failed erase, block %d\n",
2233 __func__, onenand_block(this, addr));
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002234 instr->state = MTD_ERASE_FAILED;
2235 instr->fail_addr = addr;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002236 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002237 }
2238
2239 len -= block_size;
2240 addr += block_size;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002241
2242 if (addr == region_end) {
2243 if (!len)
2244 break;
2245 region++;
2246
2247 block_size = region->erasesize;
2248 region_end = region->offset + region->erasesize * region->numblocks;
2249
2250 if (len & (block_size - 1)) {
2251 /* FIXME: This should be handled at MTD partitioning level. */
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302252 printk(KERN_ERR "%s: Unaligned address\n",
2253 __func__);
Mika Korhonen73885ae2009-10-23 07:50:42 +02002254 return -EIO;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002255 }
2256 }
Mika Korhonen73885ae2009-10-23 07:50:42 +02002257 }
2258 return 0;
2259}
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002260
Mika Korhonen73885ae2009-10-23 07:50:42 +02002261/**
2262 * onenand_erase - [MTD Interface] erase block(s)
2263 * @param mtd MTD device structure
2264 * @param instr erase instruction
2265 *
2266 * Erase one or more blocks
2267 */
2268static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2269{
2270 struct onenand_chip *this = mtd->priv;
2271 unsigned int block_size;
2272 loff_t addr = instr->addr;
2273 loff_t len = instr->len;
2274 int ret = 0;
2275 struct mtd_erase_region_info *region = NULL;
2276 loff_t region_offset = 0;
2277
2278 DEBUG(MTD_DEBUG_LEVEL3, "%s: start=0x%012llx, len=%llu\n", __func__,
2279 (unsigned long long) instr->addr, (unsigned long long) instr->len);
2280
2281 /* Do not allow erase past end of device */
2282 if (unlikely((len + addr) > mtd->size)) {
2283 printk(KERN_ERR "%s: Erase past end of device\n", __func__);
2284 return -EINVAL;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002285 }
2286
Mika Korhonen73885ae2009-10-23 07:50:42 +02002287 if (FLEXONENAND(this)) {
2288 /* Find the eraseregion of this address */
2289 int i = flexonenand_region(mtd, addr);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002290
Mika Korhonen73885ae2009-10-23 07:50:42 +02002291 region = &mtd->eraseregions[i];
2292 block_size = region->erasesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002293
Mika Korhonen73885ae2009-10-23 07:50:42 +02002294 /* Start address within region must align on block boundary.
2295 * Erase region's start offset is always block start address.
2296 */
2297 region_offset = region->offset;
2298 } else
2299 block_size = 1 << this->erase_shift;
2300
2301 /* Start address must align on block boundary */
2302 if (unlikely((addr - region_offset) & (block_size - 1))) {
2303 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2304 return -EINVAL;
2305 }
2306
2307 /* Length must align on block boundary */
2308 if (unlikely(len & (block_size - 1))) {
2309 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2310 return -EINVAL;
2311 }
2312
2313 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2314
2315 /* Grab the lock and see if the device is available */
2316 onenand_get_device(mtd, FL_ERASING);
2317
2318 ret = onenand_block_by_block_erase(mtd, instr, region, block_size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002319
2320 /* Deselect and wake up anyone waiting on the device */
2321 onenand_release_device(mtd);
2322
Adrian Hunter3cd3a862007-10-12 10:34:01 +03002323 /* Do call back function */
Mika Korhonen73885ae2009-10-23 07:50:42 +02002324 if (!ret) {
2325 instr->state = MTD_ERASE_DONE;
Adrian Hunter3cd3a862007-10-12 10:34:01 +03002326 mtd_erase_callback(instr);
Mika Korhonen73885ae2009-10-23 07:50:42 +02002327 }
Adrian Hunter3cd3a862007-10-12 10:34:01 +03002328
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002329 return ret;
2330}
2331
2332/**
2333 * onenand_sync - [MTD Interface] sync
2334 * @param mtd MTD device structure
2335 *
2336 * Sync is actually a wait for chip ready function
2337 */
2338static void onenand_sync(struct mtd_info *mtd)
2339{
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302340 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002341
2342 /* Grab the lock and see if the device is available */
2343 onenand_get_device(mtd, FL_SYNCING);
2344
2345 /* Release it and go back */
2346 onenand_release_device(mtd);
2347}
2348
2349/**
2350 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2351 * @param mtd MTD device structure
2352 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002353 *
2354 * Check whether the block is bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002355 */
2356static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2357{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002358 int ret;
2359
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002360 /* Check for invalid offset */
2361 if (ofs > mtd->size)
2362 return -EINVAL;
2363
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002364 onenand_get_device(mtd, FL_READING);
2365 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2366 onenand_release_device(mtd);
2367 return ret;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002368}
2369
2370/**
2371 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2372 * @param mtd MTD device structure
2373 * @param ofs offset from device start
2374 *
2375 * This is the default implementation, which can be overridden by
2376 * a hardware specific driver.
2377 */
2378static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2379{
2380 struct onenand_chip *this = mtd->priv;
2381 struct bbm_info *bbm = this->bbm;
2382 u_char buf[2] = {0, 0};
Kyungmin Park12f77c92007-08-30 09:36:05 +09002383 struct mtd_oob_ops ops = {
2384 .mode = MTD_OOB_PLACE,
2385 .ooblen = 2,
2386 .oobbuf = buf,
2387 .ooboffs = 0,
2388 };
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002389 int block;
2390
2391 /* Get block number */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002392 block = onenand_block(this, ofs);
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002393 if (bbm->bbt)
2394 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2395
Mika Korhonen492e1502009-06-09 21:52:35 +03002396 /* We write two bytes, so we don't have to mess with 16-bit access */
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002397 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002398 /* FIXME : What to do when marking SLC block in partition
2399 * with MLC erasesize? For now, it is not advisable to
2400 * create partitions containing both SLC and MLC regions.
2401 */
2402 return onenand_write_oob_nolock(mtd, ofs, &ops);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002403}
2404
2405/**
2406 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2407 * @param mtd MTD device structure
2408 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002409 *
2410 * Mark the block as bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002411 */
2412static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2413{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002414 struct onenand_chip *this = mtd->priv;
2415 int ret;
2416
2417 ret = onenand_block_isbad(mtd, ofs);
2418 if (ret) {
2419 /* If it was bad already, return success and do nothing */
2420 if (ret > 0)
2421 return 0;
2422 return ret;
2423 }
2424
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002425 onenand_get_device(mtd, FL_WRITING);
2426 ret = this->block_markbad(mtd, ofs);
2427 onenand_release_device(mtd);
2428 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002429}
2430
2431/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09002432 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002433 * @param mtd MTD device structure
2434 * @param ofs offset relative to mtd start
Kyungmin Park08f782b2006-11-16 11:29:39 +09002435 * @param len number of bytes to lock or unlock
Kyungmin Parke3da8062007-02-15 09:36:39 +09002436 * @param cmd lock or unlock command
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002437 *
Kyungmin Park08f782b2006-11-16 11:29:39 +09002438 * Lock or unlock one or more blocks
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002439 */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002440static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002441{
2442 struct onenand_chip *this = mtd->priv;
2443 int start, end, block, value, status;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002444 int wp_status_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002445
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002446 start = onenand_block(this, ofs);
2447 end = onenand_block(this, ofs + len) - 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002448
Kyungmin Park08f782b2006-11-16 11:29:39 +09002449 if (cmd == ONENAND_CMD_LOCK)
2450 wp_status_mask = ONENAND_WP_LS;
2451 else
2452 wp_status_mask = ONENAND_WP_US;
2453
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002454 /* Continuous lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002455 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002456 /* Set start block address */
2457 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2458 /* Set end block address */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002459 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002460 /* Write lock command */
2461 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002462
2463 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002464 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002465
2466 /* Sanity check */
2467 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2468 & ONENAND_CTRL_ONGO)
2469 continue;
2470
2471 /* Check lock status */
2472 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002473 if (!(status & wp_status_mask))
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302474 printk(KERN_ERR "%s: wp status = 0x%x\n",
2475 __func__, status);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002476
2477 return 0;
2478 }
2479
2480 /* Block lock scheme */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002481 for (block = start; block < end + 1; block++) {
Kyungmin Park20ba89a2005-12-16 11:17:29 +09002482 /* Set block address */
2483 value = onenand_block_address(this, block);
2484 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2485 /* Select DataRAM for DDP */
2486 value = onenand_bufferram_address(this, block);
2487 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002488 /* Set start block address */
2489 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002490 /* Write lock command */
2491 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002492
2493 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002494 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002495
2496 /* Sanity check */
2497 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2498 & ONENAND_CTRL_ONGO)
2499 continue;
2500
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002501 /* Check lock status */
2502 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002503 if (!(status & wp_status_mask))
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302504 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2505 __func__, block, status);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002506 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002507
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002508 return 0;
2509}
2510
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002511/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09002512 * onenand_lock - [MTD Interface] Lock block(s)
2513 * @param mtd MTD device structure
2514 * @param ofs offset relative to mtd start
2515 * @param len number of bytes to unlock
2516 *
2517 * Lock one or more blocks
2518 */
Adrian Hunter69423d92008-12-10 13:37:21 +00002519static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Park08f782b2006-11-16 11:29:39 +09002520{
Adrian Hunter34627f02007-10-12 10:19:26 +03002521 int ret;
2522
2523 onenand_get_device(mtd, FL_LOCKING);
2524 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2525 onenand_release_device(mtd);
2526 return ret;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002527}
2528
Kyungmin Park08f782b2006-11-16 11:29:39 +09002529/**
2530 * onenand_unlock - [MTD Interface] Unlock block(s)
2531 * @param mtd MTD device structure
2532 * @param ofs offset relative to mtd start
2533 * @param len number of bytes to unlock
2534 *
2535 * Unlock one or more blocks
2536 */
Adrian Hunter69423d92008-12-10 13:37:21 +00002537static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Park08f782b2006-11-16 11:29:39 +09002538{
Adrian Hunter34627f02007-10-12 10:19:26 +03002539 int ret;
2540
2541 onenand_get_device(mtd, FL_LOCKING);
2542 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2543 onenand_release_device(mtd);
2544 return ret;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002545}
2546
2547/**
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002548 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2549 * @param this onenand chip data structure
2550 *
2551 * Check lock status
2552 */
Kyungmin Park66a10502008-02-13 15:55:38 +09002553static int onenand_check_lock_status(struct onenand_chip *this)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002554{
2555 unsigned int value, block, status;
2556 unsigned int end;
2557
2558 end = this->chipsize >> this->erase_shift;
2559 for (block = 0; block < end; block++) {
2560 /* Set block address */
2561 value = onenand_block_address(this, block);
2562 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2563 /* Select DataRAM for DDP */
2564 value = onenand_bufferram_address(this, block);
2565 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2566 /* Set start block address */
2567 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2568
2569 /* Check lock status */
2570 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park66a10502008-02-13 15:55:38 +09002571 if (!(status & ONENAND_WP_US)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302572 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2573 __func__, block, status);
Kyungmin Park66a10502008-02-13 15:55:38 +09002574 return 0;
2575 }
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002576 }
Kyungmin Park66a10502008-02-13 15:55:38 +09002577
2578 return 1;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002579}
2580
2581/**
2582 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2583 * @param mtd MTD device structure
2584 *
2585 * Unlock all blocks
2586 */
Kyungmin Park66a10502008-02-13 15:55:38 +09002587static void onenand_unlock_all(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002588{
2589 struct onenand_chip *this = mtd->priv;
Kyungmin Park66a10502008-02-13 15:55:38 +09002590 loff_t ofs = 0;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002591 loff_t len = mtd->size;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002592
2593 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09002594 /* Set start block address */
2595 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002596 /* Write unlock command */
2597 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2598
2599 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002600 this->wait(mtd, FL_LOCKING);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002601
2602 /* Sanity check */
2603 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2604 & ONENAND_CTRL_ONGO)
2605 continue;
2606
Kyungmin Park31bb9992009-05-12 13:46:57 -07002607 /* Don't check lock status */
2608 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2609 return;
2610
Kyungmin Park66a10502008-02-13 15:55:38 +09002611 /* Check lock status */
2612 if (onenand_check_lock_status(this))
2613 return;
2614
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002615 /* Workaround for all block unlock in DDP */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002616 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Park66a10502008-02-13 15:55:38 +09002617 /* All blocks on another chip */
2618 ofs = this->chipsize >> 1;
2619 len = this->chipsize >> 1;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002620 }
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002621 }
2622
Kyungmin Park66a10502008-02-13 15:55:38 +09002623 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002624}
2625
Kyungmin Park493c6462006-05-12 17:03:07 +03002626#ifdef CONFIG_MTD_ONENAND_OTP
2627
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302628/**
2629 * onenand_otp_command - Send OTP specific command to OneNAND device
2630 * @param mtd MTD device structure
2631 * @param cmd the command to be sent
2632 * @param addr offset to read from or write to
2633 * @param len number of bytes to read or write
2634 */
2635static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2636 size_t len)
2637{
2638 struct onenand_chip *this = mtd->priv;
2639 int value, block, page;
2640
2641 /* Address translation */
2642 switch (cmd) {
2643 case ONENAND_CMD_OTP_ACCESS:
2644 block = (int) (addr >> this->erase_shift);
2645 page = -1;
2646 break;
2647
2648 default:
2649 block = (int) (addr >> this->erase_shift);
2650 page = (int) (addr >> this->page_shift);
2651
2652 if (ONENAND_IS_2PLANE(this)) {
2653 /* Make the even block number */
2654 block &= ~1;
2655 /* Is it the odd plane? */
2656 if (addr & this->writesize)
2657 block++;
2658 page >>= 1;
2659 }
2660 page &= this->page_mask;
2661 break;
2662 }
2663
2664 if (block != -1) {
2665 /* Write 'DFS, FBA' of Flash */
2666 value = onenand_block_address(this, block);
2667 this->write_word(value, this->base +
2668 ONENAND_REG_START_ADDRESS1);
2669 }
2670
2671 if (page != -1) {
2672 /* Now we use page size operation */
2673 int sectors = 4, count = 4;
2674 int dataram;
2675
2676 switch (cmd) {
2677 default:
2678 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2679 cmd = ONENAND_CMD_2X_PROG;
2680 dataram = ONENAND_CURRENT_BUFFERRAM(this);
2681 break;
2682 }
2683
2684 /* Write 'FPA, FSA' of Flash */
2685 value = onenand_page_address(page, sectors);
2686 this->write_word(value, this->base +
2687 ONENAND_REG_START_ADDRESS8);
2688
2689 /* Write 'BSA, BSC' of DataRAM */
2690 value = onenand_buffer_address(dataram, sectors, count);
2691 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2692 }
2693
2694 /* Interrupt clear */
2695 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2696
2697 /* Write command */
2698 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2699
2700 return 0;
2701}
2702
2703/**
2704 * onenand_otp_write_oob_nolock - [Internal] OneNAND write out-of-band, specific to OTP
2705 * @param mtd MTD device structure
2706 * @param to offset to write to
2707 * @param len number of bytes to write
2708 * @param retlen pointer to variable to store the number of written bytes
2709 * @param buf the data to write
2710 *
2711 * OneNAND write out-of-band only for OTP
2712 */
2713static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2714 struct mtd_oob_ops *ops)
2715{
2716 struct onenand_chip *this = mtd->priv;
2717 int column, ret = 0, oobsize;
2718 int written = 0;
2719 u_char *oobbuf;
2720 size_t len = ops->ooblen;
2721 const u_char *buf = ops->oobbuf;
2722 int block, value, status;
2723
2724 to += ops->ooboffs;
2725
2726 /* Initialize retlen, in case of early exit */
2727 ops->oobretlen = 0;
2728
2729 oobsize = mtd->oobsize;
2730
2731 column = to & (mtd->oobsize - 1);
2732
2733 oobbuf = this->oob_buf;
2734
2735 /* Loop until all data write */
2736 while (written < len) {
2737 int thislen = min_t(int, oobsize, len - written);
2738
2739 cond_resched();
2740
2741 block = (int) (to >> this->erase_shift);
2742 /*
2743 * Write 'DFS, FBA' of Flash
2744 * Add: F100h DQ=DFS, FBA
2745 */
2746
2747 value = onenand_block_address(this, block);
2748 this->write_word(value, this->base +
2749 ONENAND_REG_START_ADDRESS1);
2750
2751 /*
2752 * Select DataRAM for DDP
2753 * Add: F101h DQ=DBS
2754 */
2755
2756 value = onenand_bufferram_address(this, block);
2757 this->write_word(value, this->base +
2758 ONENAND_REG_START_ADDRESS2);
2759 ONENAND_SET_NEXT_BUFFERRAM(this);
2760
2761 /*
2762 * Enter OTP access mode
2763 */
2764 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2765 this->wait(mtd, FL_OTPING);
2766
2767 /* We send data to spare ram with oobsize
2768 * to prevent byte access */
2769 memcpy(oobbuf + column, buf, thislen);
2770
2771 /*
2772 * Write Data into DataRAM
2773 * Add: 8th Word
2774 * in sector0/spare/page0
2775 * DQ=XXFCh
2776 */
2777 this->write_bufferram(mtd, ONENAND_SPARERAM,
2778 oobbuf, 0, mtd->oobsize);
2779
2780 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2781 onenand_update_bufferram(mtd, to, 0);
2782 if (ONENAND_IS_2PLANE(this)) {
2783 ONENAND_SET_BUFFERRAM1(this);
2784 onenand_update_bufferram(mtd, to + this->writesize, 0);
2785 }
2786
2787 ret = this->wait(mtd, FL_WRITING);
2788 if (ret) {
2789 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2790 break;
2791 }
2792
2793 /* Exit OTP access mode */
2794 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2795 this->wait(mtd, FL_RESETING);
2796
2797 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2798 status &= 0x60;
2799
2800 if (status == 0x60) {
2801 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2802 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2803 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2804 } else if (status == 0x20) {
2805 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2806 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2807 printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
2808 } else if (status == 0x40) {
2809 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2810 printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
2811 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2812 } else {
2813 printk(KERN_DEBUG "Reboot to check\n");
2814 }
2815
2816 written += thislen;
2817 if (written == len)
2818 break;
2819
2820 to += mtd->writesize;
2821 buf += thislen;
2822 column = 0;
2823 }
2824
2825 ops->oobretlen = written;
2826
2827 return ret;
2828}
2829
Mika Korhonen492e1502009-06-09 21:52:35 +03002830/* Internal OTP operation */
Kyungmin Park493c6462006-05-12 17:03:07 +03002831typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2832 size_t *retlen, u_char *buf);
2833
2834/**
2835 * do_otp_read - [DEFAULT] Read OTP block area
2836 * @param mtd MTD device structure
2837 * @param from The offset to read
2838 * @param len number of bytes to read
2839 * @param retlen pointer to variable to store the number of readbytes
2840 * @param buf the databuffer to put/get data
2841 *
2842 * Read OTP block area.
2843 */
2844static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2845 size_t *retlen, u_char *buf)
2846{
2847 struct onenand_chip *this = mtd->priv;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002848 struct mtd_oob_ops ops = {
2849 .len = len,
2850 .ooblen = 0,
2851 .datbuf = buf,
2852 .oobbuf = NULL,
2853 };
Kyungmin Park493c6462006-05-12 17:03:07 +03002854 int ret;
2855
2856 /* Enter OTP access mode */
2857 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2858 this->wait(mtd, FL_OTPING);
2859
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002860 ret = ONENAND_IS_MLC(this) ?
2861 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2862 onenand_read_ops_nolock(mtd, from, &ops);
Kyungmin Park493c6462006-05-12 17:03:07 +03002863
2864 /* Exit OTP access mode */
2865 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2866 this->wait(mtd, FL_RESETING);
2867
2868 return ret;
2869}
2870
2871/**
2872 * do_otp_write - [DEFAULT] Write OTP block area
2873 * @param mtd MTD device structure
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002874 * @param to The offset to write
Kyungmin Park493c6462006-05-12 17:03:07 +03002875 * @param len number of bytes to write
2876 * @param retlen pointer to variable to store the number of write bytes
2877 * @param buf the databuffer to put/get data
2878 *
2879 * Write OTP block area.
2880 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002881static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
Kyungmin Park493c6462006-05-12 17:03:07 +03002882 size_t *retlen, u_char *buf)
2883{
2884 struct onenand_chip *this = mtd->priv;
2885 unsigned char *pbuf = buf;
2886 int ret;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002887 struct mtd_oob_ops ops;
Kyungmin Park493c6462006-05-12 17:03:07 +03002888
2889 /* Force buffer page aligned */
Joern Engel28318772006-05-22 23:18:05 +02002890 if (len < mtd->writesize) {
Kyungmin Park493c6462006-05-12 17:03:07 +03002891 memcpy(this->page_buf, buf, len);
Joern Engel28318772006-05-22 23:18:05 +02002892 memset(this->page_buf + len, 0xff, mtd->writesize - len);
Kyungmin Park493c6462006-05-12 17:03:07 +03002893 pbuf = this->page_buf;
Joern Engel28318772006-05-22 23:18:05 +02002894 len = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03002895 }
2896
2897 /* Enter OTP access mode */
2898 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2899 this->wait(mtd, FL_OTPING);
2900
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002901 ops.len = len;
2902 ops.ooblen = 0;
Kyungmin Park14370852007-10-10 13:48:14 +09002903 ops.datbuf = pbuf;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002904 ops.oobbuf = NULL;
2905 ret = onenand_write_ops_nolock(mtd, to, &ops);
2906 *retlen = ops.retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03002907
2908 /* Exit OTP access mode */
2909 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2910 this->wait(mtd, FL_RESETING);
2911
2912 return ret;
2913}
2914
2915/**
2916 * do_otp_lock - [DEFAULT] Lock OTP block area
2917 * @param mtd MTD device structure
2918 * @param from The offset to lock
2919 * @param len number of bytes to lock
2920 * @param retlen pointer to variable to store the number of lock bytes
2921 * @param buf the databuffer to put/get data
2922 *
2923 * Lock OTP block area.
2924 */
2925static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2926 size_t *retlen, u_char *buf)
2927{
2928 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002929 struct mtd_oob_ops ops;
Kyungmin Park493c6462006-05-12 17:03:07 +03002930 int ret;
2931
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002932 if (FLEXONENAND(this)) {
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302933
2934 /* Enter OTP access mode */
2935 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2936 this->wait(mtd, FL_OTPING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002937 /*
2938 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2939 * main area of page 49.
2940 */
2941 ops.len = mtd->writesize;
2942 ops.ooblen = 0;
2943 ops.datbuf = buf;
2944 ops.oobbuf = NULL;
2945 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
2946 *retlen = ops.retlen;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302947
2948 /* Exit OTP access mode */
2949 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2950 this->wait(mtd, FL_RESETING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002951 } else {
2952 ops.mode = MTD_OOB_PLACE;
2953 ops.ooblen = len;
2954 ops.oobbuf = buf;
2955 ops.ooboffs = 0;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302956 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002957 *retlen = ops.oobretlen;
2958 }
Kyungmin Park493c6462006-05-12 17:03:07 +03002959
Kyungmin Park493c6462006-05-12 17:03:07 +03002960 return ret;
2961}
2962
2963/**
2964 * onenand_otp_walk - [DEFAULT] Handle OTP operation
2965 * @param mtd MTD device structure
2966 * @param from The offset to read/write
2967 * @param len number of bytes to read/write
2968 * @param retlen pointer to variable to store the number of read bytes
2969 * @param buf the databuffer to put/get data
2970 * @param action do given action
2971 * @param mode specify user and factory
2972 *
2973 * Handle OTP operation.
2974 */
2975static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2976 size_t *retlen, u_char *buf,
2977 otp_op_t action, int mode)
2978{
2979 struct onenand_chip *this = mtd->priv;
2980 int otp_pages;
2981 int density;
2982 int ret = 0;
2983
2984 *retlen = 0;
2985
Kyungmin Parke71f04f2007-12-11 11:23:45 +09002986 density = onenand_get_density(this->device_id);
Kyungmin Park493c6462006-05-12 17:03:07 +03002987 if (density < ONENAND_DEVICE_DENSITY_512Mb)
2988 otp_pages = 20;
2989 else
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302990 otp_pages = 50;
Kyungmin Park493c6462006-05-12 17:03:07 +03002991
2992 if (mode == MTD_OTP_FACTORY) {
Joern Engel28318772006-05-22 23:18:05 +02002993 from += mtd->writesize * otp_pages;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302994 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
Kyungmin Park493c6462006-05-12 17:03:07 +03002995 }
2996
2997 /* Check User/Factory boundary */
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302998 if (mode == MTD_OTP_USER) {
2999 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
3000 return 0;
3001 } else {
3002 if (((mtd->writesize * otp_pages) - len) < 0)
3003 return 0;
3004 }
Kyungmin Park493c6462006-05-12 17:03:07 +03003005
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003006 onenand_get_device(mtd, FL_OTPING);
Kyungmin Park493c6462006-05-12 17:03:07 +03003007 while (len > 0 && otp_pages > 0) {
3008 if (!action) { /* OTP Info functions */
3009 struct otp_info *otpinfo;
3010
3011 len -= sizeof(struct otp_info);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003012 if (len <= 0) {
3013 ret = -ENOSPC;
3014 break;
3015 }
Kyungmin Park493c6462006-05-12 17:03:07 +03003016
3017 otpinfo = (struct otp_info *) buf;
3018 otpinfo->start = from;
Joern Engel28318772006-05-22 23:18:05 +02003019 otpinfo->length = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03003020 otpinfo->locked = 0;
3021
Joern Engel28318772006-05-22 23:18:05 +02003022 from += mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03003023 buf += sizeof(struct otp_info);
3024 *retlen += sizeof(struct otp_info);
3025 } else {
3026 size_t tmp_retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03003027
3028 ret = action(mtd, from, len, &tmp_retlen, buf);
3029
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303030 buf += tmp_retlen;
3031 len -= tmp_retlen;
3032 *retlen += tmp_retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03003033
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003034 if (ret)
3035 break;
Kyungmin Park493c6462006-05-12 17:03:07 +03003036 }
3037 otp_pages--;
3038 }
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003039 onenand_release_device(mtd);
Kyungmin Park493c6462006-05-12 17:03:07 +03003040
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003041 return ret;
Kyungmin Park493c6462006-05-12 17:03:07 +03003042}
3043
3044/**
3045 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3046 * @param mtd MTD device structure
3047 * @param buf the databuffer to put/get data
3048 * @param len number of bytes to read
3049 *
3050 * Read factory OTP info.
3051 */
3052static int onenand_get_fact_prot_info(struct mtd_info *mtd,
3053 struct otp_info *buf, size_t len)
3054{
3055 size_t retlen;
3056 int ret;
3057
3058 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
3059
3060 return ret ? : retlen;
3061}
3062
3063/**
3064 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3065 * @param mtd MTD device structure
3066 * @param from The offset to read
3067 * @param len number of bytes to read
3068 * @param retlen pointer to variable to store the number of read bytes
3069 * @param buf the databuffer to put/get data
3070 *
3071 * Read factory OTP area.
3072 */
3073static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3074 size_t len, size_t *retlen, u_char *buf)
3075{
3076 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3077}
3078
3079/**
3080 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3081 * @param mtd MTD device structure
3082 * @param buf the databuffer to put/get data
3083 * @param len number of bytes to read
3084 *
3085 * Read user OTP info.
3086 */
3087static int onenand_get_user_prot_info(struct mtd_info *mtd,
3088 struct otp_info *buf, size_t len)
3089{
3090 size_t retlen;
3091 int ret;
3092
3093 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
3094
3095 return ret ? : retlen;
3096}
3097
3098/**
3099 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3100 * @param mtd MTD device structure
3101 * @param from The offset to read
3102 * @param len number of bytes to read
3103 * @param retlen pointer to variable to store the number of read bytes
3104 * @param buf the databuffer to put/get data
3105 *
3106 * Read user OTP area.
3107 */
3108static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3109 size_t len, size_t *retlen, u_char *buf)
3110{
3111 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3112}
3113
3114/**
3115 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3116 * @param mtd MTD device structure
3117 * @param from The offset to write
3118 * @param len number of bytes to write
3119 * @param retlen pointer to variable to store the number of write bytes
3120 * @param buf the databuffer to put/get data
3121 *
3122 * Write user OTP area.
3123 */
3124static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3125 size_t len, size_t *retlen, u_char *buf)
3126{
3127 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3128}
3129
3130/**
3131 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3132 * @param mtd MTD device structure
3133 * @param from The offset to lock
3134 * @param len number of bytes to unlock
3135 *
3136 * Write lock mark on spare area in page 0 in OTP block
3137 */
3138static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3139 size_t len)
3140{
Kyungmin Park69d79182007-12-14 14:47:21 +09003141 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003142 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
Kyungmin Park493c6462006-05-12 17:03:07 +03003143 size_t retlen;
3144 int ret;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303145 unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
Kyungmin Park493c6462006-05-12 17:03:07 +03003146
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003147 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3148 : mtd->oobsize);
Kyungmin Park493c6462006-05-12 17:03:07 +03003149 /*
Kyungmin Park493c6462006-05-12 17:03:07 +03003150 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3151 * We write 16 bytes spare area instead of 2 bytes.
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003152 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3153 * main area of page 49.
Kyungmin Park493c6462006-05-12 17:03:07 +03003154 */
Kyungmin Park493c6462006-05-12 17:03:07 +03003155
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003156 from = 0;
3157 len = FLEXONENAND(this) ? mtd->writesize : 16;
3158
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303159 /*
3160 * Note: OTP lock operation
3161 * OTP block : 0xXXFC XX 1111 1100
3162 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3163 * Both : 0xXXF0 (If chip support) XX 1111 0000
3164 */
3165 if (FLEXONENAND(this))
3166 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3167
3168 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3169 if (otp == 1)
3170 buf[otp_lock_offset] = 0xFC;
3171 else if (otp == 2)
3172 buf[otp_lock_offset] = 0xF3;
3173 else if (otp == 3)
3174 buf[otp_lock_offset] = 0xF0;
3175 else if (otp != 0)
3176 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3177
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003178 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
Kyungmin Park493c6462006-05-12 17:03:07 +03003179
3180 return ret ? : retlen;
3181}
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303182
Kyungmin Park493c6462006-05-12 17:03:07 +03003183#endif /* CONFIG_MTD_ONENAND_OTP */
3184
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003185/**
Kyungmin Park75384b02007-01-18 11:10:57 +09003186 * onenand_check_features - Check and set OneNAND features
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003187 * @param mtd MTD data structure
3188 *
Kyungmin Park75384b02007-01-18 11:10:57 +09003189 * Check and set OneNAND features
3190 * - lock scheme
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003191 * - two plane
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003192 */
Kyungmin Park75384b02007-01-18 11:10:57 +09003193static void onenand_check_features(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003194{
3195 struct onenand_chip *this = mtd->priv;
3196 unsigned int density, process;
3197
3198 /* Lock scheme depends on density and process */
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003199 density = onenand_get_density(this->device_id);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003200 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
3201
3202 /* Lock scheme */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003203 switch (density) {
3204 case ONENAND_DEVICE_DENSITY_4Gb:
3205 this->options |= ONENAND_HAS_2PLANE;
3206
3207 case ONENAND_DEVICE_DENSITY_2Gb:
Mika Korhonen492e1502009-06-09 21:52:35 +03003208 /* 2Gb DDP does not have 2 plane */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003209 if (!ONENAND_IS_DDP(this))
3210 this->options |= ONENAND_HAS_2PLANE;
3211 this->options |= ONENAND_HAS_UNLOCK_ALL;
3212
3213 case ONENAND_DEVICE_DENSITY_1Gb:
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003214 /* A-Die has all block unlock */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003215 if (process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003216 this->options |= ONENAND_HAS_UNLOCK_ALL;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003217 break;
3218
3219 default:
3220 /* Some OneNAND has continuous lock scheme */
3221 if (!process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003222 this->options |= ONENAND_HAS_CONT_LOCK;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003223 break;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003224 }
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003225
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003226 if (ONENAND_IS_MLC(this))
3227 this->options &= ~ONENAND_HAS_2PLANE;
3228
3229 if (FLEXONENAND(this)) {
3230 this->options &= ~ONENAND_HAS_CONT_LOCK;
3231 this->options |= ONENAND_HAS_UNLOCK_ALL;
3232 }
3233
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003234 if (this->options & ONENAND_HAS_CONT_LOCK)
3235 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3236 if (this->options & ONENAND_HAS_UNLOCK_ALL)
3237 printk(KERN_DEBUG "Chip support all block unlock\n");
3238 if (this->options & ONENAND_HAS_2PLANE)
3239 printk(KERN_DEBUG "Chip has 2 plane\n");
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003240}
3241
3242/**
Kyungmin Parke3da8062007-02-15 09:36:39 +09003243 * onenand_print_device_info - Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003244 * @param device device ID
Kyungmin Parke3da8062007-02-15 09:36:39 +09003245 * @param version version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003246 *
Kyungmin Parke3da8062007-02-15 09:36:39 +09003247 * Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003248 */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003249static void onenand_print_device_info(int device, int version)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003250{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003251 int vcc, demuxed, ddp, density, flexonenand;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003252
3253 vcc = device & ONENAND_DEVICE_VCC_MASK;
3254 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3255 ddp = device & ONENAND_DEVICE_IS_DDP;
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003256 density = onenand_get_density(device);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003257 flexonenand = device & DEVICE_IS_FLEXONENAND;
3258 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3259 demuxed ? "" : "Muxed ",
3260 flexonenand ? "Flex-" : "",
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003261 ddp ? "(DDP)" : "",
3262 (16 << density),
3263 vcc ? "2.65/3.3" : "1.8",
3264 device);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003265 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003266}
3267
3268static const struct onenand_manufacturers onenand_manuf_ids[] = {
3269 {ONENAND_MFR_SAMSUNG, "Samsung"},
Adrian Hunteree8f3762009-05-05 11:04:19 +03003270 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003271};
3272
3273/**
3274 * onenand_check_maf - Check manufacturer ID
3275 * @param manuf manufacturer ID
3276 *
3277 * Check manufacturer ID
3278 */
3279static int onenand_check_maf(int manuf)
3280{
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003281 int size = ARRAY_SIZE(onenand_manuf_ids);
3282 char *name;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003283 int i;
3284
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003285 for (i = 0; i < size; i++)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003286 if (manuf == onenand_manuf_ids[i].id)
3287 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003288
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003289 if (i < size)
3290 name = onenand_manuf_ids[i].name;
3291 else
3292 name = "Unknown";
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003293
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003294 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3295
3296 return (i == size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003297}
3298
3299/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003300* flexonenand_get_boundary - Reads the SLC boundary
3301* @param onenand_info - onenand info structure
3302**/
3303static int flexonenand_get_boundary(struct mtd_info *mtd)
3304{
3305 struct onenand_chip *this = mtd->priv;
3306 unsigned die, bdry;
3307 int ret, syscfg, locked;
3308
3309 /* Disable ECC */
3310 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3311 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3312
3313 for (die = 0; die < this->dies; die++) {
3314 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3315 this->wait(mtd, FL_SYNCING);
3316
3317 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3318 ret = this->wait(mtd, FL_READING);
3319
3320 bdry = this->read_word(this->base + ONENAND_DATARAM);
3321 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3322 locked = 0;
3323 else
3324 locked = 1;
3325 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3326
3327 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3328 ret = this->wait(mtd, FL_RESETING);
3329
3330 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3331 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3332 }
3333
3334 /* Enable ECC */
3335 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3336 return 0;
3337}
3338
3339/**
3340 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3341 * boundary[], diesize[], mtd->size, mtd->erasesize
3342 * @param mtd - MTD device structure
3343 */
3344static void flexonenand_get_size(struct mtd_info *mtd)
3345{
3346 struct onenand_chip *this = mtd->priv;
3347 int die, i, eraseshift, density;
3348 int blksperdie, maxbdry;
3349 loff_t ofs;
3350
3351 density = onenand_get_density(this->device_id);
3352 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3353 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3354 maxbdry = blksperdie - 1;
3355 eraseshift = this->erase_shift - 1;
3356
3357 mtd->numeraseregions = this->dies << 1;
3358
3359 /* This fills up the device boundary */
3360 flexonenand_get_boundary(mtd);
3361 die = ofs = 0;
3362 i = -1;
3363 for (; die < this->dies; die++) {
3364 if (!die || this->boundary[die-1] != maxbdry) {
3365 i++;
3366 mtd->eraseregions[i].offset = ofs;
3367 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3368 mtd->eraseregions[i].numblocks =
3369 this->boundary[die] + 1;
3370 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3371 eraseshift++;
3372 } else {
3373 mtd->numeraseregions -= 1;
3374 mtd->eraseregions[i].numblocks +=
3375 this->boundary[die] + 1;
3376 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3377 }
3378 if (this->boundary[die] != maxbdry) {
3379 i++;
3380 mtd->eraseregions[i].offset = ofs;
3381 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3382 mtd->eraseregions[i].numblocks = maxbdry ^
3383 this->boundary[die];
3384 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3385 eraseshift--;
3386 } else
3387 mtd->numeraseregions -= 1;
3388 }
3389
3390 /* Expose MLC erase size except when all blocks are SLC */
3391 mtd->erasesize = 1 << this->erase_shift;
3392 if (mtd->numeraseregions == 1)
3393 mtd->erasesize >>= 1;
3394
3395 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3396 for (i = 0; i < mtd->numeraseregions; i++)
3397 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3398 " numblocks: %04u]\n",
3399 (unsigned int) mtd->eraseregions[i].offset,
3400 mtd->eraseregions[i].erasesize,
3401 mtd->eraseregions[i].numblocks);
3402
3403 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3404 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3405 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3406 << (this->erase_shift - 1);
3407 mtd->size += this->diesize[die];
3408 }
3409}
3410
3411/**
3412 * flexonenand_check_blocks_erased - Check if blocks are erased
3413 * @param mtd_info - mtd info structure
3414 * @param start - first erase block to check
3415 * @param end - last erase block to check
3416 *
3417 * Converting an unerased block from MLC to SLC
3418 * causes byte values to change. Since both data and its ECC
3419 * have changed, reads on the block give uncorrectable error.
3420 * This might lead to the block being detected as bad.
3421 *
3422 * Avoid this by ensuring that the block to be converted is
3423 * erased.
3424 */
3425static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3426{
3427 struct onenand_chip *this = mtd->priv;
3428 int i, ret;
3429 int block;
3430 struct mtd_oob_ops ops = {
3431 .mode = MTD_OOB_PLACE,
3432 .ooboffs = 0,
3433 .ooblen = mtd->oobsize,
3434 .datbuf = NULL,
3435 .oobbuf = this->oob_buf,
3436 };
3437 loff_t addr;
3438
3439 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3440
3441 for (block = start; block <= end; block++) {
3442 addr = flexonenand_addr(this, block);
3443 if (onenand_block_isbad_nolock(mtd, addr, 0))
3444 continue;
3445
3446 /*
3447 * Since main area write results in ECC write to spare,
3448 * it is sufficient to check only ECC bytes for change.
3449 */
3450 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3451 if (ret)
3452 return ret;
3453
3454 for (i = 0; i < mtd->oobsize; i++)
3455 if (this->oob_buf[i] != 0xff)
3456 break;
3457
3458 if (i != mtd->oobsize) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303459 printk(KERN_WARNING "%s: Block %d not erased.\n",
3460 __func__, block);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003461 return 1;
3462 }
3463 }
3464
3465 return 0;
3466}
3467
3468/**
3469 * flexonenand_set_boundary - Writes the SLC boundary
3470 * @param mtd - mtd info structure
3471 */
3472int flexonenand_set_boundary(struct mtd_info *mtd, int die,
3473 int boundary, int lock)
3474{
3475 struct onenand_chip *this = mtd->priv;
3476 int ret, density, blksperdie, old, new, thisboundary;
3477 loff_t addr;
3478
3479 /* Change only once for SDP Flex-OneNAND */
3480 if (die && (!ONENAND_IS_DDP(this)))
3481 return 0;
3482
3483 /* boundary value of -1 indicates no required change */
3484 if (boundary < 0 || boundary == this->boundary[die])
3485 return 0;
3486
3487 density = onenand_get_density(this->device_id);
3488 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3489 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3490
3491 if (boundary >= blksperdie) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303492 printk(KERN_ERR "%s: Invalid boundary value. "
3493 "Boundary not changed.\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003494 return -EINVAL;
3495 }
3496
3497 /* Check if converting blocks are erased */
3498 old = this->boundary[die] + (die * this->density_mask);
3499 new = boundary + (die * this->density_mask);
3500 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3501 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303502 printk(KERN_ERR "%s: Please erase blocks "
3503 "before boundary change\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003504 return ret;
3505 }
3506
3507 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3508 this->wait(mtd, FL_SYNCING);
3509
3510 /* Check is boundary is locked */
3511 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3512 ret = this->wait(mtd, FL_READING);
3513
3514 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3515 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303516 printk(KERN_ERR "%s: boundary locked\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003517 ret = 1;
3518 goto out;
3519 }
3520
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303521 printk(KERN_INFO "Changing die %d boundary: %d%s\n",
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003522 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3523
3524 addr = die ? this->diesize[0] : 0;
3525
3526 boundary &= FLEXONENAND_PI_MASK;
3527 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3528
3529 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3530 ret = this->wait(mtd, FL_ERASING);
3531 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303532 printk(KERN_ERR "%s: flexonenand_set_boundary: "
3533 "Failed PI erase for Die %d\n", __func__, die);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003534 goto out;
3535 }
3536
3537 this->write_word(boundary, this->base + ONENAND_DATARAM);
3538 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3539 ret = this->wait(mtd, FL_WRITING);
3540 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303541 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3542 __func__, die);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003543 goto out;
3544 }
3545
3546 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3547 ret = this->wait(mtd, FL_WRITING);
3548out:
3549 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3550 this->wait(mtd, FL_RESETING);
3551 if (!ret)
3552 /* Recalculate device size on boundary change*/
3553 flexonenand_get_size(mtd);
3554
3555 return ret;
3556}
3557
3558/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003559 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3560 * @param mtd MTD device structure
3561 *
3562 * OneNAND detection method:
Michael Opdenacker59c51592007-05-09 08:57:56 +02003563 * Compare the values from command with ones from register
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003564 */
3565static int onenand_probe(struct mtd_info *mtd)
3566{
3567 struct onenand_chip *this = mtd->priv;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003568 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003569 int density;
Kyungmin Park47e777e2006-09-25 23:53:28 +00003570 int syscfg;
3571
3572 /* Save system configuration 1 */
3573 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3574 /* Clear Sync. Burst Read mode to read BootRAM */
Adrian Hunteree8f3762009-05-05 11:04:19 +03003575 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003576
3577 /* Send the command for reading device ID from BootRAM */
3578 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3579
3580 /* Read manufacturer and device IDs from BootRAM */
3581 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3582 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3583
Kyungmin Park47e777e2006-09-25 23:53:28 +00003584 /* Reset OneNAND to read default register values */
3585 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3586 /* Wait reset */
3587 this->wait(mtd, FL_RESETING);
3588
3589 /* Restore system configuration 1 */
3590 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3591
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003592 /* Check manufacturer ID */
3593 if (onenand_check_maf(bram_maf_id))
3594 return -ENXIO;
3595
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003596 /* Read manufacturer and device IDs from Register */
3597 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3598 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09003599 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003600 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003601
3602 /* Check OneNAND device */
3603 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3604 return -ENXIO;
3605
3606 /* Flash device information */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003607 onenand_print_device_info(dev_id, ver_id);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003608 this->device_id = dev_id;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003609 this->version_id = ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003610
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003611 density = onenand_get_density(dev_id);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003612 if (FLEXONENAND(this)) {
3613 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3614 /* Maximum possible erase regions */
3615 mtd->numeraseregions = this->dies << 1;
3616 mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
3617 * (this->dies << 1), GFP_KERNEL);
3618 if (!mtd->eraseregions)
3619 return -ENOMEM;
3620 }
3621
3622 /*
3623 * For Flex-OneNAND, chipsize represents maximum possible device size.
3624 * mtd->size represents the actual device size.
3625 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003626 this->chipsize = (16 << density) << 20;
3627
3628 /* OneNAND page size & block size */
3629 /* The data buffer size is equal to page size */
Joern Engel28318772006-05-22 23:18:05 +02003630 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003631 /* We use the full BufferRAM */
3632 if (ONENAND_IS_MLC(this))
3633 mtd->writesize <<= 1;
3634
Joern Engel28318772006-05-22 23:18:05 +02003635 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09003636 /* Pages per a block are always 64 in OneNAND */
Joern Engel28318772006-05-22 23:18:05 +02003637 mtd->erasesize = mtd->writesize << 6;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003638 /*
3639 * Flex-OneNAND SLC area has 64 pages per block.
3640 * Flex-OneNAND MLC area has 128 pages per block.
3641 * Expose MLC erase size to find erase_shift and page_mask.
3642 */
3643 if (FLEXONENAND(this))
3644 mtd->erasesize <<= 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003645
3646 this->erase_shift = ffs(mtd->erasesize) - 1;
Joern Engel28318772006-05-22 23:18:05 +02003647 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09003648 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003649 /* Set density mask. it is used for DDP */
3650 if (ONENAND_IS_DDP(this))
3651 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003652 /* It's real page size */
3653 this->writesize = mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003654
Mika Korhonen492e1502009-06-09 21:52:35 +03003655 /* REVISIT: Multichip handling */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003656
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003657 if (FLEXONENAND(this))
3658 flexonenand_get_size(mtd);
3659 else
3660 mtd->size = this->chipsize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003661
Kyungmin Park75384b02007-01-18 11:10:57 +09003662 /* Check OneNAND features */
3663 onenand_check_features(mtd);
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00003664
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003665 /*
3666 * We emulate the 4KiB page and 256KiB erase block size
3667 * But oobsize is still 64 bytes.
3668 * It is only valid if you turn on 2X program support,
3669 * Otherwise it will be ignored by compiler.
3670 */
3671 if (ONENAND_IS_2PLANE(this)) {
3672 mtd->writesize <<= 1;
3673 mtd->erasesize <<= 1;
3674 }
3675
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003676 return 0;
3677}
3678
Kyungmin Parka41371e2005-09-29 03:55:31 +01003679/**
3680 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3681 * @param mtd MTD device structure
3682 */
3683static int onenand_suspend(struct mtd_info *mtd)
3684{
3685 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3686}
3687
3688/**
3689 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3690 * @param mtd MTD device structure
3691 */
3692static void onenand_resume(struct mtd_info *mtd)
3693{
3694 struct onenand_chip *this = mtd->priv;
3695
3696 if (this->state == FL_PM_SUSPENDED)
3697 onenand_release_device(mtd);
3698 else
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303699 printk(KERN_ERR "%s: resume() called for the chip which is not "
3700 "in suspended state\n", __func__);
Kyungmin Parka41371e2005-09-29 03:55:31 +01003701}
3702
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003703/**
3704 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3705 * @param mtd MTD device structure
3706 * @param maxchips Number of chips to scan for
3707 *
3708 * This fills out all the not initialized function pointers
3709 * with the defaults.
3710 * The flash ID is read and the mtd/chip structures are
3711 * filled with the appropriate values.
3712 */
3713int onenand_scan(struct mtd_info *mtd, int maxchips)
3714{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003715 int i, ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003716 struct onenand_chip *this = mtd->priv;
3717
3718 if (!this->read_word)
3719 this->read_word = onenand_readw;
3720 if (!this->write_word)
3721 this->write_word = onenand_writew;
3722
3723 if (!this->command)
3724 this->command = onenand_command;
3725 if (!this->wait)
Kyungmin Park2c221202006-11-16 11:23:48 +09003726 onenand_setup_wait(mtd);
Kyungmin Park31bb9992009-05-12 13:46:57 -07003727 if (!this->bbt_wait)
3728 this->bbt_wait = onenand_bbt_wait;
3729 if (!this->unlock_all)
3730 this->unlock_all = onenand_unlock_all;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003731
3732 if (!this->read_bufferram)
3733 this->read_bufferram = onenand_read_bufferram;
3734 if (!this->write_bufferram)
3735 this->write_bufferram = onenand_write_bufferram;
3736
Kyungmin Parkcdc00132005-09-03 07:15:48 +01003737 if (!this->block_markbad)
3738 this->block_markbad = onenand_default_block_markbad;
3739 if (!this->scan_bbt)
3740 this->scan_bbt = onenand_default_bbt;
3741
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003742 if (onenand_probe(mtd))
3743 return -ENXIO;
3744
Kyungmin Park52b0eea2005-09-03 07:07:19 +01003745 /* Set Sync. Burst Read after probing */
3746 if (this->mmcontrol) {
3747 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3748 this->read_bufferram = onenand_sync_read_bufferram;
3749 }
3750
Kyungmin Park532a37c2005-12-16 11:17:29 +09003751 /* Allocate buffers, if necessary */
3752 if (!this->page_buf) {
Kyungmin Park470bc842007-03-09 10:08:11 +09003753 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
Kyungmin Park532a37c2005-12-16 11:17:29 +09003754 if (!this->page_buf) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303755 printk(KERN_ERR "%s: Can't allocate page_buf\n",
3756 __func__);
Kyungmin Park532a37c2005-12-16 11:17:29 +09003757 return -ENOMEM;
3758 }
3759 this->options |= ONENAND_PAGEBUF_ALLOC;
3760 }
Kyungmin Park470bc842007-03-09 10:08:11 +09003761 if (!this->oob_buf) {
3762 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3763 if (!this->oob_buf) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303764 printk(KERN_ERR "%s: Can't allocate oob_buf\n",
3765 __func__);
Kyungmin Park470bc842007-03-09 10:08:11 +09003766 if (this->options & ONENAND_PAGEBUF_ALLOC) {
3767 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3768 kfree(this->page_buf);
3769 }
3770 return -ENOMEM;
3771 }
3772 this->options |= ONENAND_OOBBUF_ALLOC;
3773 }
Kyungmin Park532a37c2005-12-16 11:17:29 +09003774
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003775 this->state = FL_READY;
3776 init_waitqueue_head(&this->wq);
3777 spin_lock_init(&this->chip_lock);
3778
Kyungmin Park60d84f92006-12-22 16:21:54 +09003779 /*
3780 * Allow subpage writes up to oobsize.
3781 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003782 switch (mtd->oobsize) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003783 case 128:
3784 this->ecclayout = &onenand_oob_128;
3785 mtd->subpage_sft = 0;
3786 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003787 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003788 this->ecclayout = &onenand_oob_64;
Kyungmin Park60d84f92006-12-22 16:21:54 +09003789 mtd->subpage_sft = 2;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003790 break;
3791
3792 case 32:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003793 this->ecclayout = &onenand_oob_32;
Kyungmin Park60d84f92006-12-22 16:21:54 +09003794 mtd->subpage_sft = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003795 break;
3796
3797 default:
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303798 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
3799 __func__, mtd->oobsize);
Kyungmin Park60d84f92006-12-22 16:21:54 +09003800 mtd->subpage_sft = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003801 /* To prevent kernel oops */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003802 this->ecclayout = &onenand_oob_32;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003803 break;
3804 }
3805
Kyungmin Park60d84f92006-12-22 16:21:54 +09003806 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02003807
3808 /*
3809 * The number of bytes available for a client to place data into
3810 * the out of band area
3811 */
3812 this->ecclayout->oobavail = 0;
Kyungmin Parkad286342007-03-23 10:19:52 +09003813 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
3814 this->ecclayout->oobfree[i].length; i++)
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02003815 this->ecclayout->oobavail +=
3816 this->ecclayout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003817 mtd->oobavail = this->ecclayout->oobavail;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02003818
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003819 mtd->ecclayout = this->ecclayout;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00003820
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003821 /* Fill in remaining MTD driver data */
3822 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02003823 mtd->flags = MTD_CAP_NANDFLASH;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003824 mtd->erase = onenand_erase;
3825 mtd->point = NULL;
3826 mtd->unpoint = NULL;
3827 mtd->read = onenand_read;
3828 mtd->write = onenand_write;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003829 mtd->read_oob = onenand_read_oob;
3830 mtd->write_oob = onenand_write_oob;
Richard Purdie6c77fd62008-02-06 10:18:22 +00003831 mtd->panic_write = onenand_panic_write;
Kyungmin Park493c6462006-05-12 17:03:07 +03003832#ifdef CONFIG_MTD_ONENAND_OTP
3833 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
3834 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
3835 mtd->get_user_prot_info = onenand_get_user_prot_info;
3836 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
3837 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
3838 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
3839#endif
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003840 mtd->sync = onenand_sync;
Kyungmin Park08f782b2006-11-16 11:29:39 +09003841 mtd->lock = onenand_lock;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003842 mtd->unlock = onenand_unlock;
Kyungmin Parka41371e2005-09-29 03:55:31 +01003843 mtd->suspend = onenand_suspend;
3844 mtd->resume = onenand_resume;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003845 mtd->block_isbad = onenand_block_isbad;
3846 mtd->block_markbad = onenand_block_markbad;
3847 mtd->owner = THIS_MODULE;
3848
3849 /* Unlock whole block */
Kyungmin Park31bb9992009-05-12 13:46:57 -07003850 this->unlock_all(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003851
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003852 ret = this->scan_bbt(mtd);
3853 if ((!FLEXONENAND(this)) || ret)
3854 return ret;
3855
3856 /* Change Flex-OneNAND boundaries if required */
3857 for (i = 0; i < MAX_DIES; i++)
3858 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3859 flex_bdry[(2 * i) + 1]);
3860
3861 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003862}
3863
3864/**
3865 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3866 * @param mtd MTD device structure
3867 */
3868void onenand_release(struct mtd_info *mtd)
3869{
Kyungmin Park532a37c2005-12-16 11:17:29 +09003870 struct onenand_chip *this = mtd->priv;
3871
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003872#ifdef CONFIG_MTD_PARTITIONS
3873 /* Deregister partitions */
3874 del_mtd_partitions (mtd);
3875#endif
3876 /* Deregister the device */
3877 del_mtd_device (mtd);
Kyungmin Park532a37c2005-12-16 11:17:29 +09003878
3879 /* Free bad block table memory, if allocated */
Adrian Hunterf00b0042007-01-22 17:01:01 +09003880 if (this->bbm) {
3881 struct bbm_info *bbm = this->bbm;
3882 kfree(bbm->bbt);
Kyungmin Park532a37c2005-12-16 11:17:29 +09003883 kfree(this->bbm);
Adrian Hunterf00b0042007-01-22 17:01:01 +09003884 }
Kyungmin Park470bc842007-03-09 10:08:11 +09003885 /* Buffers allocated by onenand_scan */
Kyungmin Park532a37c2005-12-16 11:17:29 +09003886 if (this->options & ONENAND_PAGEBUF_ALLOC)
3887 kfree(this->page_buf);
Kyungmin Park470bc842007-03-09 10:08:11 +09003888 if (this->options & ONENAND_OOBBUF_ALLOC)
3889 kfree(this->oob_buf);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003890 kfree(mtd->eraseregions);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003891}
3892
3893EXPORT_SYMBOL_GPL(onenand_scan);
3894EXPORT_SYMBOL_GPL(onenand_release);
3895
3896MODULE_LICENSE("GPL");
3897MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
3898MODULE_DESCRIPTION("Generic OneNAND flash driver code");