blob: 979e4af7c5fc73df42165fdb4b86034fb6cdc8c2 [file] [log] [blame]
Kyungmin Park79e90462007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053012 * Rohit Hagargundgi <h.rohit at samsung.com>,
13 * Amul Kumar Saha <amul.saha@samsung.com>:
14 * Flex-OneNAND support
15 * Copyright (C) Samsung Electronics, 2009
16 *
Kyungmin Park79e90462007-09-10 17:13:49 +090017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <common.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000023#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090024#include <linux/mtd/mtd.h>
25#include <linux/mtd/onenand.h>
26
27#include <asm/io.h>
28#include <asm/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020029#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090030
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090031/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053032static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090033{
34 void *ret = dst;
35 short *d = dst;
36 const short *s = src;
37
38 len >>= 1;
39 while (len-- > 0)
40 *d++ = *s++;
41 return ret;
42}
43
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010044/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053045 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
46 * For now, we expose only 64 out of 80 ecc bytes
47 */
48static struct nand_ecclayout onenand_oob_128 = {
49 .eccbytes = 64,
50 .eccpos = {
51 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
52 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
53 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
54 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
55 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
56 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
57 102, 103, 104, 105
58 },
59 .oobfree = {
60 {2, 4}, {18, 4}, {34, 4}, {50, 4},
61 {66, 4}, {82, 4}, {98, 4}, {114, 4}
62 }
63};
64
65/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010066 * onenand_oob_64 - oob info for large (2KB) page
67 */
68static struct nand_ecclayout onenand_oob_64 = {
69 .eccbytes = 20,
70 .eccpos = {
71 8, 9, 10, 11, 12,
72 24, 25, 26, 27, 28,
73 40, 41, 42, 43, 44,
74 56, 57, 58, 59, 60,
75 },
76 .oobfree = {
77 {2, 3}, {14, 2}, {18, 3}, {30, 2},
78 {34, 3}, {46, 2}, {50, 3}, {62, 2}
79 }
80};
81
82/**
83 * onenand_oob_32 - oob info for middle (1KB) page
84 */
85static struct nand_ecclayout onenand_oob_32 = {
86 .eccbytes = 10,
87 .eccpos = {
88 8, 9, 10, 11, 12,
89 24, 25, 26, 27, 28,
90 },
91 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
92};
93
Kyungmin Park79e90462007-09-10 17:13:49 +090094static const unsigned char ffchars[] = {
95 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900111};
112
113/**
114 * onenand_readw - [OneNAND Interface] Read OneNAND register
115 * @param addr address to read
116 *
117 * Read OneNAND register
118 */
119static unsigned short onenand_readw(void __iomem * addr)
120{
121 return readw(addr);
122}
123
124/**
125 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
126 * @param value value to write
127 * @param addr address to write
128 *
129 * Write OneNAND register with value
130 */
131static void onenand_writew(unsigned short value, void __iomem * addr)
132{
133 writew(value, addr);
134}
135
136/**
137 * onenand_block_address - [DEFAULT] Get block address
138 * @param device the device id
139 * @param block the block
140 * @return translated block address if DDP, otherwise same
141 *
142 * Setup Start Address 1 Register (F100h)
143 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900144static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900145{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900146 /* Device Flash Core select, NAND Flash Block Address */
147 if (block & this->density_mask)
148 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900149
150 return block;
151}
152
153/**
154 * onenand_bufferram_address - [DEFAULT] Get bufferram address
155 * @param device the device id
156 * @param block the block
157 * @return set DBS value if DDP, otherwise 0
158 *
159 * Setup Start Address 2 Register (F101h) for DDP
160 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900161static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900162{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900163 /* Device BufferRAM Select */
164 if (block & this->density_mask)
165 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900166
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900167 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900168}
169
170/**
171 * onenand_page_address - [DEFAULT] Get page address
172 * @param page the page address
173 * @param sector the sector address
174 * @return combined page and sector address
175 *
176 * Setup Start Address 8 Register (F107h)
177 */
178static int onenand_page_address(int page, int sector)
179{
180 /* Flash Page Address, Flash Sector Address */
181 int fpa, fsa;
182
183 fpa = page & ONENAND_FPA_MASK;
184 fsa = sector & ONENAND_FSA_MASK;
185
186 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
187}
188
189/**
190 * onenand_buffer_address - [DEFAULT] Get buffer address
191 * @param dataram1 DataRAM index
192 * @param sectors the sector address
193 * @param count the number of sectors
194 * @return the start buffer value
195 *
196 * Setup Start Buffer Register (F200h)
197 */
198static int onenand_buffer_address(int dataram1, int sectors, int count)
199{
200 int bsa, bsc;
201
202 /* BufferRAM Sector Address */
203 bsa = sectors & ONENAND_BSA_MASK;
204
205 if (dataram1)
206 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
207 else
208 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
209
210 /* BufferRAM Sector Count */
211 bsc = count & ONENAND_BSC_MASK;
212
213 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
214}
215
216/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530217 * flexonenand_block - Return block number for flash address
218 * @param this - OneNAND device structure
219 * @param addr - Address for which block number is needed
220 */
221static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
222{
223 unsigned int boundary, blk, die = 0;
224
225 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
226 die = 1;
227 addr -= this->diesize[0];
228 }
229
230 boundary = this->boundary[die];
231
232 blk = addr >> (this->erase_shift - 1);
233 if (blk > boundary)
234 blk = (blk + boundary + 1) >> 1;
235
236 blk += die ? this->density_mask : 0;
237 return blk;
238}
239
240unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
241{
242 if (!FLEXONENAND(this))
243 return addr >> this->erase_shift;
244 return flexonenand_block(this, addr);
245}
246
247/**
248 * flexonenand_addr - Return address of the block
249 * @this: OneNAND device structure
250 * @block: Block number on Flex-OneNAND
251 *
252 * Return address of the block
253 */
254static loff_t flexonenand_addr(struct onenand_chip *this, int block)
255{
256 loff_t ofs = 0;
257 int die = 0, boundary;
258
259 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
260 block -= this->density_mask;
261 die = 1;
262 ofs = this->diesize[0];
263 }
264
265 boundary = this->boundary[die];
266 ofs += (loff_t) block << (this->erase_shift - 1);
267 if (block > (boundary + 1))
268 ofs += (loff_t) (block - boundary - 1)
269 << (this->erase_shift - 1);
270 return ofs;
271}
272
273loff_t onenand_addr(struct onenand_chip *this, int block)
274{
275 if (!FLEXONENAND(this))
276 return (loff_t) block << this->erase_shift;
277 return flexonenand_addr(this, block);
278}
279
280/**
281 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
282 * @param mtd MTD device structure
283 * @param addr address whose erase region needs to be identified
284 */
285int flexonenand_region(struct mtd_info *mtd, loff_t addr)
286{
287 int i;
288
289 for (i = 0; i < mtd->numeraseregions; i++)
290 if (addr < mtd->eraseregions[i].offset)
291 break;
292 return i - 1;
293}
294
295/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900296 * onenand_get_density - [DEFAULT] Get OneNAND density
297 * @param dev_id OneNAND device ID
298 *
299 * Get OneNAND density from device ID
300 */
301static inline int onenand_get_density(int dev_id)
302{
303 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
304 return (density & ONENAND_DEVICE_DENSITY_MASK);
305}
306
307/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900308 * onenand_command - [DEFAULT] Send command to OneNAND device
309 * @param mtd MTD device structure
310 * @param cmd the command to be sent
311 * @param addr offset to read from or write to
312 * @param len number of bytes to read or write
313 *
314 * Send command to OneNAND device. This function is used for middle/large page
315 * devices (1KB/2KB Bytes per page)
316 */
317static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
318 size_t len)
319{
320 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530321 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900322 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530323
Kyungmin Park79e90462007-09-10 17:13:49 +0900324 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530325 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900326
327 /* Address translation */
328 switch (cmd) {
329 case ONENAND_CMD_UNLOCK:
330 case ONENAND_CMD_LOCK:
331 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900332 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900333 block = -1;
334 page = -1;
335 break;
336
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530337 case FLEXONENAND_CMD_PI_ACCESS:
338 /* addr contains die index */
339 block = addr * this->density_mask;
340 page = -1;
341 break;
342
Kyungmin Park79e90462007-09-10 17:13:49 +0900343 case ONENAND_CMD_ERASE:
344 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530345 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900346 page = -1;
347 break;
348
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530349 case FLEXONENAND_CMD_READ_PI:
350 cmd = ONENAND_CMD_READ;
351 block = addr * this->density_mask;
352 page = 0;
353 break;
354
Kyungmin Park79e90462007-09-10 17:13:49 +0900355 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530356 block = onenand_block(this, addr);
357 page = (int) (addr
358 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900359 page &= this->page_mask;
360 break;
361 }
362
363 /* NOTE: The setting order of the registers is very important! */
364 if (cmd == ONENAND_CMD_BUFFERRAM) {
365 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900366 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900367 this->write_word(value,
368 this->base + ONENAND_REG_START_ADDRESS2);
369
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100370 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530371 ONENAND_SET_BUFFERRAM0(this);
372 else
373 /* Switch to the next data buffer */
374 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900375
376 return 0;
377 }
378
379 if (block != -1) {
380 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900381 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900382 this->write_word(value,
383 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900384
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530385 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900386 value = onenand_bufferram_address(this, block);
387 this->write_word(value,
388 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900389 }
390
391 if (page != -1) {
392 int dataram;
393
394 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530395 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900396 case ONENAND_CMD_READ:
397 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100398 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530399 dataram = ONENAND_SET_BUFFERRAM0(this);
400 else
401 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
402
Kyungmin Park79e90462007-09-10 17:13:49 +0900403 break;
404
405 default:
406 dataram = ONENAND_CURRENT_BUFFERRAM(this);
407 break;
408 }
409
410 /* Write 'FPA, FSA' of Flash */
411 value = onenand_page_address(page, sectors);
412 this->write_word(value,
413 this->base + ONENAND_REG_START_ADDRESS8);
414
415 /* Write 'BSA, BSC' of DataRAM */
416 value = onenand_buffer_address(dataram, sectors, count);
417 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900418 }
419
420 /* Interrupt clear */
421 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
422 /* Write command */
423 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
424
425 return 0;
426}
427
428/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530429 * onenand_read_ecc - return ecc status
430 * @param this onenand chip structure
431 */
432static int onenand_read_ecc(struct onenand_chip *this)
433{
434 int ecc, i;
435
436 if (!FLEXONENAND(this))
437 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
438
439 for (i = 0; i < 4; i++) {
440 ecc = this->read_word(this->base
441 + ((ONENAND_REG_ECC_STATUS + i) << 1));
442 if (likely(!ecc))
443 continue;
444 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
445 return ONENAND_ECC_2BIT_ALL;
446 }
447
448 return 0;
449}
450
451/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900452 * onenand_wait - [DEFAULT] wait until the command is done
453 * @param mtd MTD device structure
454 * @param state state to select the max. timeout value
455 *
456 * Wait for command done. This applies to all OneNAND command
457 * Read can take up to 30us, erase up to 2ms and program up to 350us
458 * according to general OneNAND specs
459 */
460static int onenand_wait(struct mtd_info *mtd, int state)
461{
462 struct onenand_chip *this = mtd->priv;
463 unsigned int flags = ONENAND_INT_MASTER;
464 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530465 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900466
467 while (1) {
468 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
469 if (interrupt & flags)
470 break;
471 }
472
473 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
474
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530475 if (interrupt & ONENAND_INT_READ) {
476 int ecc = onenand_read_ecc(this);
477 if (ecc & ONENAND_ECC_2BIT_ALL) {
478 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
479 return -EBADMSG;
480 }
481 }
482
Kyungmin Park79e90462007-09-10 17:13:49 +0900483 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900484 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
485 if (ctrl & ONENAND_CTRL_LOCK)
486 printk("onenand_wait: it's locked error = 0x%04x\n",
487 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900488
Kyungmin Park79e90462007-09-10 17:13:49 +0900489 return -EIO;
490 }
491
Kyungmin Park79e90462007-09-10 17:13:49 +0900492
493 return 0;
494}
495
496/**
497 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
498 * @param mtd MTD data structure
499 * @param area BufferRAM area
500 * @return offset given area
501 *
502 * Return BufferRAM offset given area
503 */
504static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
505{
506 struct onenand_chip *this = mtd->priv;
507
508 if (ONENAND_CURRENT_BUFFERRAM(this)) {
509 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900510 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900511 if (area == ONENAND_SPARERAM)
512 return mtd->oobsize;
513 }
514
515 return 0;
516}
517
518/**
519 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
520 * @param mtd MTD data structure
521 * @param area BufferRAM area
522 * @param buffer the databuffer to put/get data
523 * @param offset offset to read from or write to
524 * @param count number of bytes to read/write
525 *
526 * Read the BufferRAM area
527 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900528static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900529 unsigned char *buffer, int offset,
530 size_t count)
531{
532 struct onenand_chip *this = mtd->priv;
533 void __iomem *bufferram;
534
535 bufferram = this->base + area;
536 bufferram += onenand_bufferram_offset(mtd, area);
537
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200538 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900539
540 return 0;
541}
542
543/**
544 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
545 * @param mtd MTD data structure
546 * @param area BufferRAM area
547 * @param buffer the databuffer to put/get data
548 * @param offset offset to read from or write to
549 * @param count number of bytes to read/write
550 *
551 * Read the BufferRAM area with Sync. Burst Mode
552 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900553static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900554 unsigned char *buffer, int offset,
555 size_t count)
556{
557 struct onenand_chip *this = mtd->priv;
558 void __iomem *bufferram;
559
560 bufferram = this->base + area;
561 bufferram += onenand_bufferram_offset(mtd, area);
562
563 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
564
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200565 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900566
567 this->mmcontrol(mtd, 0);
568
569 return 0;
570}
571
572/**
573 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
574 * @param mtd MTD data structure
575 * @param area BufferRAM area
576 * @param buffer the databuffer to put/get data
577 * @param offset offset to read from or write to
578 * @param count number of bytes to read/write
579 *
580 * Write the BufferRAM area
581 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900582static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900583 const unsigned char *buffer, int offset,
584 size_t count)
585{
586 struct onenand_chip *this = mtd->priv;
587 void __iomem *bufferram;
588
589 bufferram = this->base + area;
590 bufferram += onenand_bufferram_offset(mtd, area);
591
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200592 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900593
594 return 0;
595}
596
597/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100598 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
599 * @param mtd MTD data structure
600 * @param addr address to check
601 * @return blockpage address
602 *
603 * Get blockpage address at 2x program mode
604 */
605static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
606{
607 struct onenand_chip *this = mtd->priv;
608 int blockpage, block, page;
609
610 /* Calculate the even block number */
611 block = (int) (addr >> this->erase_shift) & ~1;
612 /* Is it the odd plane? */
613 if (addr & this->writesize)
614 block++;
615 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
616 blockpage = (block << 7) | page;
617
618 return blockpage;
619}
620
621/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900622 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
623 * @param mtd MTD data structure
624 * @param addr address to check
625 * @return 1 if there are valid data, otherwise 0
626 *
627 * Check bufferram if there is data we required
628 */
629static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
630{
631 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900632 int blockpage, found = 0;
633 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900634
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900635 if (ONENAND_IS_2PLANE(this))
636 blockpage = onenand_get_2x_blockpage(mtd, addr);
637 else
638 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900639
640 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900641 i = ONENAND_CURRENT_BUFFERRAM(this);
642 if (this->bufferram[i].blockpage == blockpage)
643 found = 1;
644 else {
645 /* Check another BufferRAM */
646 i = ONENAND_NEXT_BUFFERRAM(this);
647 if (this->bufferram[i].blockpage == blockpage) {
648 ONENAND_SET_NEXT_BUFFERRAM(this);
649 found = 1;
650 }
651 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900652
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900653 if (found && ONENAND_IS_DDP(this)) {
654 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530655 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900656 int value = onenand_bufferram_address(this, block);
657 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
658 }
659
660 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900661}
662
663/**
664 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
665 * @param mtd MTD data structure
666 * @param addr address to update
667 * @param valid valid flag
668 *
669 * Update BufferRAM information
670 */
671static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
672 int valid)
673{
674 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900675 int blockpage;
676 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900677
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900678 if (ONENAND_IS_2PLANE(this))
679 blockpage = onenand_get_2x_blockpage(mtd, addr);
680 else
681 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900682
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900683 /* Invalidate another BufferRAM */
684 i = ONENAND_NEXT_BUFFERRAM(this);
685 if (this->bufferram[i].blockpage == blockpage)
686 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900687
688 /* Update BufferRAM */
689 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900690 if (valid)
691 this->bufferram[i].blockpage = blockpage;
692 else
693 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900694
695 return 0;
696}
697
698/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900699 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
700 * @param mtd MTD data structure
701 * @param addr start address to invalidate
702 * @param len length to invalidate
703 *
704 * Invalidate BufferRAM information
705 */
706static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200707 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900708{
709 struct onenand_chip *this = mtd->priv;
710 int i;
711 loff_t end_addr = addr + len;
712
713 /* Invalidate BufferRAM */
714 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900715 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900716
717 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900718 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900719 }
720}
721
722/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900723 * onenand_get_device - [GENERIC] Get chip for selected access
724 * @param mtd MTD device structure
725 * @param new_state the state which is requested
726 *
727 * Get the device and lock it for exclusive access
728 */
729static void onenand_get_device(struct mtd_info *mtd, int new_state)
730{
731 /* Do nothing */
732}
733
734/**
735 * onenand_release_device - [GENERIC] release chip
736 * @param mtd MTD device structure
737 *
738 * Deselect, release chip lock and wake up anyone waiting on the device
739 */
740static void onenand_release_device(struct mtd_info *mtd)
741{
742 /* Do nothing */
743}
744
745/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000746 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900747 * @param mtd MTD device structure
748 * @param buf destination address
749 * @param column oob offset to read from
750 * @param thislen oob length to read
751 */
752static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
753 int column, int thislen)
754{
755 struct onenand_chip *this = mtd->priv;
756 struct nand_oobfree *free;
757 int readcol = column;
758 int readend = column + thislen;
759 int lastgap = 0;
760 unsigned int i;
761 uint8_t *oob_buf = this->oob_buf;
762
763 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530764 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
765 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900766 if (readcol >= lastgap)
767 readcol += free->offset - lastgap;
768 if (readend >= lastgap)
769 readend += free->offset - lastgap;
770 lastgap = free->offset + free->length;
771 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900772 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900773 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530774 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
775 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900776 int free_end = free->offset + free->length;
777 if (free->offset < readend && free_end > readcol) {
778 int st = max_t(int,free->offset,readcol);
779 int ed = min_t(int,free_end,readend);
780 int n = ed - st;
781 memcpy(buf, oob_buf + st, n);
782 buf += n;
783 } else if (column == 0)
784 break;
785 }
786 return 0;
787}
788
789/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530790 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
791 * @param mtd MTD device structure
792 * @param addr address to recover
793 * @param status return value from onenand_wait
794 *
795 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
796 * lower page address and MSB page has higher page address in paired pages.
797 * If power off occurs during MSB page program, the paired LSB page data can
798 * become corrupt. LSB page recovery read is a way to read LSB page though page
799 * data are corrupted. When uncorrectable error occurs as a result of LSB page
800 * read after power up, issue LSB page recovery read.
801 */
802static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
803{
804 struct onenand_chip *this = mtd->priv;
805 int i;
806
807 /* Recovery is only for Flex-OneNAND */
808 if (!FLEXONENAND(this))
809 return status;
810
811 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000812 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530813 return status;
814
815 /* check if address lies in MLC region */
816 i = flexonenand_region(mtd, addr);
817 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
818 return status;
819
820 printk("onenand_recover_lsb:"
821 "Attempting to recover from uncorrectable read\n");
822
823 /* Issue the LSB page recovery command */
824 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
825 return this->wait(mtd, FL_READING);
826}
827
828/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900829 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900830 * @param mtd MTD device structure
831 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900832 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900833 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900834 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900835 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900836static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
837 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900838{
839 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900840 struct mtd_ecc_stats stats;
841 size_t len = ops->len;
842 size_t ooblen = ops->ooblen;
843 u_char *buf = ops->datbuf;
844 u_char *oobbuf = ops->oobbuf;
845 int read = 0, column, thislen;
846 int oobread = 0, oobcolumn, thisooblen, oobsize;
847 int ret = 0, boundary = 0;
848 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900849
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900850 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900851
Sergey Lapin3a38a552013-01-14 03:46:50 +0000852 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900853 oobsize = this->ecclayout->oobavail;
854 else
855 oobsize = mtd->oobsize;
856
857 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900858
859 /* Do not allow reads past end of device */
860 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900861 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
862 ops->retlen = 0;
863 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900864 return -EINVAL;
865 }
866
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900867 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900868
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900869 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530870 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900871
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900872 /* Do first load to bufferRAM */
873 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900874 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900875 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900876 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900877 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530878 if (unlikely(ret))
879 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900880 onenand_update_bufferram(mtd, from, !ret);
881 if (ret == -EBADMSG)
882 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900883 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900884 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900885
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900886 thislen = min_t(int, writesize, len - read);
887 column = from & (writesize - 1);
888 if (column + thislen > writesize)
889 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900890
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900891 while (!ret) {
892 /* If there is more to load then start next load */
893 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100894 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900895 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900896 this->command(mtd, ONENAND_CMD_READ, from, writesize);
897 /*
898 * Chip boundary handling in DDP
899 * Now we issued chip 1 read and pointed chip 1
900 * bufferam so we have to point chip 0 bufferam.
901 */
902 if (ONENAND_IS_DDP(this) &&
903 unlikely(from == (this->chipsize >> 1))) {
904 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
905 boundary = 1;
906 } else
907 boundary = 0;
908 ONENAND_SET_PREV_BUFFERRAM(this);
909 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900910
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900911 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900912 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900913
914 /* Read oob area if needed */
915 if (oobbuf) {
916 thisooblen = oobsize - oobcolumn;
917 thisooblen = min_t(int, thisooblen, ooblen - oobread);
918
Sergey Lapin3a38a552013-01-14 03:46:50 +0000919 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900920 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
921 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900922 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900923 oobread += thisooblen;
924 oobbuf += thisooblen;
925 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900926 }
927
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100928 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530929 this->command(mtd, ONENAND_CMD_READ, from, writesize);
930 ret = this->wait(mtd, FL_READING);
931 if (unlikely(ret))
932 ret = onenand_recover_lsb(mtd, from, ret);
933 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000934 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530935 ret = 0;
936 }
937
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900938 /* See if we are done */
939 read += thislen;
940 if (read == len)
941 break;
942 /* Set up for next read from bufferRAM */
943 if (unlikely(boundary))
944 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100945 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530946 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900947 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900948 thislen = min_t(int, writesize, len - read);
949 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900950
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100951 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530952 /* Now wait for load */
953 ret = this->wait(mtd, FL_READING);
954 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000955 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530956 ret = 0;
957 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900958 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900959
960 /*
961 * Return success, if no ECC failures, else -EBADMSG
962 * fs driver will take care of that, because
963 * retlen == desired len and result == -EBADMSG
964 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900965 ops->retlen = read;
966 ops->oobretlen = oobread;
967
968 if (ret)
969 return ret;
970
971 if (mtd->ecc_stats.failed - stats.failed)
972 return -EBADMSG;
973
Paul Burton700a76c2013-09-04 15:16:56 +0100974 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
975 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900976}
977
978/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900979 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
980 * @param mtd MTD device structure
981 * @param from offset to read from
982 * @param ops oob operation description structure
983 *
984 * OneNAND read out-of-band data from the spare area
985 */
986static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
987 struct mtd_oob_ops *ops)
988{
989 struct onenand_chip *this = mtd->priv;
990 struct mtd_ecc_stats stats;
991 int read = 0, thislen, column, oobsize;
992 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000993 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900994 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530995 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900996
997 from += ops->ooboffs;
998
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900999 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001000
1001 /* Initialize return length value */
1002 ops->oobretlen = 0;
1003
Sergey Lapin3a38a552013-01-14 03:46:50 +00001004 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001005 oobsize = this->ecclayout->oobavail;
1006 else
1007 oobsize = mtd->oobsize;
1008
1009 column = from & (mtd->oobsize - 1);
1010
1011 if (unlikely(column >= oobsize)) {
1012 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1013 return -EINVAL;
1014 }
1015
1016 /* Do not allow reads past end of device */
1017 if (unlikely(from >= mtd->size ||
1018 column + len > ((mtd->size >> this->page_shift) -
1019 (from >> this->page_shift)) * oobsize)) {
1020 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1021 return -EINVAL;
1022 }
1023
1024 stats = mtd->ecc_stats;
1025
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001026 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1027 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301028
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001029 while (read < len) {
1030 thislen = oobsize - column;
1031 thislen = min_t(int, thislen, len);
1032
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001033 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301034 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001035
1036 onenand_update_bufferram(mtd, from, 0);
1037
1038 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301039 if (unlikely(ret))
1040 ret = onenand_recover_lsb(mtd, from, ret);
1041
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001042 if (ret && ret != -EBADMSG) {
1043 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1044 break;
1045 }
1046
Sergey Lapin3a38a552013-01-14 03:46:50 +00001047 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001048 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1049 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001050 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001051
1052 read += thislen;
1053
1054 if (read == len)
1055 break;
1056
1057 buf += thislen;
1058
1059 /* Read more? */
1060 if (read < len) {
1061 /* Page size */
1062 from += mtd->writesize;
1063 column = 0;
1064 }
1065 }
1066
1067 ops->oobretlen = read;
1068
1069 if (ret)
1070 return ret;
1071
1072 if (mtd->ecc_stats.failed - stats.failed)
1073 return -EBADMSG;
1074
1075 return 0;
1076}
1077
1078/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001079 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1080 * @param mtd MTD device structure
1081 * @param from offset to read from
1082 * @param len number of bytes to read
1083 * @param retlen pointer to variable to store the number of read bytes
1084 * @param buf the databuffer to put data
1085 *
1086 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1087*/
1088int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1089 size_t * retlen, u_char * buf)
1090{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001091 struct mtd_oob_ops ops = {
1092 .len = len,
1093 .ooblen = 0,
1094 .datbuf = buf,
1095 .oobbuf = NULL,
1096 };
1097 int ret;
1098
1099 onenand_get_device(mtd, FL_READING);
1100 ret = onenand_read_ops_nolock(mtd, from, &ops);
1101 onenand_release_device(mtd);
1102
1103 *retlen = ops.retlen;
1104 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001105}
1106
1107/**
1108 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1109 * @param mtd MTD device structure
1110 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001111 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001112 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001113 * OneNAND main and/or out-of-band
1114 */
1115int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1116 struct mtd_oob_ops *ops)
1117{
1118 int ret;
1119
1120 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001121 case MTD_OPS_PLACE_OOB:
1122 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001123 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001124 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001125 /* Not implemented yet */
1126 default:
1127 return -EINVAL;
1128 }
1129
1130 onenand_get_device(mtd, FL_READING);
1131 if (ops->datbuf)
1132 ret = onenand_read_ops_nolock(mtd, from, ops);
1133 else
1134 ret = onenand_read_oob_nolock(mtd, from, ops);
1135 onenand_release_device(mtd);
1136
1137 return ret;
1138}
1139
1140/**
1141 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1142 * @param mtd MTD device structure
1143 * @param state state to select the max. timeout value
1144 *
1145 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001146 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001147static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001148{
1149 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001150 unsigned int flags = ONENAND_INT_MASTER;
1151 unsigned int interrupt;
1152 unsigned int ctrl;
1153
1154 while (1) {
1155 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1156 if (interrupt & flags)
1157 break;
1158 }
1159
1160 /* To get correct interrupt status in timeout case */
1161 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1162 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1163
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001164 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301165 int ecc = onenand_read_ecc(this);
1166 if (ecc & ONENAND_ECC_2BIT_ALL) {
1167 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1168 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001169 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301170 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001171 } else {
1172 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1173 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1174 return ONENAND_BBT_READ_FATAL_ERROR;
1175 }
1176
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001177 /* Initial bad block case: 0x2400 or 0x0400 */
1178 if (ctrl & ONENAND_CTRL_ERROR) {
1179 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1180 return ONENAND_BBT_READ_ERROR;
1181 }
1182
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001183 return 0;
1184}
1185
1186/**
1187 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1188 * @param mtd MTD device structure
1189 * @param from offset to read from
1190 * @param ops oob operation description structure
1191 *
1192 * OneNAND read out-of-band data from the spare area for bbt scan
1193 */
1194int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1195 struct mtd_oob_ops *ops)
1196{
1197 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001198 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301199 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001200 size_t len = ops->ooblen;
1201 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001202
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001203 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001204
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001205 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1206 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301207
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001208 /* Initialize return value */
1209 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001210
1211 /* Do not allow reads past end of device */
1212 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001213 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1214 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001215 }
1216
1217 /* Grab the lock and see if the device is available */
1218 onenand_get_device(mtd, FL_READING);
1219
1220 column = from & (mtd->oobsize - 1);
1221
1222 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001223
Kyungmin Park79e90462007-09-10 17:13:49 +09001224 thislen = mtd->oobsize - column;
1225 thislen = min_t(int, thislen, len);
1226
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001227 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301228 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001229
1230 onenand_update_bufferram(mtd, from, 0);
1231
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001232 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301233 if (unlikely(ret))
1234 ret = onenand_recover_lsb(mtd, from, ret);
1235
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001236 if (ret)
1237 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001238
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001239 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001240 read += thislen;
1241 if (read == len)
1242 break;
1243
Kyungmin Park79e90462007-09-10 17:13:49 +09001244 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001245
Kyungmin Park79e90462007-09-10 17:13:49 +09001246 /* Read more? */
1247 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001248 /* Update Page size */
1249 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001250 column = 0;
1251 }
1252 }
1253
1254 /* Deselect and wake up anyone waiting on the device */
1255 onenand_release_device(mtd);
1256
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001257 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001258 return ret;
1259}
1260
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001261
Kyungmin Park79e90462007-09-10 17:13:49 +09001262#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1263/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001264 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1265 * @param mtd MTD device structure
1266 * @param buf the databuffer to verify
1267 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001268 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001269static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001270{
1271 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001272 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301273 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001274
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001275 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1276 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301277
1278 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001279 onenand_update_bufferram(mtd, to, 0);
1280 status = this->wait(mtd, FL_READING);
1281 if (status)
1282 return status;
1283
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001284 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001285 for (i = 0; i < mtd->oobsize; i++)
1286 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1287 return -EBADMSG;
1288
1289 return 0;
1290}
1291
1292/**
1293 * onenand_verify - [GENERIC] verify the chip contents after a write
1294 * @param mtd MTD device structure
1295 * @param buf the databuffer to verify
1296 * @param addr offset to read from
1297 * @param len number of bytes to read and compare
1298 */
1299static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1300{
1301 struct onenand_chip *this = mtd->priv;
1302 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001303 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001304 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001305
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001306 while (len != 0) {
1307 thislen = min_t(int, this->writesize, len);
1308 column = addr & (this->writesize - 1);
1309 if (column + thislen > this->writesize)
1310 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001311
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001312 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001313
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001314 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001315
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001316 ret = this->wait(mtd, FL_READING);
1317 if (ret)
1318 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001319
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001320 onenand_update_bufferram(mtd, addr, 1);
1321
1322 dataram = this->base + ONENAND_DATARAM;
1323 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1324
1325 if (memcmp(buf, dataram + column, thislen))
1326 return -EBADMSG;
1327
1328 len -= thislen;
1329 buf += thislen;
1330 addr += thislen;
1331 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001332
1333 return 0;
1334}
1335#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001336#define onenand_verify(...) (0)
1337#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001338#endif
1339
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001340#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001341
1342/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001343 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001344 * @param mtd MTD device structure
1345 * @param oob_buf oob buffer
1346 * @param buf source address
1347 * @param column oob offset to write to
1348 * @param thislen oob length to write
1349 */
1350static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1351 const u_char *buf, int column, int thislen)
1352{
1353 struct onenand_chip *this = mtd->priv;
1354 struct nand_oobfree *free;
1355 int writecol = column;
1356 int writeend = column + thislen;
1357 int lastgap = 0;
1358 unsigned int i;
1359
1360 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301361 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1362 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001363 if (writecol >= lastgap)
1364 writecol += free->offset - lastgap;
1365 if (writeend >= lastgap)
1366 writeend += free->offset - lastgap;
1367 lastgap = free->offset + free->length;
1368 }
1369 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301370 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1371 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001372 int free_end = free->offset + free->length;
1373 if (free->offset < writeend && free_end > writecol) {
1374 int st = max_t(int,free->offset,writecol);
1375 int ed = min_t(int,free_end,writeend);
1376 int n = ed - st;
1377 memcpy(oob_buf + st, buf, n);
1378 buf += n;
1379 } else if (column == 0)
1380 break;
1381 }
1382 return 0;
1383}
1384
1385/**
1386 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1387 * @param mtd MTD device structure
1388 * @param to offset to write to
1389 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001390 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001391 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001392 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001393static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1394 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001395{
1396 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001397 int written = 0, column, thislen, subpage;
1398 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1399 size_t len = ops->len;
1400 size_t ooblen = ops->ooblen;
1401 const u_char *buf = ops->datbuf;
1402 const u_char *oob = ops->oobbuf;
1403 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001404 int ret = 0;
1405
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001406 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001407
1408 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001409 ops->retlen = 0;
1410 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001411
Kyungmin Park79e90462007-09-10 17:13:49 +09001412 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001413 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1414 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001415 return -EINVAL;
1416 }
1417
Sergey Lapin3a38a552013-01-14 03:46:50 +00001418 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001419 oobsize = this->ecclayout->oobavail;
1420 else
1421 oobsize = mtd->oobsize;
1422
1423 oobcolumn = to & (mtd->oobsize - 1);
1424
1425 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001426
1427 /* Loop until all data write */
1428 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001429 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001430
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001431 thislen = min_t(int, mtd->writesize - column, len - written);
1432 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001433
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001434 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001435
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001436 /* Partial page write */
1437 subpage = thislen < mtd->writesize;
1438 if (subpage) {
1439 memset(this->page_buf, 0xff, mtd->writesize);
1440 memcpy(this->page_buf + column, buf, thislen);
1441 wbuf = this->page_buf;
1442 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001443
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001444 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001445
1446 if (oob) {
1447 oobbuf = this->oob_buf;
1448
1449 /* We send data to spare ram with oobsize
1450 * * to prevent byte access */
1451 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001452 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001453 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1454 else
1455 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1456
1457 oobwritten += thisooblen;
1458 oob += thisooblen;
1459 oobcolumn = 0;
1460 } else
1461 oobbuf = (u_char *) ffchars;
1462
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001463 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001464
1465 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001466
1467 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001468
1469 /* In partial page write we don't update bufferram */
1470 onenand_update_bufferram(mtd, to, !ret && !subpage);
1471 if (ONENAND_IS_2PLANE(this)) {
1472 ONENAND_SET_BUFFERRAM1(this);
1473 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1474 }
1475
Kyungmin Park79e90462007-09-10 17:13:49 +09001476 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001477 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001478 break;
1479 }
1480
Kyungmin Park79e90462007-09-10 17:13:49 +09001481 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001482 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001483 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001484 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001485 break;
1486 }
1487
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001488 written += thislen;
1489
Kyungmin Park79e90462007-09-10 17:13:49 +09001490 if (written == len)
1491 break;
1492
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001493 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001494 to += thislen;
1495 buf += thislen;
1496 }
1497
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001498 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001499
1500 return ret;
1501}
1502
1503/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001504 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001505 * @param mtd MTD device structure
1506 * @param to offset to write to
1507 * @param len number of bytes to write
1508 * @param retlen pointer to variable to store the number of written bytes
1509 * @param buf the data to write
1510 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001511 *
1512 * OneNAND write out-of-band
1513 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001514static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1515 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001516{
1517 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001518 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301519 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001520 u_char *oobbuf;
1521 size_t len = ops->ooblen;
1522 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001523 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001524
1525 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001526
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001527 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001528
1529 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001530 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001531
Sergey Lapin3a38a552013-01-14 03:46:50 +00001532 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001533 oobsize = this->ecclayout->oobavail;
1534 else
1535 oobsize = mtd->oobsize;
1536
1537 column = to & (mtd->oobsize - 1);
1538
1539 if (unlikely(column >= oobsize)) {
1540 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001541 return -EINVAL;
1542 }
1543
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001544 /* For compatibility with NAND: Do not allow write past end of page */
1545 if (unlikely(column + len > oobsize)) {
1546 printk(KERN_ERR "onenand_write_oob_nolock: "
1547 "Attempt to write past end of page\n");
1548 return -EINVAL;
1549 }
1550
1551 /* Do not allow reads past end of device */
1552 if (unlikely(to >= mtd->size ||
1553 column + len > ((mtd->size >> this->page_shift) -
1554 (to >> this->page_shift)) * oobsize)) {
1555 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1556 return -EINVAL;
1557 }
1558
1559 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001560
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001561 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1562 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301563
Kyungmin Park79e90462007-09-10 17:13:49 +09001564 /* Loop until all data write */
1565 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001566 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001567
1568 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1569
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001570 /* We send data to spare ram with oobsize
1571 * to prevent byte access */
1572 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001573 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001574 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1575 else
1576 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001577 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001578
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001579 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301580 /* Set main area of DataRAM to 0xff*/
1581 memset(this->page_buf, 0xff, mtd->writesize);
1582 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1583 this->page_buf, 0, mtd->writesize);
1584 }
1585
1586 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001587
1588 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001589 if (ONENAND_IS_2PLANE(this)) {
1590 ONENAND_SET_BUFFERRAM1(this);
1591 onenand_update_bufferram(mtd, to + this->writesize, 0);
1592 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001593
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001594 ret = this->wait(mtd, FL_WRITING);
1595 if (ret) {
1596 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001597 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001598 }
1599
1600 ret = onenand_verify_oob(mtd, oobbuf, to);
1601 if (ret) {
1602 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1603 break;
1604 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001605
1606 written += thislen;
1607 if (written == len)
1608 break;
1609
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001610 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001611 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001612 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001613 }
1614
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001615 ops->oobretlen = written;
1616
1617 return ret;
1618}
1619
1620/**
1621 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1622 * @param mtd MTD device structure
1623 * @param to offset to write to
1624 * @param len number of bytes to write
1625 * @param retlen pointer to variable to store the number of written bytes
1626 * @param buf the data to write
1627 *
1628 * Write with ECC
1629 */
1630int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1631 size_t * retlen, const u_char * buf)
1632{
1633 struct mtd_oob_ops ops = {
1634 .len = len,
1635 .ooblen = 0,
1636 .datbuf = (u_char *) buf,
1637 .oobbuf = NULL,
1638 };
1639 int ret;
1640
1641 onenand_get_device(mtd, FL_WRITING);
1642 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001643 onenand_release_device(mtd);
1644
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001645 *retlen = ops.retlen;
1646 return ret;
1647}
Kyungmin Park79e90462007-09-10 17:13:49 +09001648
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001649/**
1650 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1651 * @param mtd MTD device structure
1652 * @param to offset to write to
1653 * @param ops oob operation description structure
1654 *
1655 * OneNAND write main and/or out-of-band
1656 */
1657int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1658 struct mtd_oob_ops *ops)
1659{
1660 int ret;
1661
1662 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001663 case MTD_OPS_PLACE_OOB:
1664 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001665 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001666 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001667 /* Not implemented yet */
1668 default:
1669 return -EINVAL;
1670 }
1671
1672 onenand_get_device(mtd, FL_WRITING);
1673 if (ops->datbuf)
1674 ret = onenand_write_ops_nolock(mtd, to, ops);
1675 else
1676 ret = onenand_write_oob_nolock(mtd, to, ops);
1677 onenand_release_device(mtd);
1678
1679 return ret;
1680
Kyungmin Park79e90462007-09-10 17:13:49 +09001681}
1682
Kyungmin Park396b0c42008-08-13 09:11:02 +09001683/**
1684 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1685 * @param mtd MTD device structure
1686 * @param ofs offset from device start
1687 * @param allowbbt 1, if its allowed to access the bbt area
1688 *
1689 * Check, if the block is bad, Either by reading the bad block table or
1690 * calling of the scan function.
1691 */
1692static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1693{
1694 struct onenand_chip *this = mtd->priv;
1695 struct bbm_info *bbm = this->bbm;
1696
1697 /* Return info from the table */
1698 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1699}
1700
1701
Kyungmin Park79e90462007-09-10 17:13:49 +09001702/**
1703 * onenand_erase - [MTD Interface] erase block(s)
1704 * @param mtd MTD device structure
1705 * @param instr erase instruction
1706 *
1707 * Erase one ore more blocks
1708 */
1709int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1710{
1711 struct onenand_chip *this = mtd->priv;
1712 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301713 loff_t addr = instr->addr;
1714 unsigned int len = instr->len;
1715 int ret = 0, i;
1716 struct mtd_erase_region_info *region = NULL;
1717 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001718
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301719 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1720 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001721
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301722 if (FLEXONENAND(this)) {
1723 /* Find the eraseregion of this address */
1724 i = flexonenand_region(mtd, addr);
1725 region = &mtd->eraseregions[i];
1726
1727 block_size = region->erasesize;
1728 region_end = region->offset
1729 + region->erasesize * region->numblocks;
1730
1731 /* Start address within region must align on block boundary.
1732 * Erase region's start offset is always block start address.
1733 */
1734 if (unlikely((addr - region->offset) & (block_size - 1))) {
1735 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1736 " Unaligned address\n");
1737 return -EINVAL;
1738 }
1739 } else {
1740 block_size = 1 << this->erase_shift;
1741
1742 /* Start address must align on block boundary */
1743 if (unlikely(addr & (block_size - 1))) {
1744 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1745 "Unaligned address\n");
1746 return -EINVAL;
1747 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001748 }
1749
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301750 /* Length must align on block boundary */
1751 if (unlikely(len & (block_size - 1))) {
Scott Wooddf83c472008-06-20 12:38:57 -05001752 MTDDEBUG (MTD_DEBUG_LEVEL0,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301753 "onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001754 return -EINVAL;
1755 }
1756
Kyungmin Park79e90462007-09-10 17:13:49 +09001757 /* Grab the lock and see if the device is available */
1758 onenand_get_device(mtd, FL_ERASING);
1759
1760 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001761 instr->state = MTD_ERASING;
1762
1763 while (len) {
1764
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001765 /* Check if we have a bad block, we do not erase bad blocks */
1766 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1767 printk(KERN_WARNING "onenand_erase: attempt to erase"
1768 " a bad block at addr 0x%08x\n",
1769 (unsigned int) addr);
1770 instr->state = MTD_ERASE_FAILED;
1771 goto erase_exit;
1772 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001773
1774 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1775
Kyungmin Park396b0c42008-08-13 09:11:02 +09001776 onenand_invalidate_bufferram(mtd, addr, block_size);
1777
Kyungmin Park79e90462007-09-10 17:13:49 +09001778 ret = this->wait(mtd, FL_ERASING);
1779 /* Check, if it is write protected */
1780 if (ret) {
1781 if (ret == -EPERM)
Scott Wooddf83c472008-06-20 12:38:57 -05001782 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001783 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001784 else
Scott Wooddf83c472008-06-20 12:38:57 -05001785 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001786 "Failed erase, block %d\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301787 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001788 instr->state = MTD_ERASE_FAILED;
1789 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001790
Kyungmin Park79e90462007-09-10 17:13:49 +09001791 goto erase_exit;
1792 }
1793
1794 len -= block_size;
1795 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301796
1797 if (addr == region_end) {
1798 if (!len)
1799 break;
1800 region++;
1801
1802 block_size = region->erasesize;
1803 region_end = region->offset
1804 + region->erasesize * region->numblocks;
1805
1806 if (len & (block_size - 1)) {
1807 /* This has been checked at MTD
1808 * partitioning level. */
1809 printk("onenand_erase: Unaligned address\n");
1810 goto erase_exit;
1811 }
1812 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001813 }
1814
1815 instr->state = MTD_ERASE_DONE;
1816
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001817erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001818
1819 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1820 /* Do call back function */
1821 if (!ret)
1822 mtd_erase_callback(instr);
1823
1824 /* Deselect and wake up anyone waiting on the device */
1825 onenand_release_device(mtd);
1826
1827 return ret;
1828}
1829
1830/**
1831 * onenand_sync - [MTD Interface] sync
1832 * @param mtd MTD device structure
1833 *
1834 * Sync is actually a wait for chip ready function
1835 */
1836void onenand_sync(struct mtd_info *mtd)
1837{
Scott Wooddf83c472008-06-20 12:38:57 -05001838 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001839
1840 /* Grab the lock and see if the device is available */
1841 onenand_get_device(mtd, FL_SYNCING);
1842
1843 /* Release it and go back */
1844 onenand_release_device(mtd);
1845}
1846
1847/**
1848 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1849 * @param mtd MTD device structure
1850 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001851 *
1852 * Check whether the block is bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001853 */
1854int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1855{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001856 int ret;
1857
1858 /* Check for invalid offset */
1859 if (ofs > mtd->size)
1860 return -EINVAL;
1861
1862 onenand_get_device(mtd, FL_READING);
1863 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1864 onenand_release_device(mtd);
1865 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001866}
1867
1868/**
Kyungmin Park87b49502008-11-13 15:14:33 +09001869 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1870 * @param mtd MTD device structure
1871 * @param ofs offset from device start
1872 *
1873 * This is the default implementation, which can be overridden by
1874 * a hardware specific driver.
1875 */
1876static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1877{
1878 struct onenand_chip *this = mtd->priv;
1879 struct bbm_info *bbm = this->bbm;
1880 u_char buf[2] = {0, 0};
1881 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001882 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park87b49502008-11-13 15:14:33 +09001883 .ooblen = 2,
1884 .oobbuf = buf,
1885 .ooboffs = 0,
1886 };
1887 int block;
1888
1889 /* Get block number */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301890 block = onenand_block(this, ofs);
Kyungmin Park87b49502008-11-13 15:14:33 +09001891 if (bbm->bbt)
1892 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1893
1894 /* We write two bytes, so we dont have to mess with 16 bit access */
1895 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1896 return onenand_write_oob_nolock(mtd, ofs, &ops);
1897}
1898
1899/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001900 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1901 * @param mtd MTD device structure
1902 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001903 *
1904 * Mark the block as bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001905 */
1906int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1907{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001908 int ret;
1909
1910 ret = onenand_block_isbad(mtd, ofs);
1911 if (ret) {
1912 /* If it was bad already, return success and do nothing */
1913 if (ret > 0)
1914 return 0;
1915 return ret;
1916 }
1917
Sergey Lapin3a38a552013-01-14 03:46:50 +00001918 ret = mtd_block_markbad(mtd, ofs);
Kyungmin Park396b0c42008-08-13 09:11:02 +09001919 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001920}
1921
1922/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001923 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1924 * @param mtd MTD device structure
1925 * @param ofs offset relative to mtd start
1926 * @param len number of bytes to lock or unlock
1927 * @param cmd lock or unlock command
Kyungmin Park79e90462007-09-10 17:13:49 +09001928 *
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001929 * Lock or unlock one or more blocks
Kyungmin Park79e90462007-09-10 17:13:49 +09001930 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001931static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park79e90462007-09-10 17:13:49 +09001932{
1933 struct onenand_chip *this = mtd->priv;
1934 int start, end, block, value, status;
1935
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301936 start = onenand_block(this, ofs);
1937 end = onenand_block(this, ofs + len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001938
1939 /* Continuous lock scheme */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001940 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park79e90462007-09-10 17:13:49 +09001941 /* Set start block address */
1942 this->write_word(start,
1943 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1944 /* Set end block address */
1945 this->write_word(end - 1,
1946 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1947 /* Write unlock command */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001948 this->command(mtd, cmd, 0, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001949
1950 /* There's no return value */
1951 this->wait(mtd, FL_UNLOCKING);
1952
1953 /* Sanity check */
1954 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1955 & ONENAND_CTRL_ONGO)
1956 continue;
1957
1958 /* Check lock status */
1959 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1960 if (!(status & ONENAND_WP_US))
1961 printk(KERN_ERR "wp status = 0x%x\n", status);
1962
1963 return 0;
1964 }
1965
1966 /* Block lock scheme */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301967 for (block = start; block < end; block++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001968 /* Set block address */
1969 value = onenand_block_address(this, block);
1970 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1971 /* Select DataRAM for DDP */
1972 value = onenand_bufferram_address(this, block);
1973 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1974
Kyungmin Park79e90462007-09-10 17:13:49 +09001975 /* Set start block address */
1976 this->write_word(block,
1977 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1978 /* Write unlock command */
1979 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1980
1981 /* There's no return value */
1982 this->wait(mtd, FL_UNLOCKING);
1983
1984 /* Sanity check */
1985 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1986 & ONENAND_CTRL_ONGO)
1987 continue;
1988
Kyungmin Park79e90462007-09-10 17:13:49 +09001989 /* Check lock status */
1990 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1991 if (!(status & ONENAND_WP_US))
1992 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1993 block, status);
1994 }
1995
1996 return 0;
1997}
1998
Stefan Roese5ed79ae2008-11-11 10:28:53 +01001999#ifdef ONENAND_LINUX
Kyungmin Park79e90462007-09-10 17:13:49 +09002000/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002001 * onenand_lock - [MTD Interface] Lock block(s)
2002 * @param mtd MTD device structure
2003 * @param ofs offset relative to mtd start
2004 * @param len number of bytes to unlock
2005 *
2006 * Lock one or more blocks
2007 */
2008static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2009{
2010 int ret;
2011
2012 onenand_get_device(mtd, FL_LOCKING);
2013 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2014 onenand_release_device(mtd);
2015 return ret;
2016}
2017
2018/**
2019 * onenand_unlock - [MTD Interface] Unlock block(s)
2020 * @param mtd MTD device structure
2021 * @param ofs offset relative to mtd start
2022 * @param len number of bytes to unlock
2023 *
2024 * Unlock one or more blocks
2025 */
2026static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2027{
2028 int ret;
2029
2030 onenand_get_device(mtd, FL_LOCKING);
2031 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2032 onenand_release_device(mtd);
2033 return ret;
2034}
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002035#endif
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002036
2037/**
2038 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2039 * @param this onenand chip data structure
2040 *
2041 * Check lock status
2042 */
2043static int onenand_check_lock_status(struct onenand_chip *this)
2044{
2045 unsigned int value, block, status;
2046 unsigned int end;
2047
2048 end = this->chipsize >> this->erase_shift;
2049 for (block = 0; block < end; block++) {
2050 /* Set block address */
2051 value = onenand_block_address(this, block);
2052 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2053 /* Select DataRAM for DDP */
2054 value = onenand_bufferram_address(this, block);
2055 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2056 /* Set start block address */
2057 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2058
2059 /* Check lock status */
2060 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2061 if (!(status & ONENAND_WP_US)) {
2062 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2063 return 0;
2064 }
2065 }
2066
2067 return 1;
2068}
2069
2070/**
2071 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2072 * @param mtd MTD device structure
2073 *
2074 * Unlock all blocks
2075 */
2076static void onenand_unlock_all(struct mtd_info *mtd)
2077{
2078 struct onenand_chip *this = mtd->priv;
2079 loff_t ofs = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302080 size_t len = mtd->size;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002081
2082 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2083 /* Set start block address */
2084 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2085 /* Write unlock command */
2086 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2087
2088 /* There's no return value */
2089 this->wait(mtd, FL_LOCKING);
2090
2091 /* Sanity check */
2092 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2093 & ONENAND_CTRL_ONGO)
2094 continue;
2095
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002096 /* Check lock status */
2097 if (onenand_check_lock_status(this))
2098 return;
2099
2100 /* Workaround for all block unlock in DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302101 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002102 /* All blocks on another chip */
2103 ofs = this->chipsize >> 1;
2104 len = this->chipsize >> 1;
2105 }
2106 }
2107
2108 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2109}
2110
2111
2112/**
2113 * onenand_check_features - Check and set OneNAND features
2114 * @param mtd MTD data structure
2115 *
2116 * Check and set OneNAND features
2117 * - lock scheme
2118 * - two plane
2119 */
2120static void onenand_check_features(struct mtd_info *mtd)
2121{
2122 struct onenand_chip *this = mtd->priv;
2123 unsigned int density, process;
2124
2125 /* Lock scheme depends on density and process */
2126 density = onenand_get_density(this->device_id);
2127 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2128
2129 /* Lock scheme */
2130 switch (density) {
2131 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002132 if (ONENAND_IS_DDP(this))
2133 this->options |= ONENAND_HAS_2PLANE;
2134 else
2135 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002136
2137 case ONENAND_DEVICE_DENSITY_2Gb:
2138 /* 2Gb DDP don't have 2 plane */
2139 if (!ONENAND_IS_DDP(this))
2140 this->options |= ONENAND_HAS_2PLANE;
2141 this->options |= ONENAND_HAS_UNLOCK_ALL;
2142
2143 case ONENAND_DEVICE_DENSITY_1Gb:
2144 /* A-Die has all block unlock */
2145 if (process)
2146 this->options |= ONENAND_HAS_UNLOCK_ALL;
2147 break;
2148
2149 default:
2150 /* Some OneNAND has continuous lock scheme */
2151 if (!process)
2152 this->options |= ONENAND_HAS_CONT_LOCK;
2153 break;
2154 }
2155
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302156 if (ONENAND_IS_MLC(this))
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002157 this->options |= ONENAND_HAS_4KB_PAGE;
2158
2159 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302160 this->options &= ~ONENAND_HAS_2PLANE;
2161
2162 if (FLEXONENAND(this)) {
2163 this->options &= ~ONENAND_HAS_CONT_LOCK;
2164 this->options |= ONENAND_HAS_UNLOCK_ALL;
2165 }
2166
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002167 if (this->options & ONENAND_HAS_CONT_LOCK)
2168 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2169 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2170 printk(KERN_DEBUG "Chip support all block unlock\n");
2171 if (this->options & ONENAND_HAS_2PLANE)
2172 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002173 if (this->options & ONENAND_HAS_4KB_PAGE)
2174 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2175
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002176}
2177
2178/**
Kyungmin Park79e90462007-09-10 17:13:49 +09002179 * onenand_print_device_info - Print device ID
2180 * @param device device ID
2181 *
2182 * Print device ID
2183 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002184char *onenand_print_device_info(int device, int version)
Kyungmin Park79e90462007-09-10 17:13:49 +09002185{
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302186 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002187 char *dev_info = malloc(80);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002188 char *p = dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002189
2190 vcc = device & ONENAND_DEVICE_VCC_MASK;
2191 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2192 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302193 density = onenand_get_density(device);
2194 flexonenand = device & DEVICE_IS_FLEXONENAND;
2195 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park79e90462007-09-10 17:13:49 +09002196 demuxed ? "" : "Muxed ",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302197 flexonenand ? "Flex-" : "",
Kyungmin Park79e90462007-09-10 17:13:49 +09002198 ddp ? "(DDP)" : "",
2199 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002200
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002201 sprintf(p, "\nOneNAND version = 0x%04x", version);
2202 printk("%s\n", dev_info);
2203
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002204 return dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002205}
2206
2207static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra891d81b2010-10-11 21:48:03 +02002208 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002209 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002210};
2211
2212/**
2213 * onenand_check_maf - Check manufacturer ID
2214 * @param manuf manufacturer ID
2215 *
2216 * Check manufacturer ID
2217 */
2218static int onenand_check_maf(int manuf)
2219{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002220 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park79e90462007-09-10 17:13:49 +09002221 int i;
Marek Vasute3334742011-11-06 00:59:52 +01002222#ifdef ONENAND_DEBUG
2223 char *name;
2224#endif
Kyungmin Park79e90462007-09-10 17:13:49 +09002225
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302226 for (i = 0; i < size; i++)
Kyungmin Park79e90462007-09-10 17:13:49 +09002227 if (manuf == onenand_manuf_ids[i].id)
2228 break;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002229
Marek Vasute3334742011-11-06 00:59:52 +01002230#ifdef ONENAND_DEBUG
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002231 if (i < size)
2232 name = onenand_manuf_ids[i].name;
2233 else
2234 name = "Unknown";
Kyungmin Park79e90462007-09-10 17:13:49 +09002235
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002236 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park79e90462007-09-10 17:13:49 +09002237#endif
2238
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002239 return i == size;
Kyungmin Park79e90462007-09-10 17:13:49 +09002240}
2241
2242/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302243* flexonenand_get_boundary - Reads the SLC boundary
2244* @param onenand_info - onenand info structure
2245*
2246* Fill up boundary[] field in onenand_chip
2247**/
2248static int flexonenand_get_boundary(struct mtd_info *mtd)
2249{
2250 struct onenand_chip *this = mtd->priv;
2251 unsigned int die, bdry;
Marek Vasute3334742011-11-06 00:59:52 +01002252 int syscfg, locked;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302253
2254 /* Disable ECC */
2255 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2256 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2257
2258 for (die = 0; die < this->dies; die++) {
2259 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2260 this->wait(mtd, FL_SYNCING);
2261
2262 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002263 this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302264
2265 bdry = this->read_word(this->base + ONENAND_DATARAM);
2266 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2267 locked = 0;
2268 else
2269 locked = 1;
2270 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2271
2272 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002273 this->wait(mtd, FL_RESETING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302274
2275 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2276 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2277 }
2278
2279 /* Enable ECC */
2280 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2281 return 0;
2282}
2283
2284/**
2285 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2286 * boundary[], diesize[], mtd->size, mtd->erasesize,
2287 * mtd->eraseregions
2288 * @param mtd - MTD device structure
2289 */
2290static void flexonenand_get_size(struct mtd_info *mtd)
2291{
2292 struct onenand_chip *this = mtd->priv;
2293 int die, i, eraseshift, density;
2294 int blksperdie, maxbdry;
2295 loff_t ofs;
2296
2297 density = onenand_get_density(this->device_id);
2298 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2299 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2300 maxbdry = blksperdie - 1;
2301 eraseshift = this->erase_shift - 1;
2302
2303 mtd->numeraseregions = this->dies << 1;
2304
2305 /* This fills up the device boundary */
2306 flexonenand_get_boundary(mtd);
2307 die = 0;
2308 ofs = 0;
2309 i = -1;
2310 for (; die < this->dies; die++) {
2311 if (!die || this->boundary[die-1] != maxbdry) {
2312 i++;
2313 mtd->eraseregions[i].offset = ofs;
2314 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2315 mtd->eraseregions[i].numblocks =
2316 this->boundary[die] + 1;
2317 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2318 eraseshift++;
2319 } else {
2320 mtd->numeraseregions -= 1;
2321 mtd->eraseregions[i].numblocks +=
2322 this->boundary[die] + 1;
2323 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2324 }
2325 if (this->boundary[die] != maxbdry) {
2326 i++;
2327 mtd->eraseregions[i].offset = ofs;
2328 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2329 mtd->eraseregions[i].numblocks = maxbdry ^
2330 this->boundary[die];
2331 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2332 eraseshift--;
2333 } else
2334 mtd->numeraseregions -= 1;
2335 }
2336
2337 /* Expose MLC erase size except when all blocks are SLC */
2338 mtd->erasesize = 1 << this->erase_shift;
2339 if (mtd->numeraseregions == 1)
2340 mtd->erasesize >>= 1;
2341
2342 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2343 for (i = 0; i < mtd->numeraseregions; i++)
2344 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2345 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2346 mtd->eraseregions[i].erasesize,
2347 mtd->eraseregions[i].numblocks);
2348
2349 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2350 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2351 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2352 << (this->erase_shift - 1);
2353 mtd->size += this->diesize[die];
2354 }
2355}
2356
2357/**
2358 * flexonenand_check_blocks_erased - Check if blocks are erased
2359 * @param mtd_info - mtd info structure
2360 * @param start - first erase block to check
2361 * @param end - last erase block to check
2362 *
2363 * Converting an unerased block from MLC to SLC
2364 * causes byte values to change. Since both data and its ECC
2365 * have changed, reads on the block give uncorrectable error.
2366 * This might lead to the block being detected as bad.
2367 *
2368 * Avoid this by ensuring that the block to be converted is
2369 * erased.
2370 */
2371static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2372 int start, int end)
2373{
2374 struct onenand_chip *this = mtd->priv;
2375 int i, ret;
2376 int block;
2377 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002378 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302379 .ooboffs = 0,
2380 .ooblen = mtd->oobsize,
2381 .datbuf = NULL,
2382 .oobbuf = this->oob_buf,
2383 };
2384 loff_t addr;
2385
2386 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2387
2388 for (block = start; block <= end; block++) {
2389 addr = flexonenand_addr(this, block);
2390 if (onenand_block_isbad_nolock(mtd, addr, 0))
2391 continue;
2392
2393 /*
2394 * Since main area write results in ECC write to spare,
2395 * it is sufficient to check only ECC bytes for change.
2396 */
2397 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2398 if (ret)
2399 return ret;
2400
2401 for (i = 0; i < mtd->oobsize; i++)
2402 if (this->oob_buf[i] != 0xff)
2403 break;
2404
2405 if (i != mtd->oobsize) {
2406 printk(KERN_WARNING "Block %d not erased.\n", block);
2407 return 1;
2408 }
2409 }
2410
2411 return 0;
2412}
2413
2414/**
2415 * flexonenand_set_boundary - Writes the SLC boundary
2416 * @param mtd - mtd info structure
2417 */
2418int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2419 int boundary, int lock)
2420{
2421 struct onenand_chip *this = mtd->priv;
2422 int ret, density, blksperdie, old, new, thisboundary;
2423 loff_t addr;
2424
2425 if (die >= this->dies)
2426 return -EINVAL;
2427
2428 if (boundary == this->boundary[die])
2429 return 0;
2430
2431 density = onenand_get_density(this->device_id);
2432 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2433 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2434
2435 if (boundary >= blksperdie) {
2436 printk("flexonenand_set_boundary:"
2437 "Invalid boundary value. "
2438 "Boundary not changed.\n");
2439 return -EINVAL;
2440 }
2441
2442 /* Check if converting blocks are erased */
2443 old = this->boundary[die] + (die * this->density_mask);
2444 new = boundary + (die * this->density_mask);
2445 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2446 + 1, max(old, new));
2447 if (ret) {
2448 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2449 return ret;
2450 }
2451
2452 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2453 this->wait(mtd, FL_SYNCING);
2454
2455 /* Check is boundary is locked */
2456 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2457 ret = this->wait(mtd, FL_READING);
2458
2459 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2460 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2461 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2462 goto out;
2463 }
2464
2465 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2466 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2467
2468 boundary &= FLEXONENAND_PI_MASK;
2469 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2470
2471 addr = die ? this->diesize[0] : 0;
2472 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2473 ret = this->wait(mtd, FL_ERASING);
2474 if (ret) {
2475 printk("flexonenand_set_boundary:"
2476 "Failed PI erase for Die %d\n", die);
2477 goto out;
2478 }
2479
2480 this->write_word(boundary, this->base + ONENAND_DATARAM);
2481 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2482 ret = this->wait(mtd, FL_WRITING);
2483 if (ret) {
2484 printk("flexonenand_set_boundary:"
2485 "Failed PI write for Die %d\n", die);
2486 goto out;
2487 }
2488
2489 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2490 ret = this->wait(mtd, FL_WRITING);
2491out:
2492 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2493 this->wait(mtd, FL_RESETING);
2494 if (!ret)
2495 /* Recalculate device size on boundary change*/
2496 flexonenand_get_size(mtd);
2497
2498 return ret;
2499}
2500
2501/**
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002502 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park79e90462007-09-10 17:13:49 +09002503 * @param mtd MTD device structure
2504 *
2505 * OneNAND detection method:
2506 * Compare the the values from command with ones from register
2507 */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002508static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park79e90462007-09-10 17:13:49 +09002509{
2510 struct onenand_chip *this = mtd->priv;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002511 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002512 int syscfg;
2513
2514 /* Save system configuration 1 */
2515 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002516
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002517 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002518 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2519 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park79e90462007-09-10 17:13:49 +09002520
2521 /* Send the command for reading device ID from BootRAM */
2522 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2523
2524 /* Read manufacturer and device IDs from BootRAM */
2525 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2526 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2527
Kyungmin Park79e90462007-09-10 17:13:49 +09002528 /* Reset OneNAND to read default register values */
2529 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2530
Kyungmin Park396b0c42008-08-13 09:11:02 +09002531 /* Wait reset */
2532 this->wait(mtd, FL_RESETING);
Kyungmin Park79e90462007-09-10 17:13:49 +09002533
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002534 /* Restore system configuration 1 */
2535 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2536
2537 /* Check manufacturer ID */
2538 if (onenand_check_maf(bram_maf_id))
2539 return -ENXIO;
2540
Kyungmin Park79e90462007-09-10 17:13:49 +09002541 /* Read manufacturer and device IDs from Register */
2542 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2543 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2544
2545 /* Check OneNAND device */
2546 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2547 return -ENXIO;
Kyungmin Park05d23182008-03-17 08:54:06 +09002548
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002549 return 0;
2550}
2551
2552/**
2553 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2554 * @param mtd MTD device structure
2555 *
2556 * OneNAND detection method:
2557 * Compare the the values from command with ones from register
2558 */
2559int onenand_probe(struct mtd_info *mtd)
2560{
2561 struct onenand_chip *this = mtd->priv;
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002562 int dev_id, ver_id;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002563 int density;
2564 int ret;
2565
2566 ret = this->chip_probe(mtd);
2567 if (ret)
2568 return ret;
2569
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002570 /* Read device IDs from Register */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002571 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2572 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2573 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2574
Kyungmin Park79e90462007-09-10 17:13:49 +09002575 /* Flash device information */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002576 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park79e90462007-09-10 17:13:49 +09002577 this->device_id = dev_id;
Stefan Roese7c9aafe2008-11-11 10:29:09 +01002578 this->version_id = ver_id;
Kyungmin Park79e90462007-09-10 17:13:49 +09002579
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002580 /* Check OneNAND features */
2581 onenand_check_features(mtd);
2582
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002583 density = onenand_get_density(dev_id);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302584 if (FLEXONENAND(this)) {
2585 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2586 /* Maximum possible erase regions */
2587 mtd->numeraseregions = this->dies << 1;
2588 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2589 * (this->dies << 1));
2590 if (!mtd->eraseregions)
2591 return -ENOMEM;
2592 }
2593
2594 /*
2595 * For Flex-OneNAND, chipsize represents maximum possible device size.
2596 * mtd->size represents the actual device size.
2597 */
Kyungmin Park79e90462007-09-10 17:13:49 +09002598 this->chipsize = (16 << density) << 20;
2599
2600 /* OneNAND page size & block size */
2601 /* The data buffer size is equal to page size */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002602 mtd->writesize =
Kyungmin Park79e90462007-09-10 17:13:49 +09002603 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302604 /* We use the full BufferRAM */
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002605 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302606 mtd->writesize <<= 1;
2607
Kyungmin Park396b0c42008-08-13 09:11:02 +09002608 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park79e90462007-09-10 17:13:49 +09002609 /* Pagers per block is always 64 in OneNAND */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002610 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302611 /*
2612 * Flex-OneNAND SLC area has 64 pages per block.
2613 * Flex-OneNAND MLC area has 128 pages per block.
2614 * Expose MLC erase size to find erase_shift and page_mask.
2615 */
2616 if (FLEXONENAND(this))
2617 mtd->erasesize <<= 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002618
2619 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002620 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002621 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Park396b0c42008-08-13 09:11:02 +09002622 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302623 /* Set density mask. it is used for DDP */
2624 if (ONENAND_IS_DDP(this))
2625 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002626 /* It's real page size */
2627 this->writesize = mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002628
2629 /* REVIST: Multichip handling */
2630
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302631 if (FLEXONENAND(this))
2632 flexonenand_get_size(mtd);
2633 else
2634 mtd->size = this->chipsize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002635
Kyungmin Park396b0c42008-08-13 09:11:02 +09002636 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002637 mtd->_erase = onenand_erase;
2638 mtd->_read = onenand_read;
2639 mtd->_write = onenand_write;
2640 mtd->_read_oob = onenand_read_oob;
2641 mtd->_write_oob = onenand_write_oob;
2642 mtd->_sync = onenand_sync;
2643 mtd->_block_isbad = onenand_block_isbad;
2644 mtd->_block_markbad = onenand_block_markbad;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002645
Kyungmin Park79e90462007-09-10 17:13:49 +09002646 return 0;
2647}
2648
2649/**
2650 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2651 * @param mtd MTD device structure
2652 * @param maxchips Number of chips to scan for
2653 *
2654 * This fills out all the not initialized function pointers
2655 * with the defaults.
2656 * The flash ID is read and the mtd/chip structures are
2657 * filled with the appropriate values.
2658 */
2659int onenand_scan(struct mtd_info *mtd, int maxchips)
2660{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002661 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002662 struct onenand_chip *this = mtd->priv;
2663
2664 if (!this->read_word)
2665 this->read_word = onenand_readw;
2666 if (!this->write_word)
2667 this->write_word = onenand_writew;
2668
2669 if (!this->command)
2670 this->command = onenand_command;
2671 if (!this->wait)
2672 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002673 if (!this->bbt_wait)
2674 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002675
2676 if (!this->read_bufferram)
2677 this->read_bufferram = onenand_read_bufferram;
2678 if (!this->write_bufferram)
2679 this->write_bufferram = onenand_write_bufferram;
2680
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002681 if (!this->chip_probe)
2682 this->chip_probe = onenand_chip_probe;
2683
Kyungmin Park87b49502008-11-13 15:14:33 +09002684 if (!this->block_markbad)
2685 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002686 if (!this->scan_bbt)
2687 this->scan_bbt = onenand_default_bbt;
2688
Kyungmin Park79e90462007-09-10 17:13:49 +09002689 if (onenand_probe(mtd))
2690 return -ENXIO;
2691
2692 /* Set Sync. Burst Read after probing */
2693 if (this->mmcontrol) {
2694 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2695 this->read_bufferram = onenand_sync_read_bufferram;
2696 }
2697
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002698 /* Allocate buffers, if necessary */
2699 if (!this->page_buf) {
2700 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2701 if (!this->page_buf) {
2702 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2703 return -ENOMEM;
2704 }
2705 this->options |= ONENAND_PAGEBUF_ALLOC;
2706 }
2707 if (!this->oob_buf) {
2708 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2709 if (!this->oob_buf) {
2710 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2711 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2712 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2713 kfree(this->page_buf);
2714 }
2715 return -ENOMEM;
2716 }
2717 this->options |= ONENAND_OOBBUF_ALLOC;
2718 }
2719
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002720 this->state = FL_READY;
2721
2722 /*
2723 * Allow subpage writes up to oobsize.
2724 */
2725 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302726 case 128:
2727 this->ecclayout = &onenand_oob_128;
2728 mtd->subpage_sft = 0;
2729 break;
2730
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002731 case 64:
2732 this->ecclayout = &onenand_oob_64;
2733 mtd->subpage_sft = 2;
2734 break;
2735
2736 case 32:
2737 this->ecclayout = &onenand_oob_32;
2738 mtd->subpage_sft = 1;
2739 break;
2740
2741 default:
2742 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2743 mtd->oobsize);
2744 mtd->subpage_sft = 0;
2745 /* To prevent kernel oops */
2746 this->ecclayout = &onenand_oob_32;
2747 break;
2748 }
2749
2750 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2751
2752 /*
2753 * The number of bytes available for a client to place data into
2754 * the out of band area
2755 */
2756 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302757
2758 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002759 this->ecclayout->oobfree[i].length; i++)
2760 this->ecclayout->oobavail +=
2761 this->ecclayout->oobfree[i].length;
2762 mtd->oobavail = this->ecclayout->oobavail;
2763
2764 mtd->ecclayout = this->ecclayout;
2765
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002766 /* Unlock whole block */
2767 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002768
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002769 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002770}
2771
2772/**
2773 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2774 * @param mtd MTD device structure
2775 */
2776void onenand_release(struct mtd_info *mtd)
2777{
2778}