blob: b2c40f67db83544b5672a3d3991271d4e45f8f9b [file] [log] [blame]
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
Kyungmin Park75384b02007-01-18 11:10:57 +09004 * Copyright (C) 2005-2007 Samsung Electronics
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01005 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Adrian Hunter81280d52007-02-15 09:47:29 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
Andrew Morton015953d2005-11-08 21:34:28 -080020#include <linux/sched.h>
Kyungmin Park2c221202006-11-16 11:23:48 +090021#include <linux/interrupt.h>
Andrew Morton015953d2005-11-08 21:34:28 -080022#include <linux/jiffies.h>
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010023#include <linux/mtd/mtd.h>
24#include <linux/mtd/onenand.h>
25#include <linux/mtd/partitions.h>
26
27#include <asm/io.h>
28
29/**
30 * onenand_oob_64 - oob info for large (2KB) page
31 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020032static struct nand_ecclayout onenand_oob_64 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010033 .eccbytes = 20,
34 .eccpos = {
35 8, 9, 10, 11, 12,
36 24, 25, 26, 27, 28,
37 40, 41, 42, 43, 44,
38 56, 57, 58, 59, 60,
39 },
40 .oobfree = {
41 {2, 3}, {14, 2}, {18, 3}, {30, 2},
Jarkko Lavinend9777f12006-05-12 17:02:35 +030042 {34, 3}, {46, 2}, {50, 3}, {62, 2}
43 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010044};
45
46/**
47 * onenand_oob_32 - oob info for middle (1KB) page
48 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020049static struct nand_ecclayout onenand_oob_32 = {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010050 .eccbytes = 10,
51 .eccpos = {
52 8, 9, 10, 11, 12,
53 24, 25, 26, 27, 28,
54 },
55 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
56};
57
58static const unsigned char ffchars[] = {
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
67};
68
69/**
70 * onenand_readw - [OneNAND Interface] Read OneNAND register
71 * @param addr address to read
72 *
73 * Read OneNAND register
74 */
75static unsigned short onenand_readw(void __iomem *addr)
76{
77 return readw(addr);
78}
79
80/**
81 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
82 * @param value value to write
83 * @param addr address to write
84 *
85 * Write OneNAND register with value
86 */
87static void onenand_writew(unsigned short value, void __iomem *addr)
88{
89 writew(value, addr);
90}
91
92/**
93 * onenand_block_address - [DEFAULT] Get block address
Kyungmin Park83a36832005-09-29 04:53:16 +010094 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010095 * @param block the block
96 * @return translated block address if DDP, otherwise same
97 *
98 * Setup Start Address 1 Register (F100h)
99 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100100static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100101{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900102 /* Device Flash Core select, NAND Flash Block Address */
103 if (block & this->density_mask)
104 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100105
106 return block;
107}
108
109/**
110 * onenand_bufferram_address - [DEFAULT] Get bufferram address
Kyungmin Park83a36832005-09-29 04:53:16 +0100111 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100112 * @param block the block
113 * @return set DBS value if DDP, otherwise 0
114 *
115 * Setup Start Address 2 Register (F101h) for DDP
116 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100117static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100118{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900119 /* Device BufferRAM Select */
120 if (block & this->density_mask)
121 return ONENAND_DDP_CHIP1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100122
Kyungmin Park738d61f2007-01-15 17:09:14 +0900123 return ONENAND_DDP_CHIP0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100124}
125
126/**
127 * onenand_page_address - [DEFAULT] Get page address
128 * @param page the page address
129 * @param sector the sector address
130 * @return combined page and sector address
131 *
132 * Setup Start Address 8 Register (F107h)
133 */
134static int onenand_page_address(int page, int sector)
135{
136 /* Flash Page Address, Flash Sector Address */
137 int fpa, fsa;
138
139 fpa = page & ONENAND_FPA_MASK;
140 fsa = sector & ONENAND_FSA_MASK;
141
142 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
143}
144
145/**
146 * onenand_buffer_address - [DEFAULT] Get buffer address
147 * @param dataram1 DataRAM index
148 * @param sectors the sector address
149 * @param count the number of sectors
150 * @return the start buffer value
151 *
152 * Setup Start Buffer Register (F200h)
153 */
154static int onenand_buffer_address(int dataram1, int sectors, int count)
155{
156 int bsa, bsc;
157
158 /* BufferRAM Sector Address */
159 bsa = sectors & ONENAND_BSA_MASK;
160
161 if (dataram1)
162 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
163 else
164 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
165
166 /* BufferRAM Sector Count */
167 bsc = count & ONENAND_BSC_MASK;
168
169 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
170}
171
172/**
173 * onenand_command - [DEFAULT] Send command to OneNAND device
174 * @param mtd MTD device structure
175 * @param cmd the command to be sent
176 * @param addr offset to read from or write to
177 * @param len number of bytes to read or write
178 *
179 * Send command to OneNAND device. This function is used for middle/large page
180 * devices (1KB/2KB Bytes per page)
181 */
182static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
183{
184 struct onenand_chip *this = mtd->priv;
Kyungmin Park493c6462006-05-12 17:03:07 +0300185 int value, readcmd = 0, block_cmd = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100186 int block, page;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100187
188 /* Address translation */
189 switch (cmd) {
190 case ONENAND_CMD_UNLOCK:
191 case ONENAND_CMD_LOCK:
192 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Park28b79ff2006-09-26 09:45:28 +0000193 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100194 block = -1;
195 page = -1;
196 break;
197
198 case ONENAND_CMD_ERASE:
199 case ONENAND_CMD_BUFFERRAM:
Kyungmin Park493c6462006-05-12 17:03:07 +0300200 case ONENAND_CMD_OTP_ACCESS:
201 block_cmd = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100202 block = (int) (addr >> this->erase_shift);
203 page = -1;
204 break;
205
206 default:
207 block = (int) (addr >> this->erase_shift);
208 page = (int) (addr >> this->page_shift);
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900209
210 if (ONENAND_IS_2PLANE(this)) {
211 /* Make the even block number */
212 block &= ~1;
213 /* Is it the odd plane? */
214 if (addr & this->writesize)
215 block++;
216 page >>= 1;
217 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100218 page &= this->page_mask;
219 break;
220 }
221
222 /* NOTE: The setting order of the registers is very important! */
223 if (cmd == ONENAND_CMD_BUFFERRAM) {
224 /* Select DataRAM for DDP */
Kyungmin Park83a36832005-09-29 04:53:16 +0100225 value = onenand_bufferram_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100226 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
227
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900228 if (ONENAND_IS_2PLANE(this))
229 /* It is always BufferRAM0 */
230 ONENAND_SET_BUFFERRAM0(this);
231 else
232 /* Switch to the next data buffer */
233 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100234
235 return 0;
236 }
237
238 if (block != -1) {
239 /* Write 'DFS, FBA' of Flash */
Kyungmin Park83a36832005-09-29 04:53:16 +0100240 value = onenand_block_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100241 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Park3cecf692006-05-12 17:02:51 +0300242
Kyungmin Park75287072006-05-12 17:03:23 +0300243 if (block_cmd) {
Kyungmin Park3cecf692006-05-12 17:02:51 +0300244 /* Select DataRAM for DDP */
245 value = onenand_bufferram_address(this, block);
246 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
247 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100248 }
249
250 if (page != -1) {
Kyungmin Park60d84f92006-12-22 16:21:54 +0900251 /* Now we use page size operation */
252 int sectors = 4, count = 4;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100253 int dataram;
254
255 switch (cmd) {
256 case ONENAND_CMD_READ:
257 case ONENAND_CMD_READOOB:
258 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
259 readcmd = 1;
260 break;
261
262 default:
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900263 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
264 cmd = ONENAND_CMD_2X_PROG;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100265 dataram = ONENAND_CURRENT_BUFFERRAM(this);
266 break;
267 }
268
269 /* Write 'FPA, FSA' of Flash */
270 value = onenand_page_address(page, sectors);
271 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
272
273 /* Write 'BSA, BSC' of DataRAM */
274 value = onenand_buffer_address(dataram, sectors, count);
275 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000276
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100277 if (readcmd) {
278 /* Select DataRAM for DDP */
Kyungmin Park83a36832005-09-29 04:53:16 +0100279 value = onenand_bufferram_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100280 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
281 }
282 }
283
284 /* Interrupt clear */
285 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
286
287 /* Write command */
288 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
289
290 return 0;
291}
292
293/**
294 * onenand_wait - [DEFAULT] wait until the command is done
295 * @param mtd MTD device structure
296 * @param state state to select the max. timeout value
297 *
298 * Wait for command done. This applies to all OneNAND command
299 * Read can take up to 30us, erase up to 2ms and program up to 350us
300 * according to general OneNAND specs
301 */
302static int onenand_wait(struct mtd_info *mtd, int state)
303{
304 struct onenand_chip * this = mtd->priv;
305 unsigned long timeout;
306 unsigned int flags = ONENAND_INT_MASTER;
307 unsigned int interrupt = 0;
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900308 unsigned int ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100309
310 /* The 20 msec is enough */
311 timeout = jiffies + msecs_to_jiffies(20);
312 while (time_before(jiffies, timeout)) {
313 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
314
315 if (interrupt & flags)
316 break;
317
318 if (state != FL_READING)
319 cond_resched();
320 }
321 /* To get correct interrupt status in timeout case */
322 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
323
324 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
325
326 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Park211ac752007-02-07 12:15:01 +0900327 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
Kyungmin Parkf6272482006-12-22 16:02:50 +0900328 if (ctrl & ONENAND_CTRL_LOCK)
Kyungmin Park211ac752007-02-07 12:15:01 +0900329 printk(KERN_ERR "onenand_wait: it's locked error.\n");
Kyungmin Parkf6272482006-12-22 16:02:50 +0900330 return ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100331 }
332
333 if (interrupt & ONENAND_INT_READ) {
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900334 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900335 if (ecc) {
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900336 if (ecc & ONENAND_ECC_2BIT_ALL) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300337 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900338 mtd->ecc_stats.failed++;
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900339 return ecc;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300340 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
341 printk(KERN_INFO "onenand_wait: correctable ECC error = 0x%04x\n", ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900342 mtd->ecc_stats.corrected++;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300343 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100344 }
Adrian Hunter9d032802007-01-10 07:51:26 +0200345 } else if (state == FL_READING) {
346 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
347 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100348 }
349
350 return 0;
351}
352
Kyungmin Park2c221202006-11-16 11:23:48 +0900353/*
354 * onenand_interrupt - [DEFAULT] onenand interrupt handler
355 * @param irq onenand interrupt number
356 * @param dev_id interrupt data
357 *
358 * complete the work
359 */
360static irqreturn_t onenand_interrupt(int irq, void *data)
361{
362 struct onenand_chip *this = (struct onenand_chip *) data;
363
364 /* To handle shared interrupt */
365 if (!this->complete.done)
366 complete(&this->complete);
367
368 return IRQ_HANDLED;
369}
370
371/*
372 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
373 * @param mtd MTD device structure
374 * @param state state to select the max. timeout value
375 *
376 * Wait for command done.
377 */
378static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
379{
380 struct onenand_chip *this = mtd->priv;
381
Kyungmin Park2c221202006-11-16 11:23:48 +0900382 wait_for_completion(&this->complete);
383
384 return onenand_wait(mtd, state);
385}
386
387/*
388 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
389 * @param mtd MTD device structure
390 * @param state state to select the max. timeout value
391 *
392 * Try interrupt based wait (It is used one-time)
393 */
394static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
395{
396 struct onenand_chip *this = mtd->priv;
397 unsigned long remain, timeout;
398
399 /* We use interrupt wait first */
400 this->wait = onenand_interrupt_wait;
401
Kyungmin Park2c221202006-11-16 11:23:48 +0900402 timeout = msecs_to_jiffies(100);
403 remain = wait_for_completion_timeout(&this->complete, timeout);
404 if (!remain) {
405 printk(KERN_INFO "OneNAND: There's no interrupt. "
406 "We use the normal wait\n");
407
408 /* Release the irq */
409 free_irq(this->irq, this);
David Woodhousec9ac5972006-11-30 08:17:38 +0000410
Kyungmin Park2c221202006-11-16 11:23:48 +0900411 this->wait = onenand_wait;
412 }
413
414 return onenand_wait(mtd, state);
415}
416
417/*
418 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
419 * @param mtd MTD device structure
420 *
421 * There's two method to wait onenand work
422 * 1. polling - read interrupt status register
423 * 2. interrupt - use the kernel interrupt method
424 */
425static void onenand_setup_wait(struct mtd_info *mtd)
426{
427 struct onenand_chip *this = mtd->priv;
428 int syscfg;
429
430 init_completion(&this->complete);
431
432 if (this->irq <= 0) {
433 this->wait = onenand_wait;
434 return;
435 }
436
437 if (request_irq(this->irq, &onenand_interrupt,
438 IRQF_SHARED, "onenand", this)) {
439 /* If we can't get irq, use the normal wait */
440 this->wait = onenand_wait;
441 return;
442 }
443
444 /* Enable interrupt */
445 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
446 syscfg |= ONENAND_SYS_CFG1_IOBE;
447 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
448
449 this->wait = onenand_try_interrupt_wait;
450}
451
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100452/**
453 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
454 * @param mtd MTD data structure
455 * @param area BufferRAM area
456 * @return offset given area
457 *
458 * Return BufferRAM offset given area
459 */
460static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
461{
462 struct onenand_chip *this = mtd->priv;
463
464 if (ONENAND_CURRENT_BUFFERRAM(this)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900465 /* Note: the 'this->writesize' is a real page size */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100466 if (area == ONENAND_DATARAM)
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900467 return this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100468 if (area == ONENAND_SPARERAM)
469 return mtd->oobsize;
470 }
471
472 return 0;
473}
474
475/**
476 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
477 * @param mtd MTD data structure
478 * @param area BufferRAM area
479 * @param buffer the databuffer to put/get data
480 * @param offset offset to read from or write to
481 * @param count number of bytes to read/write
482 *
483 * Read the BufferRAM area
484 */
485static int onenand_read_bufferram(struct mtd_info *mtd, int area,
486 unsigned char *buffer, int offset, size_t count)
487{
488 struct onenand_chip *this = mtd->priv;
489 void __iomem *bufferram;
490
491 bufferram = this->base + area;
492
493 bufferram += onenand_bufferram_offset(mtd, area);
494
Kyungmin Park9c01f872006-05-12 17:02:31 +0300495 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
496 unsigned short word;
497
498 /* Align with word(16-bit) size */
499 count--;
500
501 /* Read word and save byte */
502 word = this->read_word(bufferram + offset + count);
503 buffer[count] = (word & 0xff);
504 }
505
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100506 memcpy(buffer, bufferram + offset, count);
507
508 return 0;
509}
510
511/**
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100512 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
513 * @param mtd MTD data structure
514 * @param area BufferRAM area
515 * @param buffer the databuffer to put/get data
516 * @param offset offset to read from or write to
517 * @param count number of bytes to read/write
518 *
519 * Read the BufferRAM area with Sync. Burst Mode
520 */
521static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
522 unsigned char *buffer, int offset, size_t count)
523{
524 struct onenand_chip *this = mtd->priv;
525 void __iomem *bufferram;
526
527 bufferram = this->base + area;
528
529 bufferram += onenand_bufferram_offset(mtd, area);
530
531 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
532
Kyungmin Park9c01f872006-05-12 17:02:31 +0300533 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
534 unsigned short word;
535
536 /* Align with word(16-bit) size */
537 count--;
538
539 /* Read word and save byte */
540 word = this->read_word(bufferram + offset + count);
541 buffer[count] = (word & 0xff);
542 }
543
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100544 memcpy(buffer, bufferram + offset, count);
545
546 this->mmcontrol(mtd, 0);
547
548 return 0;
549}
550
551/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100552 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
553 * @param mtd MTD data structure
554 * @param area BufferRAM area
555 * @param buffer the databuffer to put/get data
556 * @param offset offset to read from or write to
557 * @param count number of bytes to read/write
558 *
559 * Write the BufferRAM area
560 */
561static int onenand_write_bufferram(struct mtd_info *mtd, int area,
562 const unsigned char *buffer, int offset, size_t count)
563{
564 struct onenand_chip *this = mtd->priv;
565 void __iomem *bufferram;
566
567 bufferram = this->base + area;
568
569 bufferram += onenand_bufferram_offset(mtd, area);
570
Kyungmin Park9c01f872006-05-12 17:02:31 +0300571 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
572 unsigned short word;
573 int byte_offset;
574
575 /* Align with word(16-bit) size */
576 count--;
577
578 /* Calculate byte access offset */
579 byte_offset = offset + count;
580
581 /* Read word and save byte */
582 word = this->read_word(bufferram + byte_offset);
583 word = (word & ~0xff) | buffer[count];
584 this->write_word(word, bufferram + byte_offset);
585 }
586
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100587 memcpy(bufferram + offset, buffer, count);
588
589 return 0;
590}
591
592/**
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900593 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
594 * @param mtd MTD data structure
595 * @param addr address to check
596 * @return blockpage address
597 *
598 * Get blockpage address at 2x program mode
599 */
600static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
601{
602 struct onenand_chip *this = mtd->priv;
603 int blockpage, block, page;
604
605 /* Calculate the even block number */
606 block = (int) (addr >> this->erase_shift) & ~1;
607 /* Is it the odd plane? */
608 if (addr & this->writesize)
609 block++;
610 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
611 blockpage = (block << 7) | page;
612
613 return blockpage;
614}
615
616/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100617 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
618 * @param mtd MTD data structure
619 * @param addr address to check
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000620 * @return 1 if there are valid data, otherwise 0
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100621 *
622 * Check bufferram if there is data we required
623 */
624static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
625{
626 struct onenand_chip *this = mtd->priv;
Adrian Huntercde36b32007-02-08 10:28:08 +0200627 int blockpage, found = 0;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900628 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000629
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900630 if (ONENAND_IS_2PLANE(this))
631 blockpage = onenand_get_2x_blockpage(mtd, addr);
632 else
633 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100634
635 /* Is there valid data? */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900636 i = ONENAND_CURRENT_BUFFERRAM(this);
637 if (this->bufferram[i].blockpage == blockpage)
Adrian Huntercde36b32007-02-08 10:28:08 +0200638 found = 1;
639 else {
640 /* Check another BufferRAM */
641 i = ONENAND_NEXT_BUFFERRAM(this);
642 if (this->bufferram[i].blockpage == blockpage) {
643 ONENAND_SET_NEXT_BUFFERRAM(this);
644 found = 1;
645 }
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900646 }
647
Adrian Huntercde36b32007-02-08 10:28:08 +0200648 if (found && ONENAND_IS_DDP(this)) {
649 /* Select DataRAM for DDP */
650 int block = (int) (addr >> this->erase_shift);
651 int value = onenand_bufferram_address(this, block);
652 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
653 }
654
655 return found;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100656}
657
658/**
659 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
660 * @param mtd MTD data structure
661 * @param addr address to update
662 * @param valid valid flag
663 *
664 * Update BufferRAM information
665 */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900666static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100667 int valid)
668{
669 struct onenand_chip *this = mtd->priv;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900670 int blockpage;
671 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000672
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900673 if (ONENAND_IS_2PLANE(this))
674 blockpage = onenand_get_2x_blockpage(mtd, addr);
675 else
676 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100677
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900678 /* Invalidate another BufferRAM */
679 i = ONENAND_NEXT_BUFFERRAM(this);
Kyungmin Park5b4246f2007-02-02 09:39:21 +0900680 if (this->bufferram[i].blockpage == blockpage)
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900681 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100682
683 /* Update BufferRAM */
684 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900685 if (valid)
686 this->bufferram[i].blockpage = blockpage;
687 else
688 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100689}
690
691/**
Adrian Hunter480b9df2007-02-07 13:55:19 +0200692 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
693 * @param mtd MTD data structure
694 * @param addr start address to invalidate
695 * @param len length to invalidate
696 *
697 * Invalidate BufferRAM information
698 */
699static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
700 unsigned int len)
701{
702 struct onenand_chip *this = mtd->priv;
703 int i;
704 loff_t end_addr = addr + len;
705
706 /* Invalidate BufferRAM */
707 for (i = 0; i < MAX_BUFFERRAM; i++) {
708 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
709 if (buf_addr >= addr && buf_addr < end_addr)
710 this->bufferram[i].blockpage = -1;
711 }
712}
713
714/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100715 * onenand_get_device - [GENERIC] Get chip for selected access
716 * @param mtd MTD device structure
717 * @param new_state the state which is requested
718 *
719 * Get the device and lock it for exclusive access
720 */
Kyungmin Parka41371e2005-09-29 03:55:31 +0100721static int onenand_get_device(struct mtd_info *mtd, int new_state)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100722{
723 struct onenand_chip *this = mtd->priv;
724 DECLARE_WAITQUEUE(wait, current);
725
726 /*
727 * Grab the lock and see if the device is available
728 */
729 while (1) {
730 spin_lock(&this->chip_lock);
731 if (this->state == FL_READY) {
732 this->state = new_state;
733 spin_unlock(&this->chip_lock);
734 break;
735 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100736 if (new_state == FL_PM_SUSPENDED) {
737 spin_unlock(&this->chip_lock);
738 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
739 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100740 set_current_state(TASK_UNINTERRUPTIBLE);
741 add_wait_queue(&this->wq, &wait);
742 spin_unlock(&this->chip_lock);
743 schedule();
744 remove_wait_queue(&this->wq, &wait);
745 }
Kyungmin Parka41371e2005-09-29 03:55:31 +0100746
747 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100748}
749
750/**
751 * onenand_release_device - [GENERIC] release chip
752 * @param mtd MTD device structure
753 *
754 * Deselect, release chip lock and wake up anyone waiting on the device
755 */
756static void onenand_release_device(struct mtd_info *mtd)
757{
758 struct onenand_chip *this = mtd->priv;
759
760 /* Release the chip */
761 spin_lock(&this->chip_lock);
762 this->state = FL_READY;
763 wake_up(&this->wq);
764 spin_unlock(&this->chip_lock);
765}
766
767/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900768 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
769 * @param mtd MTD device structure
770 * @param buf destination address
771 * @param column oob offset to read from
772 * @param thislen oob length to read
773 */
774static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
775 int thislen)
776{
777 struct onenand_chip *this = mtd->priv;
778 struct nand_oobfree *free;
779 int readcol = column;
780 int readend = column + thislen;
781 int lastgap = 0;
782 unsigned int i;
783 uint8_t *oob_buf = this->oob_buf;
784
785 free = this->ecclayout->oobfree;
786 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
787 if (readcol >= lastgap)
788 readcol += free->offset - lastgap;
789 if (readend >= lastgap)
790 readend += free->offset - lastgap;
791 lastgap = free->offset + free->length;
792 }
793 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
794 free = this->ecclayout->oobfree;
795 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
796 int free_end = free->offset + free->length;
797 if (free->offset < readend && free_end > readcol) {
798 int st = max_t(int,free->offset,readcol);
799 int ed = min_t(int,free_end,readend);
800 int n = ed - st;
801 memcpy(buf, oob_buf + st, n);
802 buf += n;
803 } else if (column == 0)
804 break;
805 }
806 return 0;
807}
808
809/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300810 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100811 * @param mtd MTD device structure
812 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900813 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100814 *
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900815 * OneNAND read main and/or out-of-band data
816 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300817static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900818 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100819{
820 struct onenand_chip *this = mtd->priv;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900821 struct mtd_ecc_stats stats;
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900822 size_t len = ops->len;
823 size_t ooblen = ops->ooblen;
824 u_char *buf = ops->datbuf;
825 u_char *oobbuf = ops->oobbuf;
826 int read = 0, column, thislen;
827 int oobread = 0, oobcolumn, thisooblen, oobsize;
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200828 int ret = 0, boundary = 0;
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900829 int writesize = this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100830
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300831 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900832
833 if (ops->mode == MTD_OOB_AUTO)
834 oobsize = this->ecclayout->oobavail;
835 else
836 oobsize = mtd->oobsize;
837
838 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100839
840 /* Do not allow reads past end of device */
841 if ((from + len) > mtd->size) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300842 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900843 ops->retlen = 0;
844 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100845 return -EINVAL;
846 }
847
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900848 stats = mtd->ecc_stats;
Artem Bityutskiy61a7e192006-12-26 16:41:24 +0900849
Adrian Huntera8de85d2007-01-04 09:51:26 +0200850 /* Read-while-load method */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100851
Adrian Huntera8de85d2007-01-04 09:51:26 +0200852 /* Do first load to bufferRAM */
853 if (read < len) {
854 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900855 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Huntera8de85d2007-01-04 09:51:26 +0200856 ret = this->wait(mtd, FL_READING);
857 onenand_update_bufferram(mtd, from, !ret);
858 }
859 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100860
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900861 thislen = min_t(int, writesize, len - read);
862 column = from & (writesize - 1);
863 if (column + thislen > writesize)
864 thislen = writesize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100865
Adrian Huntera8de85d2007-01-04 09:51:26 +0200866 while (!ret) {
867 /* If there is more to load then start next load */
868 from += thislen;
869 if (read + thislen < len) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900870 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200871 /*
872 * Chip boundary handling in DDP
873 * Now we issued chip 1 read and pointed chip 1
874 * bufferam so we have to point chip 0 bufferam.
875 */
Kyungmin Park738d61f2007-01-15 17:09:14 +0900876 if (ONENAND_IS_DDP(this) &&
877 unlikely(from == (this->chipsize >> 1))) {
878 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200879 boundary = 1;
880 } else
881 boundary = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +0200882 ONENAND_SET_PREV_BUFFERRAM(this);
883 }
884 /* While load is going, read from last bufferRAM */
885 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900886
887 /* Read oob area if needed */
888 if (oobbuf) {
889 thisooblen = oobsize - oobcolumn;
890 thisooblen = min_t(int, thisooblen, ooblen - oobread);
891
892 if (ops->mode == MTD_OOB_AUTO)
893 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
894 else
895 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
896 oobread += thisooblen;
897 oobbuf += thisooblen;
898 oobcolumn = 0;
899 }
900
Adrian Huntera8de85d2007-01-04 09:51:26 +0200901 /* See if we are done */
902 read += thislen;
903 if (read == len)
904 break;
905 /* Set up for next read from bufferRAM */
Adrian Hunter0fc2cce2007-01-09 17:55:21 +0200906 if (unlikely(boundary))
Kyungmin Park738d61f2007-01-15 17:09:14 +0900907 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Huntera8de85d2007-01-04 09:51:26 +0200908 ONENAND_SET_NEXT_BUFFERRAM(this);
909 buf += thislen;
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900910 thislen = min_t(int, writesize, len - read);
Adrian Huntera8de85d2007-01-04 09:51:26 +0200911 column = 0;
912 cond_resched();
913 /* Now wait for load */
914 ret = this->wait(mtd, FL_READING);
915 onenand_update_bufferram(mtd, from, !ret);
916 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100917
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100918 /*
919 * Return success, if no ECC failures, else -EBADMSG
920 * fs driver will take care of that, because
921 * retlen == desired len and result == -EBADMSG
922 */
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900923 ops->retlen = read;
924 ops->oobretlen = oobread;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900925
926 if (mtd->ecc_stats.failed - stats.failed)
927 return -EBADMSG;
928
Adrian Huntera8de85d2007-01-04 09:51:26 +0200929 if (ret)
930 return ret;
931
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900932 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100933}
934
935/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300936 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100937 * @param mtd MTD device structure
938 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +0900939 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100940 *
941 * OneNAND read out-of-band data from the spare area
942 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300943static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Park12f77c92007-08-30 09:36:05 +0900944 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100945{
946 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200947 int read = 0, thislen, column, oobsize;
Kyungmin Park12f77c92007-08-30 09:36:05 +0900948 size_t len = ops->ooblen;
949 mtd_oob_mode_t mode = ops->mode;
950 u_char *buf = ops->oobbuf;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100951 int ret = 0;
952
Kyungmin Park12f77c92007-08-30 09:36:05 +0900953 from += ops->ooboffs;
954
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300955 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100956
957 /* Initialize return length value */
Kyungmin Park12f77c92007-08-30 09:36:05 +0900958 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100959
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200960 if (mode == MTD_OOB_AUTO)
961 oobsize = this->ecclayout->oobavail;
962 else
963 oobsize = mtd->oobsize;
964
965 column = from & (mtd->oobsize - 1);
966
967 if (unlikely(column >= oobsize)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300968 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200969 return -EINVAL;
970 }
971
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100972 /* Do not allow reads past end of device */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200973 if (unlikely(from >= mtd->size ||
974 column + len > ((mtd->size >> this->page_shift) -
975 (from >> this->page_shift)) * oobsize)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300976 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100977 return -EINVAL;
978 }
979
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100980 while (read < len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +0900981 cond_resched();
982
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200983 thislen = oobsize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100984 thislen = min_t(int, thislen, len);
985
986 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
987
988 onenand_update_bufferram(mtd, from, 0);
989
990 ret = this->wait(mtd, FL_READING);
991 /* First copy data and check return value for ECC handling */
992
Adrian Huntera5e7c7b2007-01-31 17:19:28 +0200993 if (mode == MTD_OOB_AUTO)
994 onenand_transfer_auto_oob(mtd, buf, column, thislen);
995 else
996 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100997
Kyungmin Parkf6272482006-12-22 16:02:50 +0900998 if (ret) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300999 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09001000 break;
Kyungmin Parkf6272482006-12-22 16:02:50 +09001001 }
1002
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001003 read += thislen;
1004
1005 if (read == len)
1006 break;
1007
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001008 buf += thislen;
1009
1010 /* Read more? */
1011 if (read < len) {
1012 /* Page size */
Joern Engel28318772006-05-22 23:18:05 +02001013 from += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001014 column = 0;
1015 }
1016 }
1017
Kyungmin Park12f77c92007-08-30 09:36:05 +09001018 ops->oobretlen = read;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001019 return ret;
1020}
1021
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001022/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001023 * onenand_read - [MTD Interface] Read data from flash
1024 * @param mtd MTD device structure
1025 * @param from offset to read from
1026 * @param len number of bytes to read
1027 * @param retlen pointer to variable to store the number of read bytes
1028 * @param buf the databuffer to put data
1029 *
1030 * Read with ecc
1031*/
1032static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1033 size_t *retlen, u_char *buf)
1034{
1035 struct mtd_oob_ops ops = {
1036 .len = len,
1037 .ooblen = 0,
1038 .datbuf = buf,
1039 .oobbuf = NULL,
1040 };
1041 int ret;
1042
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001043 onenand_get_device(mtd, FL_READING);
1044 ret = onenand_read_ops_nolock(mtd, from, &ops);
1045 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001046
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001047 *retlen = ops.retlen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001048 return ret;
1049}
1050
1051/**
1052 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09001053 * @param mtd: MTD device structure
1054 * @param from: offset to read from
1055 * @param ops: oob operation description structure
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001056
1057 * Read main and/or out-of-band
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001058 */
1059static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1060 struct mtd_oob_ops *ops)
1061{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001062 int ret;
1063
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001064 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001065 case MTD_OOB_PLACE:
1066 case MTD_OOB_AUTO:
1067 break;
1068 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001069 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001070 default:
1071 return -EINVAL;
1072 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001073
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001074 onenand_get_device(mtd, FL_READING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001075 if (ops->datbuf)
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001076 ret = onenand_read_ops_nolock(mtd, from, ops);
1077 else
1078 ret = onenand_read_oob_nolock(mtd, from, ops);
1079 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001080
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001081 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001082}
1083
Kyungmin Park211ac752007-02-07 12:15:01 +09001084/**
1085 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1086 * @param mtd MTD device structure
1087 * @param state state to select the max. timeout value
1088 *
1089 * Wait for command done.
1090 */
1091static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1092{
1093 struct onenand_chip *this = mtd->priv;
1094 unsigned long timeout;
1095 unsigned int interrupt;
1096 unsigned int ctrl;
1097
1098 /* The 20 msec is enough */
1099 timeout = jiffies + msecs_to_jiffies(20);
1100 while (time_before(jiffies, timeout)) {
1101 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1102 if (interrupt & ONENAND_INT_MASTER)
1103 break;
1104 }
1105 /* To get correct interrupt status in timeout case */
1106 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1107 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1108
1109 if (ctrl & ONENAND_CTRL_ERROR) {
1110 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1111 /* Initial bad block case */
1112 if (ctrl & ONENAND_CTRL_LOAD)
1113 return ONENAND_BBT_READ_ERROR;
1114 return ONENAND_BBT_READ_FATAL_ERROR;
1115 }
1116
1117 if (interrupt & ONENAND_INT_READ) {
1118 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1119 if (ecc & ONENAND_ECC_2BIT_ALL)
1120 return ONENAND_BBT_READ_ERROR;
1121 } else {
1122 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1123 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1124 return ONENAND_BBT_READ_FATAL_ERROR;
1125 }
1126
1127 return 0;
1128}
1129
1130/**
1131 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1132 * @param mtd MTD device structure
1133 * @param from offset to read from
Kyungmin Parke3da8062007-02-15 09:36:39 +09001134 * @param ops oob operation description structure
Kyungmin Park211ac752007-02-07 12:15:01 +09001135 *
1136 * OneNAND read out-of-band data from the spare area for bbt scan
1137 */
1138int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1139 struct mtd_oob_ops *ops)
1140{
1141 struct onenand_chip *this = mtd->priv;
1142 int read = 0, thislen, column;
1143 int ret = 0;
1144 size_t len = ops->ooblen;
1145 u_char *buf = ops->oobbuf;
1146
Andrew Morton5785bdd2007-02-17 16:02:05 -08001147 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park211ac752007-02-07 12:15:01 +09001148
1149 /* Initialize return value */
1150 ops->oobretlen = 0;
1151
1152 /* Do not allow reads past end of device */
1153 if (unlikely((from + len) > mtd->size)) {
1154 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1155 return ONENAND_BBT_READ_FATAL_ERROR;
1156 }
1157
1158 /* Grab the lock and see if the device is available */
1159 onenand_get_device(mtd, FL_READING);
1160
1161 column = from & (mtd->oobsize - 1);
1162
1163 while (read < len) {
1164 cond_resched();
1165
1166 thislen = mtd->oobsize - column;
1167 thislen = min_t(int, thislen, len);
1168
1169 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1170
1171 onenand_update_bufferram(mtd, from, 0);
1172
1173 ret = onenand_bbt_wait(mtd, FL_READING);
1174 if (ret)
1175 break;
1176
1177 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1178 read += thislen;
1179 if (read == len)
1180 break;
1181
1182 buf += thislen;
1183
1184 /* Read more? */
1185 if (read < len) {
1186 /* Update Page size */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001187 from += this->writesize;
Kyungmin Park211ac752007-02-07 12:15:01 +09001188 column = 0;
1189 }
1190 }
1191
1192 /* Deselect and wake up anyone waiting on the device */
1193 onenand_release_device(mtd);
1194
1195 ops->oobretlen = read;
1196 return ret;
1197}
1198
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001199#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1200/**
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001201 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1202 * @param mtd MTD device structure
1203 * @param buf the databuffer to verify
1204 * @param to offset to read from
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001205 */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001206static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001207{
1208 struct onenand_chip *this = mtd->priv;
Kyungmin Park91014e92007-02-12 10:34:39 +09001209 char oobbuf[64];
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001210 int status, i;
1211
1212 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1213 onenand_update_bufferram(mtd, to, 0);
1214 status = this->wait(mtd, FL_READING);
1215 if (status)
1216 return status;
1217
Kyungmin Park91014e92007-02-12 10:34:39 +09001218 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1219 for (i = 0; i < mtd->oobsize; i++)
1220 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001221 return -EBADMSG;
1222
1223 return 0;
1224}
1225
1226/**
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001227 * onenand_verify - [GENERIC] verify the chip contents after a write
1228 * @param mtd MTD device structure
1229 * @param buf the databuffer to verify
1230 * @param addr offset to read from
1231 * @param len number of bytes to read and compare
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001232 */
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001233static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001234{
1235 struct onenand_chip *this = mtd->priv;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001236 void __iomem *dataram;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001237 int ret = 0;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001238 int thislen, column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001239
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001240 while (len != 0) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001241 thislen = min_t(int, this->writesize, len);
1242 column = addr & (this->writesize - 1);
1243 if (column + thislen > this->writesize)
1244 thislen = this->writesize - column;
Kyungmin Park60d84f92006-12-22 16:21:54 +09001245
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001246 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001247
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001248 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001249
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001250 ret = this->wait(mtd, FL_READING);
1251 if (ret)
1252 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001253
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001254 onenand_update_bufferram(mtd, addr, 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001255
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001256 dataram = this->base + ONENAND_DATARAM;
1257 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1258
1259 if (memcmp(buf, dataram + column, thislen))
1260 return -EBADMSG;
1261
1262 len -= thislen;
1263 buf += thislen;
1264 addr += thislen;
1265 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001266
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001267 return 0;
1268}
1269#else
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001270#define onenand_verify(...) (0)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001271#define onenand_verify_oob(...) (0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001272#endif
1273
Kyungmin Park60d84f92006-12-22 16:21:54 +09001274#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001275
1276/**
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001277 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1278 * @param mtd MTD device structure
1279 * @param oob_buf oob buffer
1280 * @param buf source address
1281 * @param column oob offset to write to
1282 * @param thislen oob length to write
1283 */
1284static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1285 const u_char *buf, int column, int thislen)
1286{
1287 struct onenand_chip *this = mtd->priv;
1288 struct nand_oobfree *free;
1289 int writecol = column;
1290 int writeend = column + thislen;
1291 int lastgap = 0;
Kyungmin Parkad286342007-03-23 10:19:52 +09001292 unsigned int i;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001293
Kyungmin Parkad286342007-03-23 10:19:52 +09001294 free = this->ecclayout->oobfree;
1295 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001296 if (writecol >= lastgap)
1297 writecol += free->offset - lastgap;
1298 if (writeend >= lastgap)
1299 writeend += free->offset - lastgap;
1300 lastgap = free->offset + free->length;
1301 }
Kyungmin Parkad286342007-03-23 10:19:52 +09001302 free = this->ecclayout->oobfree;
1303 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001304 int free_end = free->offset + free->length;
1305 if (free->offset < writeend && free_end > writecol) {
1306 int st = max_t(int,free->offset,writecol);
1307 int ed = min_t(int,free_end,writeend);
1308 int n = ed - st;
1309 memcpy(oob_buf + st, buf, n);
1310 buf += n;
Adrian Hunterc36c46d2007-03-23 17:16:22 +09001311 } else if (column == 0)
Kyungmin Park5bc399e2007-03-09 09:41:07 +09001312 break;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001313 }
1314 return 0;
1315}
1316
1317/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001318 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001319 * @param mtd MTD device structure
1320 * @param to offset to write to
1321 * @param ops oob operation description structure
1322 *
1323 * Write main and/or oob with ECC
1324 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001325static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001326 struct mtd_oob_ops *ops)
1327{
1328 struct onenand_chip *this = mtd->priv;
1329 int written = 0, column, thislen, subpage;
1330 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1331 size_t len = ops->len;
1332 size_t ooblen = ops->ooblen;
1333 const u_char *buf = ops->datbuf;
1334 const u_char *oob = ops->oobbuf;
1335 u_char *oobbuf;
1336 int ret = 0;
1337
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001338 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001339
1340 /* Initialize retlen, in case of early exit */
1341 ops->retlen = 0;
1342 ops->oobretlen = 0;
1343
1344 /* Do not allow writes past end of device */
1345 if (unlikely((to + len) > mtd->size)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001346 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001347 return -EINVAL;
1348 }
1349
1350 /* Reject writes, which are not page aligned */
1351 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001352 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001353 return -EINVAL;
1354 }
1355
1356 if (ops->mode == MTD_OOB_AUTO)
1357 oobsize = this->ecclayout->oobavail;
1358 else
1359 oobsize = mtd->oobsize;
1360
1361 oobcolumn = to & (mtd->oobsize - 1);
1362
1363 column = to & (mtd->writesize - 1);
1364
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001365 /* Loop until all data write */
1366 while (written < len) {
1367 u_char *wbuf = (u_char *) buf;
1368
1369 thislen = min_t(int, mtd->writesize - column, len - written);
1370 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1371
1372 cond_resched();
1373
1374 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1375
1376 /* Partial page write */
1377 subpage = thislen < mtd->writesize;
1378 if (subpage) {
1379 memset(this->page_buf, 0xff, mtd->writesize);
1380 memcpy(this->page_buf + column, buf, thislen);
1381 wbuf = this->page_buf;
1382 }
1383
1384 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1385
1386 if (oob) {
1387 oobbuf = this->oob_buf;
1388
1389 /* We send data to spare ram with oobsize
1390 * to prevent byte access */
1391 memset(oobbuf, 0xff, mtd->oobsize);
1392 if (ops->mode == MTD_OOB_AUTO)
1393 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1394 else
1395 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1396
1397 oobwritten += thisooblen;
1398 oob += thisooblen;
1399 oobcolumn = 0;
1400 } else
1401 oobbuf = (u_char *) ffchars;
1402
1403 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1404
1405 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1406
1407 ret = this->wait(mtd, FL_WRITING);
1408
1409 /* In partial page write we don't update bufferram */
1410 onenand_update_bufferram(mtd, to, !ret && !subpage);
1411 if (ONENAND_IS_2PLANE(this)) {
1412 ONENAND_SET_BUFFERRAM1(this);
1413 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1414 }
1415
1416 if (ret) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001417 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001418 break;
1419 }
1420
1421 /* Only check verify write turn on */
1422 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1423 if (ret) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001424 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001425 break;
1426 }
1427
1428 written += thislen;
1429
1430 if (written == len)
1431 break;
1432
1433 column = 0;
1434 to += thislen;
1435 buf += thislen;
1436 }
1437
1438 /* Deselect and wake up anyone waiting on the device */
1439 onenand_release_device(mtd);
1440
1441 ops->retlen = written;
1442
1443 return ret;
1444}
1445
1446
1447/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001448 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001449 * @param mtd MTD device structure
1450 * @param to offset to write to
1451 * @param len number of bytes to write
1452 * @param retlen pointer to variable to store the number of written bytes
1453 * @param buf the data to write
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001454 * @param mode operation mode
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001455 *
1456 * OneNAND write out-of-band
1457 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001458static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1459 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001460{
1461 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001462 int column, ret = 0, oobsize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001463 int written = 0;
Kyungmin Park91014e92007-02-12 10:34:39 +09001464 u_char *oobbuf;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001465 size_t len = ops->ooblen;
1466 const u_char *buf = ops->oobbuf;
1467 mtd_oob_mode_t mode = ops->mode;
1468
1469 to += ops->ooboffs;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001470
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001471 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001472
1473 /* Initialize retlen, in case of early exit */
Kyungmin Park12f77c92007-08-30 09:36:05 +09001474 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001475
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001476 if (mode == MTD_OOB_AUTO)
1477 oobsize = this->ecclayout->oobavail;
1478 else
1479 oobsize = mtd->oobsize;
1480
1481 column = to & (mtd->oobsize - 1);
1482
1483 if (unlikely(column >= oobsize)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001484 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001485 return -EINVAL;
1486 }
1487
Adrian Hunter52e42002007-02-06 09:15:39 +09001488 /* For compatibility with NAND: Do not allow write past end of page */
Kyungmin Park91014e92007-02-12 10:34:39 +09001489 if (unlikely(column + len > oobsize)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001490 printk(KERN_ERR "onenand_write_oob_nolock: "
Adrian Hunter52e42002007-02-06 09:15:39 +09001491 "Attempt to write past end of page\n");
1492 return -EINVAL;
1493 }
1494
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001495 /* Do not allow reads past end of device */
1496 if (unlikely(to >= mtd->size ||
1497 column + len > ((mtd->size >> this->page_shift) -
1498 (to >> this->page_shift)) * oobsize)) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001499 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001500 return -EINVAL;
1501 }
1502
Kyungmin Park470bc842007-03-09 10:08:11 +09001503 oobbuf = this->oob_buf;
Kyungmin Park91014e92007-02-12 10:34:39 +09001504
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001505 /* Loop until all data write */
1506 while (written < len) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001507 int thislen = min_t(int, oobsize, len - written);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001508
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001509 cond_resched();
1510
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001511 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1512
Kyungmin Park34c10602006-05-12 17:02:46 +03001513 /* We send data to spare ram with oobsize
1514 * to prevent byte access */
Kyungmin Park91014e92007-02-12 10:34:39 +09001515 memset(oobbuf, 0xff, mtd->oobsize);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001516 if (mode == MTD_OOB_AUTO)
Kyungmin Park91014e92007-02-12 10:34:39 +09001517 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001518 else
Kyungmin Park91014e92007-02-12 10:34:39 +09001519 memcpy(oobbuf + column, buf, thislen);
1520 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001521
1522 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1523
1524 onenand_update_bufferram(mtd, to, 0);
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001525 if (ONENAND_IS_2PLANE(this)) {
1526 ONENAND_SET_BUFFERRAM1(this);
1527 onenand_update_bufferram(mtd, to + this->writesize, 0);
1528 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001529
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001530 ret = this->wait(mtd, FL_WRITING);
1531 if (ret) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001532 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09001533 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001534 }
1535
Kyungmin Park91014e92007-02-12 10:34:39 +09001536 ret = onenand_verify_oob(mtd, oobbuf, to);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001537 if (ret) {
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001538 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09001539 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001540 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001541
1542 written += thislen;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001543 if (written == len)
1544 break;
1545
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001546 to += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001547 buf += thislen;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001548 column = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001549 }
1550
Kyungmin Park12f77c92007-08-30 09:36:05 +09001551 ops->oobretlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001552
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001553 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001554}
1555
1556/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001557 * onenand_write - [MTD Interface] write buffer to FLASH
1558 * @param mtd MTD device structure
1559 * @param to offset to write to
1560 * @param len number of bytes to write
1561 * @param retlen pointer to variable to store the number of written bytes
1562 * @param buf the data to write
1563 *
1564 * Write with ECC
1565 */
1566static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1567 size_t *retlen, const u_char *buf)
1568{
1569 struct mtd_oob_ops ops = {
1570 .len = len,
1571 .ooblen = 0,
1572 .datbuf = (u_char *) buf,
1573 .oobbuf = NULL,
1574 };
1575 int ret;
1576
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001577 onenand_get_device(mtd, FL_WRITING);
1578 ret = onenand_write_ops_nolock(mtd, to, &ops);
1579 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001580
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001581 *retlen = ops.retlen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001582 return ret;
1583}
1584
1585/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001586 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09001587 * @param mtd: MTD device structure
1588 * @param to: offset to write
1589 * @param ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001590 */
1591static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1592 struct mtd_oob_ops *ops)
1593{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001594 int ret;
1595
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001596 switch (ops->mode) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001597 case MTD_OOB_PLACE:
1598 case MTD_OOB_AUTO:
1599 break;
1600 case MTD_OOB_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001601 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001602 default:
1603 return -EINVAL;
1604 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001605
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001606 onenand_get_device(mtd, FL_WRITING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001607 if (ops->datbuf)
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001608 ret = onenand_write_ops_nolock(mtd, to, ops);
1609 else
1610 ret = onenand_write_oob_nolock(mtd, to, ops);
1611 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001612
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001613 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001614}
1615
1616/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001617 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001618 * @param mtd MTD device structure
1619 * @param ofs offset from device start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001620 * @param allowbbt 1, if its allowed to access the bbt area
1621 *
1622 * Check, if the block is bad. Either by reading the bad block table or
1623 * calling of the scan function.
1624 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001625static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001626{
1627 struct onenand_chip *this = mtd->priv;
1628 struct bbm_info *bbm = this->bbm;
1629
1630 /* Return info from the table */
1631 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1632}
1633
1634/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001635 * onenand_erase - [MTD Interface] erase block(s)
1636 * @param mtd MTD device structure
1637 * @param instr erase instruction
1638 *
1639 * Erase one ore more blocks
1640 */
1641static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1642{
1643 struct onenand_chip *this = mtd->priv;
1644 unsigned int block_size;
1645 loff_t addr;
1646 int len;
1647 int ret = 0;
1648
1649 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1650
1651 block_size = (1 << this->erase_shift);
1652
1653 /* Start address must align on block boundary */
1654 if (unlikely(instr->addr & (block_size - 1))) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001655 printk(KERN_ERR "onenand_erase: Unaligned address\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001656 return -EINVAL;
1657 }
1658
1659 /* Length must align on block boundary */
1660 if (unlikely(instr->len & (block_size - 1))) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001661 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001662 return -EINVAL;
1663 }
1664
1665 /* Do not allow erase past end of device */
1666 if (unlikely((instr->len + instr->addr) > mtd->size)) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001667 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001668 return -EINVAL;
1669 }
1670
1671 instr->fail_addr = 0xffffffff;
1672
1673 /* Grab the lock and see if the device is available */
1674 onenand_get_device(mtd, FL_ERASING);
1675
1676 /* Loop throught the pages */
1677 len = instr->len;
1678 addr = instr->addr;
1679
1680 instr->state = MTD_ERASING;
1681
1682 while (len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001683 cond_resched();
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001684
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001685 /* Check if we have a bad block, we do not erase bad blocks */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001686 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001687 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1688 instr->state = MTD_ERASE_FAILED;
1689 goto erase_exit;
1690 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001691
1692 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1693
Adrian Hunter480b9df2007-02-07 13:55:19 +02001694 onenand_invalidate_bufferram(mtd, addr, block_size);
1695
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001696 ret = this->wait(mtd, FL_ERASING);
1697 /* Check, if it is write protected */
1698 if (ret) {
Kyungmin Park211ac752007-02-07 12:15:01 +09001699 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001700 instr->state = MTD_ERASE_FAILED;
1701 instr->fail_addr = addr;
1702 goto erase_exit;
1703 }
1704
1705 len -= block_size;
1706 addr += block_size;
1707 }
1708
1709 instr->state = MTD_ERASE_DONE;
1710
1711erase_exit:
1712
1713 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1714 /* Do call back function */
1715 if (!ret)
1716 mtd_erase_callback(instr);
1717
1718 /* Deselect and wake up anyone waiting on the device */
1719 onenand_release_device(mtd);
1720
1721 return ret;
1722}
1723
1724/**
1725 * onenand_sync - [MTD Interface] sync
1726 * @param mtd MTD device structure
1727 *
1728 * Sync is actually a wait for chip ready function
1729 */
1730static void onenand_sync(struct mtd_info *mtd)
1731{
1732 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1733
1734 /* Grab the lock and see if the device is available */
1735 onenand_get_device(mtd, FL_SYNCING);
1736
1737 /* Release it and go back */
1738 onenand_release_device(mtd);
1739}
1740
1741/**
1742 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1743 * @param mtd MTD device structure
1744 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001745 *
1746 * Check whether the block is bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001747 */
1748static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1749{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001750 int ret;
1751
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001752 /* Check for invalid offset */
1753 if (ofs > mtd->size)
1754 return -EINVAL;
1755
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001756 onenand_get_device(mtd, FL_READING);
1757 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1758 onenand_release_device(mtd);
1759 return ret;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001760}
1761
1762/**
1763 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1764 * @param mtd MTD device structure
1765 * @param ofs offset from device start
1766 *
1767 * This is the default implementation, which can be overridden by
1768 * a hardware specific driver.
1769 */
1770static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1771{
1772 struct onenand_chip *this = mtd->priv;
1773 struct bbm_info *bbm = this->bbm;
1774 u_char buf[2] = {0, 0};
Kyungmin Park12f77c92007-08-30 09:36:05 +09001775 struct mtd_oob_ops ops = {
1776 .mode = MTD_OOB_PLACE,
1777 .ooblen = 2,
1778 .oobbuf = buf,
1779 .ooboffs = 0,
1780 };
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001781 int block;
1782
1783 /* Get block number */
1784 block = ((int) ofs) >> bbm->bbt_erase_shift;
1785 if (bbm->bbt)
1786 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1787
1788 /* We write two bytes, so we dont have to mess with 16 bit access */
1789 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001790 return onenand_write_oob_nolock(mtd, ofs, &ops);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001791}
1792
1793/**
1794 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1795 * @param mtd MTD device structure
1796 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001797 *
1798 * Mark the block as bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001799 */
1800static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1801{
Kyungmin Parkcdc00132005-09-03 07:15:48 +01001802 struct onenand_chip *this = mtd->priv;
1803 int ret;
1804
1805 ret = onenand_block_isbad(mtd, ofs);
1806 if (ret) {
1807 /* If it was bad already, return success and do nothing */
1808 if (ret > 0)
1809 return 0;
1810 return ret;
1811 }
1812
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001813 onenand_get_device(mtd, FL_WRITING);
1814 ret = this->block_markbad(mtd, ofs);
1815 onenand_release_device(mtd);
1816 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001817}
1818
1819/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09001820 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001821 * @param mtd MTD device structure
1822 * @param ofs offset relative to mtd start
Kyungmin Park08f782b2006-11-16 11:29:39 +09001823 * @param len number of bytes to lock or unlock
Kyungmin Parke3da8062007-02-15 09:36:39 +09001824 * @param cmd lock or unlock command
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001825 *
Kyungmin Park08f782b2006-11-16 11:29:39 +09001826 * Lock or unlock one or more blocks
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001827 */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001828static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001829{
1830 struct onenand_chip *this = mtd->priv;
1831 int start, end, block, value, status;
Kyungmin Park08f782b2006-11-16 11:29:39 +09001832 int wp_status_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001833
1834 start = ofs >> this->erase_shift;
1835 end = len >> this->erase_shift;
1836
Kyungmin Park08f782b2006-11-16 11:29:39 +09001837 if (cmd == ONENAND_CMD_LOCK)
1838 wp_status_mask = ONENAND_WP_LS;
1839 else
1840 wp_status_mask = ONENAND_WP_US;
1841
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001842 /* Continuous lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001843 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001844 /* Set start block address */
1845 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1846 /* Set end block address */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001847 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001848 /* Write lock command */
1849 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001850
1851 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001852 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001853
1854 /* Sanity check */
1855 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1856 & ONENAND_CTRL_ONGO)
1857 continue;
1858
1859 /* Check lock status */
1860 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001861 if (!(status & wp_status_mask))
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001862 printk(KERN_ERR "wp status = 0x%x\n", status);
1863
1864 return 0;
1865 }
1866
1867 /* Block lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001868 for (block = start; block < start + end; block++) {
Kyungmin Park20ba89a2005-12-16 11:17:29 +09001869 /* Set block address */
1870 value = onenand_block_address(this, block);
1871 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1872 /* Select DataRAM for DDP */
1873 value = onenand_bufferram_address(this, block);
1874 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001875 /* Set start block address */
1876 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001877 /* Write lock command */
1878 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001879
1880 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001881 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001882
1883 /* Sanity check */
1884 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1885 & ONENAND_CTRL_ONGO)
1886 continue;
1887
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001888 /* Check lock status */
1889 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09001890 if (!(status & wp_status_mask))
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001891 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1892 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001893
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001894 return 0;
1895}
1896
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001897/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09001898 * onenand_lock - [MTD Interface] Lock block(s)
1899 * @param mtd MTD device structure
1900 * @param ofs offset relative to mtd start
1901 * @param len number of bytes to unlock
1902 *
1903 * Lock one or more blocks
1904 */
1905static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1906{
1907 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1908}
1909
Kyungmin Park08f782b2006-11-16 11:29:39 +09001910/**
1911 * onenand_unlock - [MTD Interface] Unlock block(s)
1912 * @param mtd MTD device structure
1913 * @param ofs offset relative to mtd start
1914 * @param len number of bytes to unlock
1915 *
1916 * Unlock one or more blocks
1917 */
1918static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1919{
1920 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1921}
1922
1923/**
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001924 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1925 * @param this onenand chip data structure
1926 *
1927 * Check lock status
1928 */
1929static void onenand_check_lock_status(struct onenand_chip *this)
1930{
1931 unsigned int value, block, status;
1932 unsigned int end;
1933
1934 end = this->chipsize >> this->erase_shift;
1935 for (block = 0; block < end; block++) {
1936 /* Set block address */
1937 value = onenand_block_address(this, block);
1938 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1939 /* Select DataRAM for DDP */
1940 value = onenand_bufferram_address(this, block);
1941 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1942 /* Set start block address */
1943 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1944
1945 /* Check lock status */
1946 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1947 if (!(status & ONENAND_WP_US))
1948 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1949 }
1950}
1951
1952/**
1953 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1954 * @param mtd MTD device structure
1955 *
1956 * Unlock all blocks
1957 */
1958static int onenand_unlock_all(struct mtd_info *mtd)
1959{
1960 struct onenand_chip *this = mtd->priv;
1961
1962 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09001963 /* Set start block address */
1964 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001965 /* Write unlock command */
1966 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1967
1968 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09001969 this->wait(mtd, FL_LOCKING);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001970
1971 /* Sanity check */
1972 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1973 & ONENAND_CTRL_ONGO)
1974 continue;
1975
1976 /* Workaround for all block unlock in DDP */
Kyungmin Park738d61f2007-01-15 17:09:14 +09001977 if (ONENAND_IS_DDP(this)) {
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001978 /* 1st block on another chip */
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09001979 loff_t ofs = this->chipsize >> 1;
1980 size_t len = mtd->erasesize;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001981
1982 onenand_unlock(mtd, ofs, len);
1983 }
1984
1985 onenand_check_lock_status(this);
1986
1987 return 0;
1988 }
1989
Kyungmin Park08f782b2006-11-16 11:29:39 +09001990 onenand_unlock(mtd, 0x0, this->chipsize);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00001991
1992 return 0;
1993}
1994
Kyungmin Park493c6462006-05-12 17:03:07 +03001995#ifdef CONFIG_MTD_ONENAND_OTP
1996
1997/* Interal OTP operation */
1998typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1999 size_t *retlen, u_char *buf);
2000
2001/**
2002 * do_otp_read - [DEFAULT] Read OTP block area
2003 * @param mtd MTD device structure
2004 * @param from The offset to read
2005 * @param len number of bytes to read
2006 * @param retlen pointer to variable to store the number of readbytes
2007 * @param buf the databuffer to put/get data
2008 *
2009 * Read OTP block area.
2010 */
2011static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2012 size_t *retlen, u_char *buf)
2013{
2014 struct onenand_chip *this = mtd->priv;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002015 struct mtd_oob_ops ops = {
2016 .len = len,
2017 .ooblen = 0,
2018 .datbuf = buf,
2019 .oobbuf = NULL,
2020 };
Kyungmin Park493c6462006-05-12 17:03:07 +03002021 int ret;
2022
2023 /* Enter OTP access mode */
2024 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2025 this->wait(mtd, FL_OTPING);
2026
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002027 ret = onenand_read_ops_nolock(mtd, from, &ops);
Kyungmin Park493c6462006-05-12 17:03:07 +03002028
2029 /* Exit OTP access mode */
2030 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2031 this->wait(mtd, FL_RESETING);
2032
2033 return ret;
2034}
2035
2036/**
2037 * do_otp_write - [DEFAULT] Write OTP block area
2038 * @param mtd MTD device structure
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002039 * @param to The offset to write
Kyungmin Park493c6462006-05-12 17:03:07 +03002040 * @param len number of bytes to write
2041 * @param retlen pointer to variable to store the number of write bytes
2042 * @param buf the databuffer to put/get data
2043 *
2044 * Write OTP block area.
2045 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002046static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
Kyungmin Park493c6462006-05-12 17:03:07 +03002047 size_t *retlen, u_char *buf)
2048{
2049 struct onenand_chip *this = mtd->priv;
2050 unsigned char *pbuf = buf;
2051 int ret;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002052 struct mtd_oob_ops ops;
Kyungmin Park493c6462006-05-12 17:03:07 +03002053
2054 /* Force buffer page aligned */
Joern Engel28318772006-05-22 23:18:05 +02002055 if (len < mtd->writesize) {
Kyungmin Park493c6462006-05-12 17:03:07 +03002056 memcpy(this->page_buf, buf, len);
Joern Engel28318772006-05-22 23:18:05 +02002057 memset(this->page_buf + len, 0xff, mtd->writesize - len);
Kyungmin Park493c6462006-05-12 17:03:07 +03002058 pbuf = this->page_buf;
Joern Engel28318772006-05-22 23:18:05 +02002059 len = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03002060 }
2061
2062 /* Enter OTP access mode */
2063 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2064 this->wait(mtd, FL_OTPING);
2065
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002066 ops.len = len;
2067 ops.ooblen = 0;
Kyungmin Park14370852007-10-10 13:48:14 +09002068 ops.datbuf = pbuf;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002069 ops.oobbuf = NULL;
2070 ret = onenand_write_ops_nolock(mtd, to, &ops);
2071 *retlen = ops.retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03002072
2073 /* Exit OTP access mode */
2074 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2075 this->wait(mtd, FL_RESETING);
2076
2077 return ret;
2078}
2079
2080/**
2081 * do_otp_lock - [DEFAULT] Lock OTP block area
2082 * @param mtd MTD device structure
2083 * @param from The offset to lock
2084 * @param len number of bytes to lock
2085 * @param retlen pointer to variable to store the number of lock bytes
2086 * @param buf the databuffer to put/get data
2087 *
2088 * Lock OTP block area.
2089 */
2090static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2091 size_t *retlen, u_char *buf)
2092{
2093 struct onenand_chip *this = mtd->priv;
Kyungmin Park12f77c92007-08-30 09:36:05 +09002094 struct mtd_oob_ops ops = {
2095 .mode = MTD_OOB_PLACE,
2096 .ooblen = len,
2097 .oobbuf = buf,
2098 .ooboffs = 0,
2099 };
Kyungmin Park493c6462006-05-12 17:03:07 +03002100 int ret;
2101
2102 /* Enter OTP access mode */
2103 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2104 this->wait(mtd, FL_OTPING);
2105
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002106 ret = onenand_write_oob_nolock(mtd, from, &ops);
Kyungmin Park12f77c92007-08-30 09:36:05 +09002107
2108 *retlen = ops.oobretlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03002109
2110 /* Exit OTP access mode */
2111 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2112 this->wait(mtd, FL_RESETING);
2113
2114 return ret;
2115}
2116
2117/**
2118 * onenand_otp_walk - [DEFAULT] Handle OTP operation
2119 * @param mtd MTD device structure
2120 * @param from The offset to read/write
2121 * @param len number of bytes to read/write
2122 * @param retlen pointer to variable to store the number of read bytes
2123 * @param buf the databuffer to put/get data
2124 * @param action do given action
2125 * @param mode specify user and factory
2126 *
2127 * Handle OTP operation.
2128 */
2129static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2130 size_t *retlen, u_char *buf,
2131 otp_op_t action, int mode)
2132{
2133 struct onenand_chip *this = mtd->priv;
2134 int otp_pages;
2135 int density;
2136 int ret = 0;
2137
2138 *retlen = 0;
2139
2140 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2141 if (density < ONENAND_DEVICE_DENSITY_512Mb)
2142 otp_pages = 20;
2143 else
2144 otp_pages = 10;
2145
2146 if (mode == MTD_OTP_FACTORY) {
Joern Engel28318772006-05-22 23:18:05 +02002147 from += mtd->writesize * otp_pages;
Kyungmin Park493c6462006-05-12 17:03:07 +03002148 otp_pages = 64 - otp_pages;
2149 }
2150
2151 /* Check User/Factory boundary */
Joern Engel28318772006-05-22 23:18:05 +02002152 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
Kyungmin Park493c6462006-05-12 17:03:07 +03002153 return 0;
2154
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002155 onenand_get_device(mtd, FL_OTPING);
Kyungmin Park493c6462006-05-12 17:03:07 +03002156 while (len > 0 && otp_pages > 0) {
2157 if (!action) { /* OTP Info functions */
2158 struct otp_info *otpinfo;
2159
2160 len -= sizeof(struct otp_info);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002161 if (len <= 0) {
2162 ret = -ENOSPC;
2163 break;
2164 }
Kyungmin Park493c6462006-05-12 17:03:07 +03002165
2166 otpinfo = (struct otp_info *) buf;
2167 otpinfo->start = from;
Joern Engel28318772006-05-22 23:18:05 +02002168 otpinfo->length = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03002169 otpinfo->locked = 0;
2170
Joern Engel28318772006-05-22 23:18:05 +02002171 from += mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03002172 buf += sizeof(struct otp_info);
2173 *retlen += sizeof(struct otp_info);
2174 } else {
2175 size_t tmp_retlen;
2176 int size = len;
2177
2178 ret = action(mtd, from, len, &tmp_retlen, buf);
2179
2180 buf += size;
2181 len -= size;
2182 *retlen += size;
2183
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002184 if (ret)
2185 break;
Kyungmin Park493c6462006-05-12 17:03:07 +03002186 }
2187 otp_pages--;
2188 }
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002189 onenand_release_device(mtd);
Kyungmin Park493c6462006-05-12 17:03:07 +03002190
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002191 return ret;
Kyungmin Park493c6462006-05-12 17:03:07 +03002192}
2193
2194/**
2195 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2196 * @param mtd MTD device structure
2197 * @param buf the databuffer to put/get data
2198 * @param len number of bytes to read
2199 *
2200 * Read factory OTP info.
2201 */
2202static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2203 struct otp_info *buf, size_t len)
2204{
2205 size_t retlen;
2206 int ret;
2207
2208 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2209
2210 return ret ? : retlen;
2211}
2212
2213/**
2214 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2215 * @param mtd MTD device structure
2216 * @param from The offset to read
2217 * @param len number of bytes to read
2218 * @param retlen pointer to variable to store the number of read bytes
2219 * @param buf the databuffer to put/get data
2220 *
2221 * Read factory OTP area.
2222 */
2223static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2224 size_t len, size_t *retlen, u_char *buf)
2225{
2226 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2227}
2228
2229/**
2230 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2231 * @param mtd MTD device structure
2232 * @param buf the databuffer to put/get data
2233 * @param len number of bytes to read
2234 *
2235 * Read user OTP info.
2236 */
2237static int onenand_get_user_prot_info(struct mtd_info *mtd,
2238 struct otp_info *buf, size_t len)
2239{
2240 size_t retlen;
2241 int ret;
2242
2243 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2244
2245 return ret ? : retlen;
2246}
2247
2248/**
2249 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2250 * @param mtd MTD device structure
2251 * @param from The offset to read
2252 * @param len number of bytes to read
2253 * @param retlen pointer to variable to store the number of read bytes
2254 * @param buf the databuffer to put/get data
2255 *
2256 * Read user OTP area.
2257 */
2258static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2259 size_t len, size_t *retlen, u_char *buf)
2260{
2261 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2262}
2263
2264/**
2265 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2266 * @param mtd MTD device structure
2267 * @param from The offset to write
2268 * @param len number of bytes to write
2269 * @param retlen pointer to variable to store the number of write bytes
2270 * @param buf the databuffer to put/get data
2271 *
2272 * Write user OTP area.
2273 */
2274static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2275 size_t len, size_t *retlen, u_char *buf)
2276{
2277 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2278}
2279
2280/**
2281 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2282 * @param mtd MTD device structure
2283 * @param from The offset to lock
2284 * @param len number of bytes to unlock
2285 *
2286 * Write lock mark on spare area in page 0 in OTP block
2287 */
2288static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2289 size_t len)
2290{
2291 unsigned char oob_buf[64];
2292 size_t retlen;
2293 int ret;
2294
2295 memset(oob_buf, 0xff, mtd->oobsize);
2296 /*
2297 * Note: OTP lock operation
2298 * OTP block : 0xXXFC
2299 * 1st block : 0xXXF3 (If chip support)
2300 * Both : 0xXXF0 (If chip support)
2301 */
2302 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2303
2304 /*
2305 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2306 * We write 16 bytes spare area instead of 2 bytes.
2307 */
2308 from = 0;
2309 len = 16;
2310
2311 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2312
2313 return ret ? : retlen;
2314}
2315#endif /* CONFIG_MTD_ONENAND_OTP */
2316
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002317/**
Kyungmin Park75384b02007-01-18 11:10:57 +09002318 * onenand_check_features - Check and set OneNAND features
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002319 * @param mtd MTD data structure
2320 *
Kyungmin Park75384b02007-01-18 11:10:57 +09002321 * Check and set OneNAND features
2322 * - lock scheme
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002323 * - two plane
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002324 */
Kyungmin Park75384b02007-01-18 11:10:57 +09002325static void onenand_check_features(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002326{
2327 struct onenand_chip *this = mtd->priv;
2328 unsigned int density, process;
2329
2330 /* Lock scheme depends on density and process */
2331 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2332 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2333
2334 /* Lock scheme */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002335 switch (density) {
2336 case ONENAND_DEVICE_DENSITY_4Gb:
2337 this->options |= ONENAND_HAS_2PLANE;
2338
2339 case ONENAND_DEVICE_DENSITY_2Gb:
2340 /* 2Gb DDP don't have 2 plane */
2341 if (!ONENAND_IS_DDP(this))
2342 this->options |= ONENAND_HAS_2PLANE;
2343 this->options |= ONENAND_HAS_UNLOCK_ALL;
2344
2345 case ONENAND_DEVICE_DENSITY_1Gb:
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002346 /* A-Die has all block unlock */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002347 if (process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002348 this->options |= ONENAND_HAS_UNLOCK_ALL;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002349 break;
2350
2351 default:
2352 /* Some OneNAND has continuous lock scheme */
2353 if (!process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002354 this->options |= ONENAND_HAS_CONT_LOCK;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002355 break;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002356 }
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002357
2358 if (this->options & ONENAND_HAS_CONT_LOCK)
2359 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2360 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2361 printk(KERN_DEBUG "Chip support all block unlock\n");
2362 if (this->options & ONENAND_HAS_2PLANE)
2363 printk(KERN_DEBUG "Chip has 2 plane\n");
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002364}
2365
2366/**
Kyungmin Parke3da8062007-02-15 09:36:39 +09002367 * onenand_print_device_info - Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002368 * @param device device ID
Kyungmin Parke3da8062007-02-15 09:36:39 +09002369 * @param version version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002370 *
Kyungmin Parke3da8062007-02-15 09:36:39 +09002371 * Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002372 */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002373static void onenand_print_device_info(int device, int version)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002374{
2375 int vcc, demuxed, ddp, density;
2376
2377 vcc = device & ONENAND_DEVICE_VCC_MASK;
2378 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2379 ddp = device & ONENAND_DEVICE_IS_DDP;
2380 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2381 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2382 demuxed ? "" : "Muxed ",
2383 ddp ? "(DDP)" : "",
2384 (16 << density),
2385 vcc ? "2.65/3.3" : "1.8",
2386 device);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002387 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002388}
2389
2390static const struct onenand_manufacturers onenand_manuf_ids[] = {
2391 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002392};
2393
2394/**
2395 * onenand_check_maf - Check manufacturer ID
2396 * @param manuf manufacturer ID
2397 *
2398 * Check manufacturer ID
2399 */
2400static int onenand_check_maf(int manuf)
2401{
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002402 int size = ARRAY_SIZE(onenand_manuf_ids);
2403 char *name;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002404 int i;
2405
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002406 for (i = 0; i < size; i++)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002407 if (manuf == onenand_manuf_ids[i].id)
2408 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002409
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002410 if (i < size)
2411 name = onenand_manuf_ids[i].name;
2412 else
2413 name = "Unknown";
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002414
Kyungmin Park37b1cc32005-12-16 11:17:29 +09002415 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2416
2417 return (i == size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002418}
2419
2420/**
2421 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2422 * @param mtd MTD device structure
2423 *
2424 * OneNAND detection method:
Michael Opdenacker59c51592007-05-09 08:57:56 +02002425 * Compare the values from command with ones from register
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002426 */
2427static int onenand_probe(struct mtd_info *mtd)
2428{
2429 struct onenand_chip *this = mtd->priv;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002430 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002431 int density;
Kyungmin Park47e777e2006-09-25 23:53:28 +00002432 int syscfg;
2433
2434 /* Save system configuration 1 */
2435 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2436 /* Clear Sync. Burst Read mode to read BootRAM */
2437 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002438
2439 /* Send the command for reading device ID from BootRAM */
2440 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2441
2442 /* Read manufacturer and device IDs from BootRAM */
2443 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2444 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2445
Kyungmin Park47e777e2006-09-25 23:53:28 +00002446 /* Reset OneNAND to read default register values */
2447 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2448 /* Wait reset */
2449 this->wait(mtd, FL_RESETING);
2450
2451 /* Restore system configuration 1 */
2452 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2453
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002454 /* Check manufacturer ID */
2455 if (onenand_check_maf(bram_maf_id))
2456 return -ENXIO;
2457
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002458 /* Read manufacturer and device IDs from Register */
2459 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2460 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09002461 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002462
2463 /* Check OneNAND device */
2464 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2465 return -ENXIO;
2466
2467 /* Flash device information */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002468 onenand_print_device_info(dev_id, ver_id);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002469 this->device_id = dev_id;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002470 this->version_id = ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002471
2472 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2473 this->chipsize = (16 << density) << 20;
Kyungmin Park83a36832005-09-29 04:53:16 +01002474 /* Set density mask. it is used for DDP */
Kyungmin Park738d61f2007-01-15 17:09:14 +09002475 if (ONENAND_IS_DDP(this))
2476 this->density_mask = (1 << (density + 6));
2477 else
2478 this->density_mask = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002479
2480 /* OneNAND page size & block size */
2481 /* The data buffer size is equal to page size */
Joern Engel28318772006-05-22 23:18:05 +02002482 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2483 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09002484 /* Pages per a block are always 64 in OneNAND */
Joern Engel28318772006-05-22 23:18:05 +02002485 mtd->erasesize = mtd->writesize << 6;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002486
2487 this->erase_shift = ffs(mtd->erasesize) - 1;
Joern Engel28318772006-05-22 23:18:05 +02002488 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09002489 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002490 /* It's real page size */
2491 this->writesize = mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002492
2493 /* REVIST: Multichip handling */
2494
2495 mtd->size = this->chipsize;
2496
Kyungmin Park75384b02007-01-18 11:10:57 +09002497 /* Check OneNAND features */
2498 onenand_check_features(mtd);
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002499
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002500 /*
2501 * We emulate the 4KiB page and 256KiB erase block size
2502 * But oobsize is still 64 bytes.
2503 * It is only valid if you turn on 2X program support,
2504 * Otherwise it will be ignored by compiler.
2505 */
2506 if (ONENAND_IS_2PLANE(this)) {
2507 mtd->writesize <<= 1;
2508 mtd->erasesize <<= 1;
2509 }
2510
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002511 return 0;
2512}
2513
Kyungmin Parka41371e2005-09-29 03:55:31 +01002514/**
2515 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2516 * @param mtd MTD device structure
2517 */
2518static int onenand_suspend(struct mtd_info *mtd)
2519{
2520 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2521}
2522
2523/**
2524 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2525 * @param mtd MTD device structure
2526 */
2527static void onenand_resume(struct mtd_info *mtd)
2528{
2529 struct onenand_chip *this = mtd->priv;
2530
2531 if (this->state == FL_PM_SUSPENDED)
2532 onenand_release_device(mtd);
2533 else
2534 printk(KERN_ERR "resume() called for the chip which is not"
2535 "in suspended state\n");
2536}
2537
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002538/**
2539 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2540 * @param mtd MTD device structure
2541 * @param maxchips Number of chips to scan for
2542 *
2543 * This fills out all the not initialized function pointers
2544 * with the defaults.
2545 * The flash ID is read and the mtd/chip structures are
2546 * filled with the appropriate values.
2547 */
2548int onenand_scan(struct mtd_info *mtd, int maxchips)
2549{
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002550 int i;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002551 struct onenand_chip *this = mtd->priv;
2552
2553 if (!this->read_word)
2554 this->read_word = onenand_readw;
2555 if (!this->write_word)
2556 this->write_word = onenand_writew;
2557
2558 if (!this->command)
2559 this->command = onenand_command;
2560 if (!this->wait)
Kyungmin Park2c221202006-11-16 11:23:48 +09002561 onenand_setup_wait(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002562
2563 if (!this->read_bufferram)
2564 this->read_bufferram = onenand_read_bufferram;
2565 if (!this->write_bufferram)
2566 this->write_bufferram = onenand_write_bufferram;
2567
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002568 if (!this->block_markbad)
2569 this->block_markbad = onenand_default_block_markbad;
2570 if (!this->scan_bbt)
2571 this->scan_bbt = onenand_default_bbt;
2572
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002573 if (onenand_probe(mtd))
2574 return -ENXIO;
2575
Kyungmin Park52b0eea2005-09-03 07:07:19 +01002576 /* Set Sync. Burst Read after probing */
2577 if (this->mmcontrol) {
2578 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2579 this->read_bufferram = onenand_sync_read_bufferram;
2580 }
2581
Kyungmin Park532a37c2005-12-16 11:17:29 +09002582 /* Allocate buffers, if necessary */
2583 if (!this->page_buf) {
Kyungmin Park470bc842007-03-09 10:08:11 +09002584 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
Kyungmin Park532a37c2005-12-16 11:17:29 +09002585 if (!this->page_buf) {
2586 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2587 return -ENOMEM;
2588 }
2589 this->options |= ONENAND_PAGEBUF_ALLOC;
2590 }
Kyungmin Park470bc842007-03-09 10:08:11 +09002591 if (!this->oob_buf) {
2592 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2593 if (!this->oob_buf) {
2594 printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2595 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2596 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2597 kfree(this->page_buf);
2598 }
2599 return -ENOMEM;
2600 }
2601 this->options |= ONENAND_OOBBUF_ALLOC;
2602 }
Kyungmin Park532a37c2005-12-16 11:17:29 +09002603
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002604 this->state = FL_READY;
2605 init_waitqueue_head(&this->wq);
2606 spin_lock_init(&this->chip_lock);
2607
Kyungmin Park60d84f92006-12-22 16:21:54 +09002608 /*
2609 * Allow subpage writes up to oobsize.
2610 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002611 switch (mtd->oobsize) {
2612 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002613 this->ecclayout = &onenand_oob_64;
Kyungmin Park60d84f92006-12-22 16:21:54 +09002614 mtd->subpage_sft = 2;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002615 break;
2616
2617 case 32:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002618 this->ecclayout = &onenand_oob_32;
Kyungmin Park60d84f92006-12-22 16:21:54 +09002619 mtd->subpage_sft = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002620 break;
2621
2622 default:
2623 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2624 mtd->oobsize);
Kyungmin Park60d84f92006-12-22 16:21:54 +09002625 mtd->subpage_sft = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002626 /* To prevent kernel oops */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002627 this->ecclayout = &onenand_oob_32;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002628 break;
2629 }
2630
Kyungmin Park60d84f92006-12-22 16:21:54 +09002631 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002632
2633 /*
2634 * The number of bytes available for a client to place data into
2635 * the out of band area
2636 */
2637 this->ecclayout->oobavail = 0;
Kyungmin Parkad286342007-03-23 10:19:52 +09002638 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2639 this->ecclayout->oobfree[i].length; i++)
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002640 this->ecclayout->oobavail +=
2641 this->ecclayout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03002642 mtd->oobavail = this->ecclayout->oobavail;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002643
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002644 mtd->ecclayout = this->ecclayout;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002645
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002646 /* Fill in remaining MTD driver data */
2647 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02002648 mtd->flags = MTD_CAP_NANDFLASH;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002649 mtd->erase = onenand_erase;
2650 mtd->point = NULL;
2651 mtd->unpoint = NULL;
2652 mtd->read = onenand_read;
2653 mtd->write = onenand_write;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002654 mtd->read_oob = onenand_read_oob;
2655 mtd->write_oob = onenand_write_oob;
Kyungmin Park493c6462006-05-12 17:03:07 +03002656#ifdef CONFIG_MTD_ONENAND_OTP
2657 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2658 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2659 mtd->get_user_prot_info = onenand_get_user_prot_info;
2660 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2661 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2662 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2663#endif
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002664 mtd->sync = onenand_sync;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002665 mtd->lock = onenand_lock;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002666 mtd->unlock = onenand_unlock;
Kyungmin Parka41371e2005-09-29 03:55:31 +01002667 mtd->suspend = onenand_suspend;
2668 mtd->resume = onenand_resume;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002669 mtd->block_isbad = onenand_block_isbad;
2670 mtd->block_markbad = onenand_block_markbad;
2671 mtd->owner = THIS_MODULE;
2672
2673 /* Unlock whole block */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002674 onenand_unlock_all(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002675
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002676 return this->scan_bbt(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002677}
2678
2679/**
2680 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2681 * @param mtd MTD device structure
2682 */
2683void onenand_release(struct mtd_info *mtd)
2684{
Kyungmin Park532a37c2005-12-16 11:17:29 +09002685 struct onenand_chip *this = mtd->priv;
2686
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002687#ifdef CONFIG_MTD_PARTITIONS
2688 /* Deregister partitions */
2689 del_mtd_partitions (mtd);
2690#endif
2691 /* Deregister the device */
2692 del_mtd_device (mtd);
Kyungmin Park532a37c2005-12-16 11:17:29 +09002693
2694 /* Free bad block table memory, if allocated */
Adrian Hunterf00b0042007-01-22 17:01:01 +09002695 if (this->bbm) {
2696 struct bbm_info *bbm = this->bbm;
2697 kfree(bbm->bbt);
Kyungmin Park532a37c2005-12-16 11:17:29 +09002698 kfree(this->bbm);
Adrian Hunterf00b0042007-01-22 17:01:01 +09002699 }
Kyungmin Park470bc842007-03-09 10:08:11 +09002700 /* Buffers allocated by onenand_scan */
Kyungmin Park532a37c2005-12-16 11:17:29 +09002701 if (this->options & ONENAND_PAGEBUF_ALLOC)
2702 kfree(this->page_buf);
Kyungmin Park470bc842007-03-09 10:08:11 +09002703 if (this->options & ONENAND_OOBBUF_ALLOC)
2704 kfree(this->oob_buf);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002705}
2706
2707EXPORT_SYMBOL_GPL(onenand_scan);
2708EXPORT_SYMBOL_GPL(onenand_release);
2709
2710MODULE_LICENSE("GPL");
2711MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2712MODULE_DESCRIPTION("Generic OneNAND flash driver code");