blob: d194d974661c567c12f8bd73ad1f75b6e2d67374 [file] [log] [blame]
Kyungmin Park916527f2007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Amul Kumar Sahacacbe912009-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 Park916527f2007-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>
Ladislav Michld9098ee2016-07-12 20:28:19 +020023#include <watchdog.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000024#include <linux/compat.h>
Kyungmin Park916527f2007-09-10 17:13:49 +090025#include <linux/mtd/mtd.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020026#include "linux/mtd/flashchip.h"
Kyungmin Park916527f2007-09-10 17:13:49 +090027#include <linux/mtd/onenand.h>
28
29#include <asm/io.h>
30#include <asm/errno.h>
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +020031#include <malloc.h>
Kyungmin Park916527f2007-09-10 17:13:49 +090032
Kyungmin Park77e475c2008-03-31 10:40:36 +090033/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +053034static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park77e475c2008-03-31 10:40:36 +090035{
36 void *ret = dst;
37 short *d = dst;
38 const short *s = src;
39
40 len >>= 1;
41 while (len-- > 0)
42 *d++ = *s++;
43 return ret;
44}
45
Stefan Roese1ae39862008-12-02 11:06:47 +010046/**
Amul Kumar Sahacacbe912009-11-06 17:15:31 +053047 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
48 * For now, we expose only 64 out of 80 ecc bytes
49 */
50static struct nand_ecclayout onenand_oob_128 = {
51 .eccbytes = 64,
52 .eccpos = {
53 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
54 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
55 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
56 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
57 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
58 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
59 102, 103, 104, 105
60 },
61 .oobfree = {
62 {2, 4}, {18, 4}, {34, 4}, {50, 4},
63 {66, 4}, {82, 4}, {98, 4}, {114, 4}
64 }
65};
66
67/**
Stefan Roese1ae39862008-12-02 11:06:47 +010068 * onenand_oob_64 - oob info for large (2KB) page
69 */
70static struct nand_ecclayout onenand_oob_64 = {
71 .eccbytes = 20,
72 .eccpos = {
73 8, 9, 10, 11, 12,
74 24, 25, 26, 27, 28,
75 40, 41, 42, 43, 44,
76 56, 57, 58, 59, 60,
77 },
78 .oobfree = {
79 {2, 3}, {14, 2}, {18, 3}, {30, 2},
80 {34, 3}, {46, 2}, {50, 3}, {62, 2}
81 }
82};
83
84/**
85 * onenand_oob_32 - oob info for middle (1KB) page
86 */
87static struct nand_ecclayout onenand_oob_32 = {
88 .eccbytes = 10,
89 .eccpos = {
90 8, 9, 10, 11, 12,
91 24, 25, 26, 27, 28,
92 },
93 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
94};
95
Marek Vasut9b569422013-12-26 01:01:24 +010096/*
97 * Warning! This array is used with the memcpy_16() function, thus
98 * it must be aligned to 2 bytes. GCC can make this array unaligned
99 * as the array is made of unsigned char, which memcpy16() doesn't
100 * like and will cause unaligned access.
101 */
102static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park916527f2007-09-10 17:13:49 +0900103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park916527f2007-09-10 17:13:49 +0900119};
120
121/**
122 * onenand_readw - [OneNAND Interface] Read OneNAND register
123 * @param addr address to read
124 *
125 * Read OneNAND register
126 */
127static unsigned short onenand_readw(void __iomem * addr)
128{
129 return readw(addr);
130}
131
132/**
133 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
134 * @param value value to write
135 * @param addr address to write
136 *
137 * Write OneNAND register with value
138 */
139static void onenand_writew(unsigned short value, void __iomem * addr)
140{
141 writew(value, addr);
142}
143
144/**
145 * onenand_block_address - [DEFAULT] Get block address
146 * @param device the device id
147 * @param block the block
148 * @return translated block address if DDP, otherwise same
149 *
150 * Setup Start Address 1 Register (F100h)
151 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900152static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park916527f2007-09-10 17:13:49 +0900153{
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900154 /* Device Flash Core select, NAND Flash Block Address */
155 if (block & this->density_mask)
156 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park916527f2007-09-10 17:13:49 +0900157
158 return block;
159}
160
161/**
162 * onenand_bufferram_address - [DEFAULT] Get bufferram address
163 * @param device the device id
164 * @param block the block
165 * @return set DBS value if DDP, otherwise 0
166 *
167 * Setup Start Address 2 Register (F101h) for DDP
168 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900169static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park916527f2007-09-10 17:13:49 +0900170{
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900171 /* Device BufferRAM Select */
172 if (block & this->density_mask)
173 return ONENAND_DDP_CHIP1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900174
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900175 return ONENAND_DDP_CHIP0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900176}
177
178/**
179 * onenand_page_address - [DEFAULT] Get page address
180 * @param page the page address
181 * @param sector the sector address
182 * @return combined page and sector address
183 *
184 * Setup Start Address 8 Register (F107h)
185 */
186static int onenand_page_address(int page, int sector)
187{
188 /* Flash Page Address, Flash Sector Address */
189 int fpa, fsa;
190
191 fpa = page & ONENAND_FPA_MASK;
192 fsa = sector & ONENAND_FSA_MASK;
193
194 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
195}
196
197/**
198 * onenand_buffer_address - [DEFAULT] Get buffer address
199 * @param dataram1 DataRAM index
200 * @param sectors the sector address
201 * @param count the number of sectors
202 * @return the start buffer value
203 *
204 * Setup Start Buffer Register (F200h)
205 */
206static int onenand_buffer_address(int dataram1, int sectors, int count)
207{
208 int bsa, bsc;
209
210 /* BufferRAM Sector Address */
211 bsa = sectors & ONENAND_BSA_MASK;
212
213 if (dataram1)
214 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
215 else
216 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
217
218 /* BufferRAM Sector Count */
219 bsc = count & ONENAND_BSC_MASK;
220
221 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
222}
223
224/**
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530225 * flexonenand_block - Return block number for flash address
226 * @param this - OneNAND device structure
227 * @param addr - Address for which block number is needed
228 */
229static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
230{
231 unsigned int boundary, blk, die = 0;
232
233 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
234 die = 1;
235 addr -= this->diesize[0];
236 }
237
238 boundary = this->boundary[die];
239
240 blk = addr >> (this->erase_shift - 1);
241 if (blk > boundary)
242 blk = (blk + boundary + 1) >> 1;
243
244 blk += die ? this->density_mask : 0;
245 return blk;
246}
247
248unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
249{
250 if (!FLEXONENAND(this))
251 return addr >> this->erase_shift;
252 return flexonenand_block(this, addr);
253}
254
255/**
256 * flexonenand_addr - Return address of the block
257 * @this: OneNAND device structure
258 * @block: Block number on Flex-OneNAND
259 *
260 * Return address of the block
261 */
262static loff_t flexonenand_addr(struct onenand_chip *this, int block)
263{
264 loff_t ofs = 0;
265 int die = 0, boundary;
266
267 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
268 block -= this->density_mask;
269 die = 1;
270 ofs = this->diesize[0];
271 }
272
273 boundary = this->boundary[die];
274 ofs += (loff_t) block << (this->erase_shift - 1);
275 if (block > (boundary + 1))
276 ofs += (loff_t) (block - boundary - 1)
277 << (this->erase_shift - 1);
278 return ofs;
279}
280
281loff_t onenand_addr(struct onenand_chip *this, int block)
282{
283 if (!FLEXONENAND(this))
284 return (loff_t) block << this->erase_shift;
285 return flexonenand_addr(this, block);
286}
287
288/**
289 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
290 * @param mtd MTD device structure
291 * @param addr address whose erase region needs to be identified
292 */
293int flexonenand_region(struct mtd_info *mtd, loff_t addr)
294{
295 int i;
296
297 for (i = 0; i < mtd->numeraseregions; i++)
298 if (addr < mtd->eraseregions[i].offset)
299 break;
300 return i - 1;
301}
302
303/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900304 * onenand_get_density - [DEFAULT] Get OneNAND density
305 * @param dev_id OneNAND device ID
306 *
307 * Get OneNAND density from device ID
308 */
309static inline int onenand_get_density(int dev_id)
310{
311 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
312 return (density & ONENAND_DEVICE_DENSITY_MASK);
313}
314
315/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900316 * onenand_command - [DEFAULT] Send command to OneNAND device
317 * @param mtd MTD device structure
318 * @param cmd the command to be sent
319 * @param addr offset to read from or write to
320 * @param len number of bytes to read or write
321 *
322 * Send command to OneNAND device. This function is used for middle/large page
323 * devices (1KB/2KB Bytes per page)
324 */
325static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
326 size_t len)
327{
328 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530329 int value;
Kyungmin Park916527f2007-09-10 17:13:49 +0900330 int block, page;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530331
Kyungmin Park916527f2007-09-10 17:13:49 +0900332 /* Now we use page size operation */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530333 int sectors = 0, count = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900334
335 /* Address translation */
336 switch (cmd) {
337 case ONENAND_CMD_UNLOCK:
338 case ONENAND_CMD_LOCK:
339 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900340 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park916527f2007-09-10 17:13:49 +0900341 block = -1;
342 page = -1;
343 break;
344
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530345 case FLEXONENAND_CMD_PI_ACCESS:
346 /* addr contains die index */
347 block = addr * this->density_mask;
Kyungmin Park916527f2007-09-10 17:13:49 +0900348 page = -1;
349 break;
350
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530351 case ONENAND_CMD_ERASE:
352 case ONENAND_CMD_BUFFERRAM:
353 block = onenand_block(this, addr);
354 page = -1;
355 break;
356
357 case FLEXONENAND_CMD_READ_PI:
358 cmd = ONENAND_CMD_READ;
359 block = addr * this->density_mask;
360 page = 0;
361 break;
362
Kyungmin Park916527f2007-09-10 17:13:49 +0900363 default:
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530364 block = onenand_block(this, addr);
365 page = (int) (addr
366 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park916527f2007-09-10 17:13:49 +0900367 page &= this->page_mask;
368 break;
369 }
370
371 /* NOTE: The setting order of the registers is very important! */
372 if (cmd == ONENAND_CMD_BUFFERRAM) {
373 /* Select DataRAM for DDP */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900374 value = onenand_bufferram_address(this, block);
Kyungmin Park916527f2007-09-10 17:13:49 +0900375 this->write_word(value,
376 this->base + ONENAND_REG_START_ADDRESS2);
377
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100378 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530379 ONENAND_SET_BUFFERRAM0(this);
380 else
381 /* Switch to the next data buffer */
382 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park916527f2007-09-10 17:13:49 +0900383
384 return 0;
385 }
386
387 if (block != -1) {
388 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900389 value = onenand_block_address(this, block);
Kyungmin Park916527f2007-09-10 17:13:49 +0900390 this->write_word(value,
391 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900392
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530393 /* Select DataRAM for DDP */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900394 value = onenand_bufferram_address(this, block);
395 this->write_word(value,
396 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park916527f2007-09-10 17:13:49 +0900397 }
398
399 if (page != -1) {
400 int dataram;
401
402 switch (cmd) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530403 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park916527f2007-09-10 17:13:49 +0900404 case ONENAND_CMD_READ:
405 case ONENAND_CMD_READOOB:
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100406 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530407 dataram = ONENAND_SET_BUFFERRAM0(this);
408 else
409 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
410
Kyungmin Park916527f2007-09-10 17:13:49 +0900411 break;
412
413 default:
414 dataram = ONENAND_CURRENT_BUFFERRAM(this);
415 break;
416 }
417
418 /* Write 'FPA, FSA' of Flash */
419 value = onenand_page_address(page, sectors);
420 this->write_word(value,
421 this->base + ONENAND_REG_START_ADDRESS8);
422
423 /* Write 'BSA, BSC' of DataRAM */
424 value = onenand_buffer_address(dataram, sectors, count);
425 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park916527f2007-09-10 17:13:49 +0900426 }
427
428 /* Interrupt clear */
429 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
430 /* Write command */
431 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
432
433 return 0;
434}
435
436/**
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530437 * onenand_read_ecc - return ecc status
438 * @param this onenand chip structure
439 */
440static int onenand_read_ecc(struct onenand_chip *this)
441{
442 int ecc, i;
443
444 if (!FLEXONENAND(this))
445 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
446
447 for (i = 0; i < 4; i++) {
448 ecc = this->read_word(this->base
449 + ((ONENAND_REG_ECC_STATUS + i) << 1));
450 if (likely(!ecc))
451 continue;
452 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
453 return ONENAND_ECC_2BIT_ALL;
454 }
455
456 return 0;
457}
458
459/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900460 * onenand_wait - [DEFAULT] wait until the command is done
461 * @param mtd MTD device structure
462 * @param state state to select the max. timeout value
463 *
464 * Wait for command done. This applies to all OneNAND command
465 * Read can take up to 30us, erase up to 2ms and program up to 350us
466 * according to general OneNAND specs
467 */
468static int onenand_wait(struct mtd_info *mtd, int state)
469{
470 struct onenand_chip *this = mtd->priv;
Kyungmin Park916527f2007-09-10 17:13:49 +0900471 unsigned int interrupt = 0;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530472 unsigned int ctrl;
Kyungmin Park916527f2007-09-10 17:13:49 +0900473
Ladislav Michld9098ee2016-07-12 20:28:19 +0200474 /* Wait at most 20ms ... */
475 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
476 u32 time_start = get_timer(0);
477 do {
478 WATCHDOG_RESET();
479 if (get_timer(time_start) > timeo)
480 return -EIO;
Kyungmin Park916527f2007-09-10 17:13:49 +0900481 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michld9098ee2016-07-12 20:28:19 +0200482 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park916527f2007-09-10 17:13:49 +0900483
484 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
485
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530486 if (interrupt & ONENAND_INT_READ) {
487 int ecc = onenand_read_ecc(this);
488 if (ecc & ONENAND_ECC_2BIT_ALL) {
489 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
490 return -EBADMSG;
491 }
492 }
493
Kyungmin Park916527f2007-09-10 17:13:49 +0900494 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900495 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
496 if (ctrl & ONENAND_CTRL_LOCK)
497 printk("onenand_wait: it's locked error = 0x%04x\n",
498 ctrl);
Kyungmin Park916527f2007-09-10 17:13:49 +0900499
Kyungmin Park916527f2007-09-10 17:13:49 +0900500 return -EIO;
501 }
502
Kyungmin Park916527f2007-09-10 17:13:49 +0900503
504 return 0;
505}
506
507/**
508 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
509 * @param mtd MTD data structure
510 * @param area BufferRAM area
511 * @return offset given area
512 *
513 * Return BufferRAM offset given area
514 */
515static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
516{
517 struct onenand_chip *this = mtd->priv;
518
519 if (ONENAND_CURRENT_BUFFERRAM(this)) {
520 if (area == ONENAND_DATARAM)
Kyungmin Parkd438d502008-08-13 09:11:02 +0900521 return mtd->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +0900522 if (area == ONENAND_SPARERAM)
523 return mtd->oobsize;
524 }
525
526 return 0;
527}
528
529/**
530 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
531 * @param mtd MTD data structure
532 * @param area BufferRAM area
533 * @param buffer the databuffer to put/get data
534 * @param offset offset to read from or write to
535 * @param count number of bytes to read/write
536 *
537 * Read the BufferRAM area
538 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900539static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900540 unsigned char *buffer, int offset,
541 size_t count)
542{
543 struct onenand_chip *this = mtd->priv;
544 void __iomem *bufferram;
545
546 bufferram = this->base + area;
547 bufferram += onenand_bufferram_offset(mtd, area);
548
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200549 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900550
551 return 0;
552}
553
554/**
555 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
556 * @param mtd MTD data structure
557 * @param area BufferRAM area
558 * @param buffer the databuffer to put/get data
559 * @param offset offset to read from or write to
560 * @param count number of bytes to read/write
561 *
562 * Read the BufferRAM area with Sync. Burst Mode
563 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900564static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900565 unsigned char *buffer, int offset,
566 size_t count)
567{
568 struct onenand_chip *this = mtd->priv;
569 void __iomem *bufferram;
570
571 bufferram = this->base + area;
572 bufferram += onenand_bufferram_offset(mtd, area);
573
574 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
575
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200576 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900577
578 this->mmcontrol(mtd, 0);
579
580 return 0;
581}
582
583/**
584 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
585 * @param mtd MTD data structure
586 * @param area BufferRAM area
587 * @param buffer the databuffer to put/get data
588 * @param offset offset to read from or write to
589 * @param count number of bytes to read/write
590 *
591 * Write the BufferRAM area
592 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900593static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park916527f2007-09-10 17:13:49 +0900594 const unsigned char *buffer, int offset,
595 size_t count)
596{
597 struct onenand_chip *this = mtd->priv;
598 void __iomem *bufferram;
599
600 bufferram = this->base + area;
601 bufferram += onenand_bufferram_offset(mtd, area);
602
Wolfgang Denkd2c6fbe2008-05-01 21:30:16 +0200603 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park916527f2007-09-10 17:13:49 +0900604
605 return 0;
606}
607
608/**
Stefan Roese4fca3312008-11-11 10:28:53 +0100609 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
610 * @param mtd MTD data structure
611 * @param addr address to check
612 * @return blockpage address
613 *
614 * Get blockpage address at 2x program mode
615 */
616static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
617{
618 struct onenand_chip *this = mtd->priv;
619 int blockpage, block, page;
620
621 /* Calculate the even block number */
622 block = (int) (addr >> this->erase_shift) & ~1;
623 /* Is it the odd plane? */
624 if (addr & this->writesize)
625 block++;
626 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
627 blockpage = (block << 7) | page;
628
629 return blockpage;
630}
631
632/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900633 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
634 * @param mtd MTD data structure
635 * @param addr address to check
636 * @return 1 if there are valid data, otherwise 0
637 *
638 * Check bufferram if there is data we required
639 */
640static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
641{
642 struct onenand_chip *this = mtd->priv;
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900643 int blockpage, found = 0;
644 unsigned int i;
Kyungmin Park916527f2007-09-10 17:13:49 +0900645
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900646 if (ONENAND_IS_2PLANE(this))
647 blockpage = onenand_get_2x_blockpage(mtd, addr);
648 else
649 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park916527f2007-09-10 17:13:49 +0900650
651 /* Is there valid data? */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900652 i = ONENAND_CURRENT_BUFFERRAM(this);
653 if (this->bufferram[i].blockpage == blockpage)
654 found = 1;
655 else {
656 /* Check another BufferRAM */
657 i = ONENAND_NEXT_BUFFERRAM(this);
658 if (this->bufferram[i].blockpage == blockpage) {
659 ONENAND_SET_NEXT_BUFFERRAM(this);
660 found = 1;
661 }
662 }
Kyungmin Park916527f2007-09-10 17:13:49 +0900663
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900664 if (found && ONENAND_IS_DDP(this)) {
665 /* Select DataRAM for DDP */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530666 int block = onenand_block(this, addr);
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900667 int value = onenand_bufferram_address(this, block);
668 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
669 }
670
671 return found;
Kyungmin Park916527f2007-09-10 17:13:49 +0900672}
673
674/**
675 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
676 * @param mtd MTD data structure
677 * @param addr address to update
678 * @param valid valid flag
679 *
680 * Update BufferRAM information
681 */
682static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
683 int valid)
684{
685 struct onenand_chip *this = mtd->priv;
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900686 int blockpage;
687 unsigned int i;
Kyungmin Park916527f2007-09-10 17:13:49 +0900688
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900689 if (ONENAND_IS_2PLANE(this))
690 blockpage = onenand_get_2x_blockpage(mtd, addr);
691 else
692 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park916527f2007-09-10 17:13:49 +0900693
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900694 /* Invalidate another BufferRAM */
695 i = ONENAND_NEXT_BUFFERRAM(this);
696 if (this->bufferram[i].blockpage == blockpage)
697 this->bufferram[i].blockpage = -1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900698
699 /* Update BufferRAM */
700 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900701 if (valid)
702 this->bufferram[i].blockpage = blockpage;
703 else
704 this->bufferram[i].blockpage = -1;
Kyungmin Park916527f2007-09-10 17:13:49 +0900705
706 return 0;
707}
708
709/**
Kyungmin Parkd438d502008-08-13 09:11:02 +0900710 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
711 * @param mtd MTD data structure
712 * @param addr start address to invalidate
713 * @param len length to invalidate
714 *
715 * Invalidate BufferRAM information
716 */
717static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200718 unsigned int len)
Kyungmin Parkd438d502008-08-13 09:11:02 +0900719{
720 struct onenand_chip *this = mtd->priv;
721 int i;
722 loff_t end_addr = addr + len;
723
724 /* Invalidate BufferRAM */
725 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900726 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Parkd438d502008-08-13 09:11:02 +0900727
728 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900729 this->bufferram[i].blockpage = -1;
Kyungmin Parkd438d502008-08-13 09:11:02 +0900730 }
731}
732
733/**
Kyungmin Park916527f2007-09-10 17:13:49 +0900734 * onenand_get_device - [GENERIC] Get chip for selected access
735 * @param mtd MTD device structure
736 * @param new_state the state which is requested
737 *
738 * Get the device and lock it for exclusive access
739 */
740static void onenand_get_device(struct mtd_info *mtd, int new_state)
741{
742 /* Do nothing */
743}
744
745/**
746 * onenand_release_device - [GENERIC] release chip
747 * @param mtd MTD device structure
748 *
749 * Deselect, release chip lock and wake up anyone waiting on the device
750 */
751static void onenand_release_device(struct mtd_info *mtd)
752{
753 /* Do nothing */
754}
755
756/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000757 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park916527f2007-09-10 17:13:49 +0900758 * @param mtd MTD device structure
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900759 * @param buf destination address
760 * @param column oob offset to read from
761 * @param thislen oob length to read
Kyungmin Park916527f2007-09-10 17:13:49 +0900762 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900763static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
764 int column, int thislen)
Kyungmin Park916527f2007-09-10 17:13:49 +0900765{
766 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900767 struct nand_oobfree *free;
768 int readcol = column;
769 int readend = column + thislen;
770 int lastgap = 0;
771 unsigned int i;
772 uint8_t *oob_buf = this->oob_buf;
Kyungmin Park916527f2007-09-10 17:13:49 +0900773
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900774 free = this->ecclayout->oobfree;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530775 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
776 i++, free++) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900777 if (readcol >= lastgap)
778 readcol += free->offset - lastgap;
779 if (readend >= lastgap)
780 readend += free->offset - lastgap;
781 lastgap = free->offset + free->length;
782 }
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900783 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900784 free = this->ecclayout->oobfree;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +0530785 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
786 i++, free++) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900787 int free_end = free->offset + free->length;
788 if (free->offset < readend && free_end > readcol) {
789 int st = max_t(int,free->offset,readcol);
790 int ed = min_t(int,free_end,readend);
791 int n = ed - st;
792 memcpy(buf, oob_buf + st, n);
793 buf += n;
794 } else if (column == 0)
795 break;
796 }
797 return 0;
798}
799
800/**
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530801 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
802 * @param mtd MTD device structure
803 * @param addr address to recover
804 * @param status return value from onenand_wait
805 *
806 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
807 * lower page address and MSB page has higher page address in paired pages.
808 * If power off occurs during MSB page program, the paired LSB page data can
809 * become corrupt. LSB page recovery read is a way to read LSB page though page
810 * data are corrupted. When uncorrectable error occurs as a result of LSB page
811 * read after power up, issue LSB page recovery read.
812 */
813static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
814{
815 struct onenand_chip *this = mtd->priv;
816 int i;
817
818 /* Recovery is only for Flex-OneNAND */
819 if (!FLEXONENAND(this))
820 return status;
821
822 /* check if we failed due to uncorrectable error */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000823 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530824 return status;
825
826 /* check if address lies in MLC region */
827 i = flexonenand_region(mtd, addr);
828 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
829 return status;
830
831 printk("onenand_recover_lsb:"
832 "Attempting to recover from uncorrectable read\n");
833
834 /* Issue the LSB page recovery command */
835 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
836 return this->wait(mtd, FL_READING);
837}
838
839/**
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900840 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
841 * @param mtd MTD device structure
842 * @param from offset to read from
843 * @param ops oob operation description structure
844 *
845 * OneNAND read main and/or out-of-band data
846 */
847static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
848 struct mtd_oob_ops *ops)
849{
850 struct onenand_chip *this = mtd->priv;
851 struct mtd_ecc_stats stats;
852 size_t len = ops->len;
853 size_t ooblen = ops->ooblen;
854 u_char *buf = ops->datbuf;
855 u_char *oobbuf = ops->oobbuf;
856 int read = 0, column, thislen;
857 int oobread = 0, oobcolumn, thisooblen, oobsize;
858 int ret = 0, boundary = 0;
859 int writesize = this->writesize;
860
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900861 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900862
Sergey Lapindfe64e22013-01-14 03:46:50 +0000863 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900864 oobsize = this->ecclayout->oobavail;
865 else
866 oobsize = mtd->oobsize;
867
868 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park916527f2007-09-10 17:13:49 +0900869
870 /* Do not allow reads past end of device */
871 if ((from + len) > mtd->size) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900872 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
873 ops->retlen = 0;
874 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900875 return -EINVAL;
876 }
877
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900878 stats = mtd->ecc_stats;
Kyungmin Park916527f2007-09-10 17:13:49 +0900879
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900880 /* Read-while-load method */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530881 /* Note: We can't use this feature in MLC */
Kyungmin Park916527f2007-09-10 17:13:49 +0900882
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900883 /* Do first load to bufferRAM */
884 if (read < len) {
Kyungmin Park916527f2007-09-10 17:13:49 +0900885 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900886 this->main_buf = buf;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900887 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +0900888 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530889 if (unlikely(ret))
890 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900891 onenand_update_bufferram(mtd, from, !ret);
892 if (ret == -EBADMSG)
893 ret = 0;
894 }
895 }
896
897 thislen = min_t(int, writesize, len - read);
898 column = from & (writesize - 1);
899 if (column + thislen > writesize)
900 thislen = writesize - column;
901
902 while (!ret) {
903 /* If there is more to load then start next load */
904 from += thislen;
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100905 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900906 this->main_buf = buf + thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900907 this->command(mtd, ONENAND_CMD_READ, from, writesize);
908 /*
909 * Chip boundary handling in DDP
910 * Now we issued chip 1 read and pointed chip 1
911 * bufferam so we have to point chip 0 bufferam.
912 */
913 if (ONENAND_IS_DDP(this) &&
914 unlikely(from == (this->chipsize >> 1))) {
915 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
916 boundary = 1;
917 } else
918 boundary = 0;
919 ONENAND_SET_PREV_BUFFERRAM(this);
Kyungmin Park916527f2007-09-10 17:13:49 +0900920 }
921
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900922 /* While load is going, read from last bufferRAM */
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900923 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park916527f2007-09-10 17:13:49 +0900924
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900925 /* Read oob area if needed */
926 if (oobbuf) {
927 thisooblen = oobsize - oobcolumn;
928 thisooblen = min_t(int, thisooblen, ooblen - oobread);
929
Sergey Lapindfe64e22013-01-14 03:46:50 +0000930 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900931 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
932 else
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900933 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900934 oobread += thisooblen;
935 oobbuf += thisooblen;
936 oobcolumn = 0;
937 }
938
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100939 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530940 this->command(mtd, ONENAND_CMD_READ, from, writesize);
941 ret = this->wait(mtd, FL_READING);
942 if (unlikely(ret))
943 ret = onenand_recover_lsb(mtd, from, ret);
944 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000945 if (mtd_is_eccerr(ret))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530946 ret = 0;
947 }
948
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900949 /* See if we are done */
Kyungmin Park916527f2007-09-10 17:13:49 +0900950 read += thislen;
951 if (read == len)
952 break;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900953 /* Set up for next read from bufferRAM */
954 if (unlikely(boundary))
955 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100956 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530957 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park916527f2007-09-10 17:13:49 +0900958 buf += thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900959 thislen = min_t(int, writesize, len - read);
960 column = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +0900961
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +0100962 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530963 /* Now wait for load */
964 ret = this->wait(mtd, FL_READING);
965 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000966 if (mtd_is_eccerr(ret))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530967 ret = 0;
968 }
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900969 }
Kyungmin Park916527f2007-09-10 17:13:49 +0900970
971 /*
972 * Return success, if no ECC failures, else -EBADMSG
973 * fs driver will take care of that, because
974 * retlen == desired len and result == -EBADMSG
975 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900976 ops->retlen = read;
977 ops->oobretlen = oobread;
978
979 if (ret)
980 return ret;
981
982 if (mtd->ecc_stats.failed - stats.failed)
983 return -EBADMSG;
984
Paul Burton40462e52013-09-04 15:16:56 +0100985 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
986 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900987}
988
989/**
990 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
991 * @param mtd MTD device structure
992 * @param from offset to read from
993 * @param ops oob operation description structure
994 *
995 * OneNAND read out-of-band data from the spare area
996 */
997static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
998 struct mtd_oob_ops *ops)
999{
1000 struct onenand_chip *this = mtd->priv;
1001 struct mtd_ecc_stats stats;
1002 int read = 0, thislen, column, oobsize;
1003 size_t len = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001004 unsigned int mode = ops->mode;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001005 u_char *buf = ops->oobbuf;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301006 int ret = 0, readcmd;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001007
1008 from += ops->ooboffs;
1009
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001010 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001011
1012 /* Initialize return length value */
1013 ops->oobretlen = 0;
1014
Sergey Lapindfe64e22013-01-14 03:46:50 +00001015 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001016 oobsize = this->ecclayout->oobavail;
1017 else
1018 oobsize = mtd->oobsize;
1019
1020 column = from & (mtd->oobsize - 1);
1021
1022 if (unlikely(column >= oobsize)) {
1023 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1024 return -EINVAL;
1025 }
1026
1027 /* Do not allow reads past end of device */
1028 if (unlikely(from >= mtd->size ||
1029 column + len > ((mtd->size >> this->page_shift) -
1030 (from >> this->page_shift)) * oobsize)) {
1031 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1032 return -EINVAL;
1033 }
1034
1035 stats = mtd->ecc_stats;
1036
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01001037 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1038 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301039
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001040 while (read < len) {
1041 thislen = oobsize - column;
1042 thislen = min_t(int, thislen, len);
1043
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001044 this->spare_buf = buf;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301045 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001046
1047 onenand_update_bufferram(mtd, from, 0);
1048
1049 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301050 if (unlikely(ret))
1051 ret = onenand_recover_lsb(mtd, from, ret);
1052
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001053 if (ret && ret != -EBADMSG) {
1054 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1055 break;
1056 }
1057
Sergey Lapindfe64e22013-01-14 03:46:50 +00001058 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001059 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1060 else
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001061 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001062
1063 read += thislen;
1064
1065 if (read == len)
1066 break;
1067
1068 buf += thislen;
1069
1070 /* Read more? */
1071 if (read < len) {
1072 /* Page size */
1073 from += mtd->writesize;
1074 column = 0;
1075 }
1076 }
1077
1078 ops->oobretlen = read;
1079
1080 if (ret)
1081 return ret;
1082
1083 if (mtd->ecc_stats.failed - stats.failed)
1084 return -EBADMSG;
1085
1086 return 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001087}
1088
1089/**
1090 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1091 * @param mtd MTD device structure
1092 * @param from offset to read from
1093 * @param len number of bytes to read
1094 * @param retlen pointer to variable to store the number of read bytes
1095 * @param buf the databuffer to put data
1096 *
1097 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1098*/
1099int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1100 size_t * retlen, u_char * buf)
1101{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001102 struct mtd_oob_ops ops = {
1103 .len = len,
1104 .ooblen = 0,
1105 .datbuf = buf,
1106 .oobbuf = NULL,
1107 };
1108 int ret;
1109
1110 onenand_get_device(mtd, FL_READING);
1111 ret = onenand_read_ops_nolock(mtd, from, &ops);
1112 onenand_release_device(mtd);
1113
1114 *retlen = ops.retlen;
1115 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001116}
1117
1118/**
1119 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1120 * @param mtd MTD device structure
1121 * @param from offset to read from
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001122 * @param ops oob operations description structure
Kyungmin Park916527f2007-09-10 17:13:49 +09001123 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001124 * OneNAND main and/or out-of-band
Kyungmin Park916527f2007-09-10 17:13:49 +09001125 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001126int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1127 struct mtd_oob_ops *ops)
1128{
1129 int ret;
1130
1131 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001132 case MTD_OPS_PLACE_OOB:
1133 case MTD_OPS_AUTO_OOB:
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001134 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001135 case MTD_OPS_RAW:
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001136 /* Not implemented yet */
1137 default:
1138 return -EINVAL;
1139 }
1140
1141 onenand_get_device(mtd, FL_READING);
1142 if (ops->datbuf)
1143 ret = onenand_read_ops_nolock(mtd, from, ops);
1144 else
1145 ret = onenand_read_oob_nolock(mtd, from, ops);
1146 onenand_release_device(mtd);
1147
1148 return ret;
1149}
1150
1151/**
1152 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1153 * @param mtd MTD device structure
1154 * @param state state to select the max. timeout value
1155 *
1156 * Wait for command done.
1157 */
1158static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1159{
1160 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001161 unsigned int interrupt;
1162 unsigned int ctrl;
1163
Ladislav Michld9098ee2016-07-12 20:28:19 +02001164 /* Wait at most 20ms ... */
1165 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
1166 u32 time_start = get_timer(0);
1167 do {
1168 WATCHDOG_RESET();
1169 if (get_timer(time_start) > timeo)
1170 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001171 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michld9098ee2016-07-12 20:28:19 +02001172 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001173
1174 /* To get correct interrupt status in timeout case */
1175 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1176 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1177
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001178 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301179 int ecc = onenand_read_ecc(this);
1180 if (ecc & ONENAND_ECC_2BIT_ALL) {
1181 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1182 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001183 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301184 }
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001185 } else {
1186 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1187 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1188 return ONENAND_BBT_READ_FATAL_ERROR;
1189 }
1190
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001191 /* Initial bad block case: 0x2400 or 0x0400 */
1192 if (ctrl & ONENAND_CTRL_ERROR) {
1193 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1194 return ONENAND_BBT_READ_ERROR;
1195 }
1196
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001197 return 0;
1198}
1199
1200/**
1201 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1202 * @param mtd MTD device structure
1203 * @param from offset to read from
1204 * @param ops oob operation description structure
1205 *
1206 * OneNAND read out-of-band data from the spare area for bbt scan
1207 */
1208int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1209 struct mtd_oob_ops *ops)
Kyungmin Park916527f2007-09-10 17:13:49 +09001210{
1211 struct onenand_chip *this = mtd->priv;
1212 int read = 0, thislen, column;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301213 int ret = 0, readcmd;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001214 size_t len = ops->ooblen;
1215 u_char *buf = ops->oobbuf;
Kyungmin Park916527f2007-09-10 17:13:49 +09001216
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001217 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001218
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01001219 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1220 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301221
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001222 /* Initialize return value */
1223 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001224
1225 /* Do not allow reads past end of device */
1226 if (unlikely((from + len) > mtd->size)) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001227 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1228 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park916527f2007-09-10 17:13:49 +09001229 }
1230
1231 /* Grab the lock and see if the device is available */
1232 onenand_get_device(mtd, FL_READING);
1233
1234 column = from & (mtd->oobsize - 1);
1235
1236 while (read < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001237
Kyungmin Park916527f2007-09-10 17:13:49 +09001238 thislen = mtd->oobsize - column;
1239 thislen = min_t(int, thislen, len);
1240
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001241 this->spare_buf = buf;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301242 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001243
1244 onenand_update_bufferram(mtd, from, 0);
1245
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001246 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301247 if (unlikely(ret))
1248 ret = onenand_recover_lsb(mtd, from, ret);
1249
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001250 if (ret)
1251 break;
Kyungmin Park916527f2007-09-10 17:13:49 +09001252
Kyungmin Parkce3277a2009-07-21 11:58:04 +09001253 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park916527f2007-09-10 17:13:49 +09001254 read += thislen;
1255 if (read == len)
1256 break;
1257
Kyungmin Park916527f2007-09-10 17:13:49 +09001258 buf += thislen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001259
Kyungmin Park916527f2007-09-10 17:13:49 +09001260 /* Read more? */
1261 if (read < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001262 /* Update Page size */
1263 from += this->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +09001264 column = 0;
1265 }
1266 }
1267
1268 /* Deselect and wake up anyone waiting on the device */
1269 onenand_release_device(mtd);
1270
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001271 ops->oobretlen = read;
Kyungmin Park916527f2007-09-10 17:13:49 +09001272 return ret;
1273}
1274
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001275
Kyungmin Park916527f2007-09-10 17:13:49 +09001276#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1277/**
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001278 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1279 * @param mtd MTD device structure
1280 * @param buf the databuffer to verify
1281 * @param to offset to read from
Kyungmin Park916527f2007-09-10 17:13:49 +09001282 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001283static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park916527f2007-09-10 17:13:49 +09001284{
1285 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001286 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301287 int status, i, readcmd;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001288
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01001289 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1290 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301291
1292 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001293 onenand_update_bufferram(mtd, to, 0);
1294 status = this->wait(mtd, FL_READING);
1295 if (status)
1296 return status;
1297
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001298 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001299 for (i = 0; i < mtd->oobsize; i++)
1300 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1301 return -EBADMSG;
1302
1303 return 0;
1304}
1305
1306/**
1307 * onenand_verify - [GENERIC] verify the chip contents after a write
1308 * @param mtd MTD device structure
1309 * @param buf the databuffer to verify
1310 * @param addr offset to read from
1311 * @param len number of bytes to read and compare
1312 */
1313static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1314{
1315 struct onenand_chip *this = mtd->priv;
1316 void __iomem *dataram;
Kyungmin Park916527f2007-09-10 17:13:49 +09001317 int ret = 0;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001318 int thislen, column;
Kyungmin Park916527f2007-09-10 17:13:49 +09001319
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001320 while (len != 0) {
1321 thislen = min_t(int, this->writesize, len);
1322 column = addr & (this->writesize - 1);
1323 if (column + thislen > this->writesize)
1324 thislen = this->writesize - column;
Kyungmin Park916527f2007-09-10 17:13:49 +09001325
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001326 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001327
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001328 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park916527f2007-09-10 17:13:49 +09001329
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001330 ret = this->wait(mtd, FL_READING);
1331 if (ret)
1332 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001333
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001334 onenand_update_bufferram(mtd, addr, 1);
1335
1336 dataram = this->base + ONENAND_DATARAM;
1337 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1338
1339 if (memcmp(buf, dataram + column, thislen))
1340 return -EBADMSG;
1341
1342 len -= thislen;
1343 buf += thislen;
1344 addr += thislen;
1345 }
Kyungmin Park916527f2007-09-10 17:13:49 +09001346
1347 return 0;
1348}
1349#else
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001350#define onenand_verify(...) (0)
1351#define onenand_verify_oob(...) (0)
Kyungmin Park916527f2007-09-10 17:13:49 +09001352#endif
1353
Stefan Roese1ae39862008-12-02 11:06:47 +01001354#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park916527f2007-09-10 17:13:49 +09001355
1356/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001357 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001358 * @param mtd MTD device structure
1359 * @param oob_buf oob buffer
1360 * @param buf source address
1361 * @param column oob offset to write to
1362 * @param thislen oob length to write
Kyungmin Park916527f2007-09-10 17:13:49 +09001363 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001364static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1365 const u_char *buf, int column, int thislen)
Kyungmin Park916527f2007-09-10 17:13:49 +09001366{
1367 struct onenand_chip *this = mtd->priv;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001368 struct nand_oobfree *free;
1369 int writecol = column;
1370 int writeend = column + thislen;
1371 int lastgap = 0;
1372 unsigned int i;
1373
1374 free = this->ecclayout->oobfree;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +05301375 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1376 i++, free++) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001377 if (writecol >= lastgap)
1378 writecol += free->offset - lastgap;
1379 if (writeend >= lastgap)
1380 writeend += free->offset - lastgap;
1381 lastgap = free->offset + free->length;
1382 }
1383 free = this->ecclayout->oobfree;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +05301384 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1385 i++, free++) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001386 int free_end = free->offset + free->length;
1387 if (free->offset < writeend && free_end > writecol) {
1388 int st = max_t(int,free->offset,writecol);
1389 int ed = min_t(int,free_end,writeend);
1390 int n = ed - st;
1391 memcpy(oob_buf + st, buf, n);
1392 buf += n;
1393 } else if (column == 0)
1394 break;
1395 }
1396 return 0;
1397}
1398
1399/**
1400 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1401 * @param mtd MTD device structure
1402 * @param to offset to write to
1403 * @param ops oob operation description structure
1404 *
1405 * Write main and/or oob with ECC
1406 */
1407static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1408 struct mtd_oob_ops *ops)
1409{
1410 struct onenand_chip *this = mtd->priv;
1411 int written = 0, column, thislen, subpage;
1412 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1413 size_t len = ops->len;
1414 size_t ooblen = ops->ooblen;
1415 const u_char *buf = ops->datbuf;
1416 const u_char *oob = ops->oobbuf;
1417 u_char *oobbuf;
Kyungmin Park916527f2007-09-10 17:13:49 +09001418 int ret = 0;
1419
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001420 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001421
1422 /* Initialize retlen, in case of early exit */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001423 ops->retlen = 0;
1424 ops->oobretlen = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001425
Kyungmin Park916527f2007-09-10 17:13:49 +09001426 /* Reject writes, which are not page aligned */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001427 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1428 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001429 return -EINVAL;
1430 }
1431
Sergey Lapindfe64e22013-01-14 03:46:50 +00001432 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001433 oobsize = this->ecclayout->oobavail;
1434 else
1435 oobsize = mtd->oobsize;
1436
1437 oobcolumn = to & (mtd->oobsize - 1);
1438
1439 column = to & (mtd->writesize - 1);
Kyungmin Park916527f2007-09-10 17:13:49 +09001440
1441 /* Loop until all data write */
1442 while (written < len) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001443 u_char *wbuf = (u_char *) buf;
Kyungmin Park916527f2007-09-10 17:13:49 +09001444
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001445 thislen = min_t(int, mtd->writesize - column, len - written);
1446 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park916527f2007-09-10 17:13:49 +09001447
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001448 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1449
1450 /* Partial page write */
1451 subpage = thislen < mtd->writesize;
1452 if (subpage) {
1453 memset(this->page_buf, 0xff, mtd->writesize);
1454 memcpy(this->page_buf + column, buf, thislen);
1455 wbuf = this->page_buf;
1456 }
1457
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001458 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001459
1460 if (oob) {
1461 oobbuf = this->oob_buf;
1462
1463 /* We send data to spare ram with oobsize
1464 * * to prevent byte access */
1465 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001466 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001467 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1468 else
1469 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1470
1471 oobwritten += thisooblen;
1472 oob += thisooblen;
1473 oobcolumn = 0;
1474 } else
1475 oobbuf = (u_char *) ffchars;
1476
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001477 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001478
Kyungmin Parkd438d502008-08-13 09:11:02 +09001479 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park916527f2007-09-10 17:13:49 +09001480
Kyungmin Park916527f2007-09-10 17:13:49 +09001481 ret = this->wait(mtd, FL_WRITING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001482
1483 /* In partial page write we don't update bufferram */
1484 onenand_update_bufferram(mtd, to, !ret && !subpage);
1485 if (ONENAND_IS_2PLANE(this)) {
1486 ONENAND_SET_BUFFERRAM1(this);
1487 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1488 }
1489
Kyungmin Park916527f2007-09-10 17:13:49 +09001490 if (ret) {
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001491 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1492 break;
1493 }
1494
1495 /* Only check verify write turn on */
1496 ret = onenand_verify(mtd, buf, to, thislen);
1497 if (ret) {
1498 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park916527f2007-09-10 17:13:49 +09001499 break;
1500 }
1501
1502 written += thislen;
1503
Kyungmin Park916527f2007-09-10 17:13:49 +09001504 if (written == len)
1505 break;
1506
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001507 column = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001508 to += thislen;
1509 buf += thislen;
1510 }
1511
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001512 ops->retlen = written;
Kyungmin Park916527f2007-09-10 17:13:49 +09001513
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001514 return ret;
1515}
1516
1517/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001518 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001519 * @param mtd MTD device structure
1520 * @param to offset to write to
1521 * @param len number of bytes to write
1522 * @param retlen pointer to variable to store the number of written bytes
1523 * @param buf the data to write
1524 * @param mode operation mode
1525 *
1526 * OneNAND write out-of-band
1527 */
1528static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1529 struct mtd_oob_ops *ops)
1530{
1531 struct onenand_chip *this = mtd->priv;
1532 int column, ret = 0, oobsize;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301533 int written = 0, oobcmd;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001534 u_char *oobbuf;
1535 size_t len = ops->ooblen;
1536 const u_char *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001537 unsigned int mode = ops->mode;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001538
1539 to += ops->ooboffs;
1540
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001541 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001542
1543 /* Initialize retlen, in case of early exit */
1544 ops->oobretlen = 0;
1545
Sergey Lapindfe64e22013-01-14 03:46:50 +00001546 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001547 oobsize = this->ecclayout->oobavail;
1548 else
1549 oobsize = mtd->oobsize;
1550
1551 column = to & (mtd->oobsize - 1);
1552
1553 if (unlikely(column >= oobsize)) {
1554 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1555 return -EINVAL;
1556 }
1557
1558 /* For compatibility with NAND: Do not allow write past end of page */
1559 if (unlikely(column + len > oobsize)) {
1560 printk(KERN_ERR "onenand_write_oob_nolock: "
1561 "Attempt to write past end of page\n");
1562 return -EINVAL;
1563 }
1564
1565 /* Do not allow reads past end of device */
1566 if (unlikely(to >= mtd->size ||
1567 column + len > ((mtd->size >> this->page_shift) -
1568 (to >> this->page_shift)) * oobsize)) {
1569 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1570 return -EINVAL;
1571 }
1572
1573 oobbuf = this->oob_buf;
1574
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01001575 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1576 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301577
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001578 /* Loop until all data write */
1579 while (written < len) {
1580 int thislen = min_t(int, oobsize, len - written);
1581
1582 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1583
1584 /* We send data to spare ram with oobsize
1585 * to prevent byte access */
1586 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001587 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001588 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1589 else
1590 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001591 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001592
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01001593 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301594 /* Set main area of DataRAM to 0xff*/
1595 memset(this->page_buf, 0xff, mtd->writesize);
1596 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1597 this->page_buf, 0, mtd->writesize);
1598 }
1599
1600 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001601
1602 onenand_update_bufferram(mtd, to, 0);
1603 if (ONENAND_IS_2PLANE(this)) {
1604 ONENAND_SET_BUFFERRAM1(this);
1605 onenand_update_bufferram(mtd, to + this->writesize, 0);
1606 }
1607
1608 ret = this->wait(mtd, FL_WRITING);
1609 if (ret) {
1610 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1611 break;
1612 }
1613
1614 ret = onenand_verify_oob(mtd, oobbuf, to);
1615 if (ret) {
1616 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1617 break;
1618 }
1619
1620 written += thislen;
1621 if (written == len)
1622 break;
1623
1624 to += mtd->writesize;
1625 buf += thislen;
1626 column = 0;
1627 }
1628
1629 ops->oobretlen = written;
Kyungmin Park916527f2007-09-10 17:13:49 +09001630
1631 return ret;
1632}
1633
1634/**
1635 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1636 * @param mtd MTD device structure
1637 * @param to offset to write to
1638 * @param len number of bytes to write
1639 * @param retlen pointer to variable to store the number of written bytes
1640 * @param buf the data to write
1641 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001642 * Write with ECC
Kyungmin Park916527f2007-09-10 17:13:49 +09001643 */
1644int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1645 size_t * retlen, const u_char * buf)
1646{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001647 struct mtd_oob_ops ops = {
1648 .len = len,
1649 .ooblen = 0,
1650 .datbuf = (u_char *) buf,
1651 .oobbuf = NULL,
1652 };
1653 int ret;
1654
1655 onenand_get_device(mtd, FL_WRITING);
1656 ret = onenand_write_ops_nolock(mtd, to, &ops);
1657 onenand_release_device(mtd);
1658
1659 *retlen = ops.retlen;
1660 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001661}
1662
1663/**
1664 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1665 * @param mtd MTD device structure
1666 * @param to offset to write to
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001667 * @param ops oob operation description structure
Kyungmin Park916527f2007-09-10 17:13:49 +09001668 *
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001669 * OneNAND write main and/or out-of-band
Kyungmin Park916527f2007-09-10 17:13:49 +09001670 */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001671int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1672 struct mtd_oob_ops *ops)
Kyungmin Park916527f2007-09-10 17:13:49 +09001673{
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001674 int ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001675
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001676 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001677 case MTD_OPS_PLACE_OOB:
1678 case MTD_OPS_AUTO_OOB:
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001679 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001680 case MTD_OPS_RAW:
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001681 /* Not implemented yet */
1682 default:
Kyungmin Park916527f2007-09-10 17:13:49 +09001683 return -EINVAL;
1684 }
1685
Kyungmin Park916527f2007-09-10 17:13:49 +09001686 onenand_get_device(mtd, FL_WRITING);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001687 if (ops->datbuf)
1688 ret = onenand_write_ops_nolock(mtd, to, ops);
1689 else
1690 ret = onenand_write_oob_nolock(mtd, to, ops);
Kyungmin Park916527f2007-09-10 17:13:49 +09001691 onenand_release_device(mtd);
1692
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001693 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001694
Kyungmin Park916527f2007-09-10 17:13:49 +09001695}
1696
1697/**
Kyungmin Parkd438d502008-08-13 09:11:02 +09001698 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1699 * @param mtd MTD device structure
1700 * @param ofs offset from device start
1701 * @param allowbbt 1, if its allowed to access the bbt area
1702 *
1703 * Check, if the block is bad, Either by reading the bad block table or
1704 * calling of the scan function.
1705 */
1706static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1707{
1708 struct onenand_chip *this = mtd->priv;
1709 struct bbm_info *bbm = this->bbm;
1710
1711 /* Return info from the table */
1712 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1713}
1714
1715
1716/**
Kyungmin Park916527f2007-09-10 17:13:49 +09001717 * onenand_erase - [MTD Interface] erase block(s)
1718 * @param mtd MTD device structure
1719 * @param instr erase instruction
1720 *
1721 * Erase one ore more blocks
1722 */
1723int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1724{
1725 struct onenand_chip *this = mtd->priv;
1726 unsigned int block_size;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301727 loff_t addr = instr->addr;
1728 unsigned int len = instr->len;
1729 int ret = 0, i;
1730 struct mtd_erase_region_info *region = NULL;
1731 unsigned int region_end = 0;
Kyungmin Park916527f2007-09-10 17:13:49 +09001732
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301733 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1734 (unsigned int) addr, len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001735
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301736 if (FLEXONENAND(this)) {
1737 /* Find the eraseregion of this address */
1738 i = flexonenand_region(mtd, addr);
1739 region = &mtd->eraseregions[i];
1740
1741 block_size = region->erasesize;
1742 region_end = region->offset
1743 + region->erasesize * region->numblocks;
1744
1745 /* Start address within region must align on block boundary.
1746 * Erase region's start offset is always block start address.
1747 */
1748 if (unlikely((addr - region->offset) & (block_size - 1))) {
1749 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1750 " Unaligned address\n");
1751 return -EINVAL;
1752 }
1753 } else {
1754 block_size = 1 << this->erase_shift;
1755
1756 /* Start address must align on block boundary */
1757 if (unlikely(addr & (block_size - 1))) {
1758 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1759 "Unaligned address\n");
1760 return -EINVAL;
1761 }
1762 }
1763
Kyungmin Park916527f2007-09-10 17:13:49 +09001764 /* Length must align on block boundary */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301765 if (unlikely(len & (block_size - 1))) {
Scott Wood3167c532008-06-20 12:38:57 -05001766 MTDDEBUG (MTD_DEBUG_LEVEL0,
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09001767 "onenand_erase: Length not block aligned\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001768 return -EINVAL;
1769 }
1770
Kyungmin Park916527f2007-09-10 17:13:49 +09001771 /* Grab the lock and see if the device is available */
1772 onenand_get_device(mtd, FL_ERASING);
1773
1774 /* Loop throught the pages */
Kyungmin Park916527f2007-09-10 17:13:49 +09001775 instr->state = MTD_ERASING;
1776
1777 while (len) {
1778
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001779 /* Check if we have a bad block, we do not erase bad blocks */
1780 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1781 printk(KERN_WARNING "onenand_erase: attempt to erase"
1782 " a bad block at addr 0x%08x\n",
1783 (unsigned int) addr);
1784 instr->state = MTD_ERASE_FAILED;
1785 goto erase_exit;
1786 }
Kyungmin Park916527f2007-09-10 17:13:49 +09001787
1788 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1789
Kyungmin Parkd438d502008-08-13 09:11:02 +09001790 onenand_invalidate_bufferram(mtd, addr, block_size);
1791
Kyungmin Park916527f2007-09-10 17:13:49 +09001792 ret = this->wait(mtd, FL_ERASING);
1793 /* Check, if it is write protected */
1794 if (ret) {
1795 if (ret == -EPERM)
Scott Wood3167c532008-06-20 12:38:57 -05001796 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk4b070802008-08-14 14:41:06 +02001797 "Device is write protected!!!\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001798 else
Scott Wood3167c532008-06-20 12:38:57 -05001799 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
Wolfgang Denk4b070802008-08-14 14:41:06 +02001800 "Failed erase, block %d\n",
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301801 onenand_block(this, addr));
Kyungmin Park916527f2007-09-10 17:13:49 +09001802 instr->state = MTD_ERASE_FAILED;
1803 instr->fail_addr = addr;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001804
Kyungmin Park916527f2007-09-10 17:13:49 +09001805 goto erase_exit;
1806 }
1807
1808 len -= block_size;
1809 addr += block_size;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301810
1811 if (addr == region_end) {
1812 if (!len)
1813 break;
1814 region++;
1815
1816 block_size = region->erasesize;
1817 region_end = region->offset
1818 + region->erasesize * region->numblocks;
1819
1820 if (len & (block_size - 1)) {
1821 /* This has been checked at MTD
1822 * partitioning level. */
1823 printk("onenand_erase: Unaligned address\n");
1824 goto erase_exit;
1825 }
1826 }
Kyungmin Park916527f2007-09-10 17:13:49 +09001827 }
1828
1829 instr->state = MTD_ERASE_DONE;
1830
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001831erase_exit:
Kyungmin Park916527f2007-09-10 17:13:49 +09001832
1833 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1834 /* Do call back function */
1835 if (!ret)
1836 mtd_erase_callback(instr);
1837
1838 /* Deselect and wake up anyone waiting on the device */
1839 onenand_release_device(mtd);
1840
1841 return ret;
1842}
1843
1844/**
1845 * onenand_sync - [MTD Interface] sync
1846 * @param mtd MTD device structure
1847 *
1848 * Sync is actually a wait for chip ready function
1849 */
1850void onenand_sync(struct mtd_info *mtd)
1851{
Scott Wood3167c532008-06-20 12:38:57 -05001852 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
Kyungmin Park916527f2007-09-10 17:13:49 +09001853
1854 /* Grab the lock and see if the device is available */
1855 onenand_get_device(mtd, FL_SYNCING);
1856
1857 /* Release it and go back */
1858 onenand_release_device(mtd);
1859}
1860
1861/**
1862 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1863 * @param mtd MTD device structure
1864 * @param ofs offset relative to mtd start
Kyungmin Parkd438d502008-08-13 09:11:02 +09001865 *
1866 * Check whether the block is bad
Kyungmin Park916527f2007-09-10 17:13:49 +09001867 */
1868int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1869{
Kyungmin Parkd438d502008-08-13 09:11:02 +09001870 int ret;
1871
1872 /* Check for invalid offset */
1873 if (ofs > mtd->size)
1874 return -EINVAL;
1875
1876 onenand_get_device(mtd, FL_READING);
1877 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1878 onenand_release_device(mtd);
1879 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001880}
1881
1882/**
Kyungmin Park1714f512008-11-13 15:14:33 +09001883 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1884 * @param mtd MTD device structure
1885 * @param ofs offset from device start
1886 *
1887 * This is the default implementation, which can be overridden by
1888 * a hardware specific driver.
1889 */
1890static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1891{
1892 struct onenand_chip *this = mtd->priv;
1893 struct bbm_info *bbm = this->bbm;
1894 u_char buf[2] = {0, 0};
1895 struct mtd_oob_ops ops = {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001896 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park1714f512008-11-13 15:14:33 +09001897 .ooblen = 2,
1898 .oobbuf = buf,
1899 .ooboffs = 0,
1900 };
1901 int block;
1902
1903 /* Get block number */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301904 block = onenand_block(this, ofs);
Kyungmin Park1714f512008-11-13 15:14:33 +09001905 if (bbm->bbt)
1906 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1907
1908 /* We write two bytes, so we dont have to mess with 16 bit access */
1909 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1910 return onenand_write_oob_nolock(mtd, ofs, &ops);
1911}
1912
1913/**
Kyungmin Park916527f2007-09-10 17:13:49 +09001914 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1915 * @param mtd MTD device structure
1916 * @param ofs offset relative to mtd start
Kyungmin Parkd438d502008-08-13 09:11:02 +09001917 *
1918 * Mark the block as bad
Kyungmin Park916527f2007-09-10 17:13:49 +09001919 */
1920int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1921{
Kyungmin Parkd438d502008-08-13 09:11:02 +09001922 int ret;
1923
1924 ret = onenand_block_isbad(mtd, ofs);
1925 if (ret) {
1926 /* If it was bad already, return success and do nothing */
1927 if (ret > 0)
1928 return 0;
1929 return ret;
1930 }
1931
Sergey Lapindfe64e22013-01-14 03:46:50 +00001932 ret = mtd_block_markbad(mtd, ofs);
Kyungmin Parkd438d502008-08-13 09:11:02 +09001933 return ret;
Kyungmin Park916527f2007-09-10 17:13:49 +09001934}
1935
1936/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001937 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1938 * @param mtd MTD device structure
1939 * @param ofs offset relative to mtd start
1940 * @param len number of bytes to lock or unlock
1941 * @param cmd lock or unlock command
Kyungmin Park916527f2007-09-10 17:13:49 +09001942 *
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001943 * Lock or unlock one or more blocks
Kyungmin Park916527f2007-09-10 17:13:49 +09001944 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001945static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park916527f2007-09-10 17:13:49 +09001946{
1947 struct onenand_chip *this = mtd->priv;
1948 int start, end, block, value, status;
1949
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301950 start = onenand_block(this, ofs);
1951 end = onenand_block(this, ofs + len);
Kyungmin Park916527f2007-09-10 17:13:49 +09001952
1953 /* Continuous lock scheme */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001954 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park916527f2007-09-10 17:13:49 +09001955 /* Set start block address */
1956 this->write_word(start,
1957 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1958 /* Set end block address */
1959 this->write_word(end - 1,
1960 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1961 /* Write unlock command */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001962 this->command(mtd, cmd, 0, 0);
Kyungmin Park916527f2007-09-10 17:13:49 +09001963
1964 /* There's no return value */
1965 this->wait(mtd, FL_UNLOCKING);
1966
1967 /* Sanity check */
1968 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1969 & ONENAND_CTRL_ONGO)
1970 continue;
1971
1972 /* Check lock status */
1973 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1974 if (!(status & ONENAND_WP_US))
1975 printk(KERN_ERR "wp status = 0x%x\n", status);
1976
1977 return 0;
1978 }
1979
1980 /* Block lock scheme */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05301981 for (block = start; block < end; block++) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +09001982 /* Set block address */
1983 value = onenand_block_address(this, block);
1984 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1985 /* Select DataRAM for DDP */
1986 value = onenand_bufferram_address(this, block);
1987 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1988
Kyungmin Park916527f2007-09-10 17:13:49 +09001989 /* Set start block address */
1990 this->write_word(block,
1991 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1992 /* Write unlock command */
1993 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1994
1995 /* There's no return value */
1996 this->wait(mtd, FL_UNLOCKING);
1997
1998 /* Sanity check */
1999 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2000 & ONENAND_CTRL_ONGO)
2001 continue;
2002
Kyungmin Park916527f2007-09-10 17:13:49 +09002003 /* Check lock status */
2004 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2005 if (!(status & ONENAND_WP_US))
2006 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2007 block, status);
2008 }
2009
2010 return 0;
2011}
2012
Stefan Roese4fca3312008-11-11 10:28:53 +01002013#ifdef ONENAND_LINUX
Kyungmin Park916527f2007-09-10 17:13:49 +09002014/**
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002015 * onenand_lock - [MTD Interface] Lock block(s)
2016 * @param mtd MTD device structure
2017 * @param ofs offset relative to mtd start
2018 * @param len number of bytes to unlock
2019 *
2020 * Lock one or more blocks
2021 */
2022static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2023{
2024 int ret;
2025
2026 onenand_get_device(mtd, FL_LOCKING);
2027 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2028 onenand_release_device(mtd);
2029 return ret;
2030}
2031
2032/**
2033 * onenand_unlock - [MTD Interface] Unlock block(s)
2034 * @param mtd MTD device structure
2035 * @param ofs offset relative to mtd start
2036 * @param len number of bytes to unlock
2037 *
2038 * Unlock one or more blocks
2039 */
2040static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2041{
2042 int ret;
2043
2044 onenand_get_device(mtd, FL_LOCKING);
2045 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2046 onenand_release_device(mtd);
2047 return ret;
2048}
Stefan Roese4fca3312008-11-11 10:28:53 +01002049#endif
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002050
2051/**
2052 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2053 * @param this onenand chip data structure
2054 *
2055 * Check lock status
2056 */
2057static int onenand_check_lock_status(struct onenand_chip *this)
2058{
2059 unsigned int value, block, status;
2060 unsigned int end;
2061
2062 end = this->chipsize >> this->erase_shift;
2063 for (block = 0; block < end; block++) {
2064 /* Set block address */
2065 value = onenand_block_address(this, block);
2066 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2067 /* Select DataRAM for DDP */
2068 value = onenand_bufferram_address(this, block);
2069 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2070 /* Set start block address */
2071 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2072
2073 /* Check lock status */
2074 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2075 if (!(status & ONENAND_WP_US)) {
2076 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2077 return 0;
2078 }
2079 }
2080
2081 return 1;
2082}
2083
2084/**
2085 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2086 * @param mtd MTD device structure
2087 *
2088 * Unlock all blocks
2089 */
2090static void onenand_unlock_all(struct mtd_info *mtd)
2091{
2092 struct onenand_chip *this = mtd->priv;
2093 loff_t ofs = 0;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302094 size_t len = mtd->size;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002095
2096 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2097 /* Set start block address */
2098 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2099 /* Write unlock command */
2100 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2101
2102 /* There's no return value */
2103 this->wait(mtd, FL_LOCKING);
2104
2105 /* Sanity check */
2106 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2107 & ONENAND_CTRL_ONGO)
2108 continue;
2109
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002110 /* Check lock status */
2111 if (onenand_check_lock_status(this))
2112 return;
2113
2114 /* Workaround for all block unlock in DDP */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302115 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002116 /* All blocks on another chip */
2117 ofs = this->chipsize >> 1;
2118 len = this->chipsize >> 1;
2119 }
2120 }
2121
2122 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2123}
2124
2125
2126/**
2127 * onenand_check_features - Check and set OneNAND features
2128 * @param mtd MTD data structure
2129 *
2130 * Check and set OneNAND features
2131 * - lock scheme
2132 * - two plane
2133 */
2134static void onenand_check_features(struct mtd_info *mtd)
2135{
2136 struct onenand_chip *this = mtd->priv;
2137 unsigned int density, process;
2138
2139 /* Lock scheme depends on density and process */
2140 density = onenand_get_density(this->device_id);
2141 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2142
2143 /* Lock scheme */
2144 switch (density) {
2145 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01002146 if (ONENAND_IS_DDP(this))
2147 this->options |= ONENAND_HAS_2PLANE;
2148 else
2149 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002150
2151 case ONENAND_DEVICE_DENSITY_2Gb:
2152 /* 2Gb DDP don't have 2 plane */
2153 if (!ONENAND_IS_DDP(this))
2154 this->options |= ONENAND_HAS_2PLANE;
2155 this->options |= ONENAND_HAS_UNLOCK_ALL;
2156
2157 case ONENAND_DEVICE_DENSITY_1Gb:
2158 /* A-Die has all block unlock */
2159 if (process)
2160 this->options |= ONENAND_HAS_UNLOCK_ALL;
2161 break;
2162
2163 default:
2164 /* Some OneNAND has continuous lock scheme */
2165 if (!process)
2166 this->options |= ONENAND_HAS_CONT_LOCK;
2167 break;
2168 }
2169
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302170 if (ONENAND_IS_MLC(this))
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01002171 this->options |= ONENAND_HAS_4KB_PAGE;
2172
2173 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302174 this->options &= ~ONENAND_HAS_2PLANE;
2175
2176 if (FLEXONENAND(this)) {
2177 this->options &= ~ONENAND_HAS_CONT_LOCK;
2178 this->options |= ONENAND_HAS_UNLOCK_ALL;
2179 }
2180
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002181 if (this->options & ONENAND_HAS_CONT_LOCK)
2182 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2183 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2184 printk(KERN_DEBUG "Chip support all block unlock\n");
2185 if (this->options & ONENAND_HAS_2PLANE)
2186 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01002187 if (this->options & ONENAND_HAS_4KB_PAGE)
2188 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2189
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002190}
2191
2192/**
Kyungmin Park916527f2007-09-10 17:13:49 +09002193 * onenand_print_device_info - Print device ID
2194 * @param device device ID
2195 *
2196 * Print device ID
2197 */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002198char *onenand_print_device_info(int device, int version)
Kyungmin Park916527f2007-09-10 17:13:49 +09002199{
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302200 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002201 char *dev_info = malloc(80);
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002202 char *p = dev_info;
Kyungmin Park916527f2007-09-10 17:13:49 +09002203
2204 vcc = device & ONENAND_DEVICE_VCC_MASK;
2205 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2206 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302207 density = onenand_get_density(device);
2208 flexonenand = device & DEVICE_IS_FLEXONENAND;
2209 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park916527f2007-09-10 17:13:49 +09002210 demuxed ? "" : "Muxed ",
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302211 flexonenand ? "Flex-" : "",
Kyungmin Park916527f2007-09-10 17:13:49 +09002212 ddp ? "(DDP)" : "",
2213 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002214
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002215 sprintf(p, "\nOneNAND version = 0x%04x", version);
2216 printk("%s\n", dev_info);
2217
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002218 return dev_info;
Kyungmin Park916527f2007-09-10 17:13:49 +09002219}
2220
2221static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra456be172010-10-11 21:48:03 +02002222 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park916527f2007-09-10 17:13:49 +09002223 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park916527f2007-09-10 17:13:49 +09002224};
2225
2226/**
2227 * onenand_check_maf - Check manufacturer ID
2228 * @param manuf manufacturer ID
2229 *
2230 * Check manufacturer ID
2231 */
2232static int onenand_check_maf(int manuf)
2233{
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002234 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park916527f2007-09-10 17:13:49 +09002235 int i;
Marek Vasut24ccca52011-11-06 00:59:52 +01002236#ifdef ONENAND_DEBUG
2237 char *name;
2238#endif
Kyungmin Park916527f2007-09-10 17:13:49 +09002239
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302240 for (i = 0; i < size; i++)
Kyungmin Park916527f2007-09-10 17:13:49 +09002241 if (manuf == onenand_manuf_ids[i].id)
2242 break;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002243
Marek Vasut24ccca52011-11-06 00:59:52 +01002244#ifdef ONENAND_DEBUG
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002245 if (i < size)
2246 name = onenand_manuf_ids[i].name;
2247 else
2248 name = "Unknown";
Kyungmin Park916527f2007-09-10 17:13:49 +09002249
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002250 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park916527f2007-09-10 17:13:49 +09002251#endif
2252
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002253 return i == size;
Kyungmin Park916527f2007-09-10 17:13:49 +09002254}
2255
2256/**
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302257* flexonenand_get_boundary - Reads the SLC boundary
2258* @param onenand_info - onenand info structure
2259*
2260* Fill up boundary[] field in onenand_chip
2261**/
2262static int flexonenand_get_boundary(struct mtd_info *mtd)
2263{
2264 struct onenand_chip *this = mtd->priv;
2265 unsigned int die, bdry;
Marek Vasut24ccca52011-11-06 00:59:52 +01002266 int syscfg, locked;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302267
2268 /* Disable ECC */
2269 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2270 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2271
2272 for (die = 0; die < this->dies; die++) {
2273 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2274 this->wait(mtd, FL_SYNCING);
2275
2276 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasut24ccca52011-11-06 00:59:52 +01002277 this->wait(mtd, FL_READING);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302278
2279 bdry = this->read_word(this->base + ONENAND_DATARAM);
2280 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2281 locked = 0;
2282 else
2283 locked = 1;
2284 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2285
2286 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasut24ccca52011-11-06 00:59:52 +01002287 this->wait(mtd, FL_RESETING);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302288
2289 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2290 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2291 }
2292
2293 /* Enable ECC */
2294 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2295 return 0;
2296}
2297
2298/**
2299 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2300 * boundary[], diesize[], mtd->size, mtd->erasesize,
2301 * mtd->eraseregions
2302 * @param mtd - MTD device structure
2303 */
2304static void flexonenand_get_size(struct mtd_info *mtd)
2305{
2306 struct onenand_chip *this = mtd->priv;
2307 int die, i, eraseshift, density;
2308 int blksperdie, maxbdry;
2309 loff_t ofs;
2310
2311 density = onenand_get_density(this->device_id);
2312 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2313 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2314 maxbdry = blksperdie - 1;
2315 eraseshift = this->erase_shift - 1;
2316
2317 mtd->numeraseregions = this->dies << 1;
2318
2319 /* This fills up the device boundary */
2320 flexonenand_get_boundary(mtd);
2321 die = 0;
2322 ofs = 0;
2323 i = -1;
2324 for (; die < this->dies; die++) {
2325 if (!die || this->boundary[die-1] != maxbdry) {
2326 i++;
2327 mtd->eraseregions[i].offset = ofs;
2328 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2329 mtd->eraseregions[i].numblocks =
2330 this->boundary[die] + 1;
2331 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2332 eraseshift++;
2333 } else {
2334 mtd->numeraseregions -= 1;
2335 mtd->eraseregions[i].numblocks +=
2336 this->boundary[die] + 1;
2337 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2338 }
2339 if (this->boundary[die] != maxbdry) {
2340 i++;
2341 mtd->eraseregions[i].offset = ofs;
2342 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2343 mtd->eraseregions[i].numblocks = maxbdry ^
2344 this->boundary[die];
2345 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2346 eraseshift--;
2347 } else
2348 mtd->numeraseregions -= 1;
2349 }
2350
2351 /* Expose MLC erase size except when all blocks are SLC */
2352 mtd->erasesize = 1 << this->erase_shift;
2353 if (mtd->numeraseregions == 1)
2354 mtd->erasesize >>= 1;
2355
2356 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2357 for (i = 0; i < mtd->numeraseregions; i++)
2358 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2359 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2360 mtd->eraseregions[i].erasesize,
2361 mtd->eraseregions[i].numblocks);
2362
2363 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2364 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2365 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2366 << (this->erase_shift - 1);
2367 mtd->size += this->diesize[die];
2368 }
2369}
2370
2371/**
2372 * flexonenand_check_blocks_erased - Check if blocks are erased
2373 * @param mtd_info - mtd info structure
2374 * @param start - first erase block to check
2375 * @param end - last erase block to check
2376 *
2377 * Converting an unerased block from MLC to SLC
2378 * causes byte values to change. Since both data and its ECC
2379 * have changed, reads on the block give uncorrectable error.
2380 * This might lead to the block being detected as bad.
2381 *
2382 * Avoid this by ensuring that the block to be converted is
2383 * erased.
2384 */
2385static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2386 int start, int end)
2387{
2388 struct onenand_chip *this = mtd->priv;
2389 int i, ret;
2390 int block;
2391 struct mtd_oob_ops ops = {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002392 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302393 .ooboffs = 0,
2394 .ooblen = mtd->oobsize,
2395 .datbuf = NULL,
2396 .oobbuf = this->oob_buf,
2397 };
2398 loff_t addr;
2399
2400 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2401
2402 for (block = start; block <= end; block++) {
2403 addr = flexonenand_addr(this, block);
2404 if (onenand_block_isbad_nolock(mtd, addr, 0))
2405 continue;
2406
2407 /*
2408 * Since main area write results in ECC write to spare,
2409 * it is sufficient to check only ECC bytes for change.
2410 */
2411 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2412 if (ret)
2413 return ret;
2414
2415 for (i = 0; i < mtd->oobsize; i++)
2416 if (this->oob_buf[i] != 0xff)
2417 break;
2418
2419 if (i != mtd->oobsize) {
2420 printk(KERN_WARNING "Block %d not erased.\n", block);
2421 return 1;
2422 }
2423 }
2424
2425 return 0;
2426}
2427
2428/**
2429 * flexonenand_set_boundary - Writes the SLC boundary
2430 * @param mtd - mtd info structure
2431 */
2432int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2433 int boundary, int lock)
2434{
2435 struct onenand_chip *this = mtd->priv;
2436 int ret, density, blksperdie, old, new, thisboundary;
2437 loff_t addr;
2438
2439 if (die >= this->dies)
2440 return -EINVAL;
2441
2442 if (boundary == this->boundary[die])
2443 return 0;
2444
2445 density = onenand_get_density(this->device_id);
2446 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2447 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2448
2449 if (boundary >= blksperdie) {
2450 printk("flexonenand_set_boundary:"
2451 "Invalid boundary value. "
2452 "Boundary not changed.\n");
2453 return -EINVAL;
2454 }
2455
2456 /* Check if converting blocks are erased */
2457 old = this->boundary[die] + (die * this->density_mask);
2458 new = boundary + (die * this->density_mask);
2459 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2460 + 1, max(old, new));
2461 if (ret) {
2462 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2463 return ret;
2464 }
2465
2466 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2467 this->wait(mtd, FL_SYNCING);
2468
2469 /* Check is boundary is locked */
2470 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2471 ret = this->wait(mtd, FL_READING);
2472
2473 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2474 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2475 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2476 goto out;
2477 }
2478
2479 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2480 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2481
2482 boundary &= FLEXONENAND_PI_MASK;
2483 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2484
2485 addr = die ? this->diesize[0] : 0;
2486 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2487 ret = this->wait(mtd, FL_ERASING);
2488 if (ret) {
2489 printk("flexonenand_set_boundary:"
2490 "Failed PI erase for Die %d\n", die);
2491 goto out;
2492 }
2493
2494 this->write_word(boundary, this->base + ONENAND_DATARAM);
2495 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2496 ret = this->wait(mtd, FL_WRITING);
2497 if (ret) {
2498 printk("flexonenand_set_boundary:"
2499 "Failed PI write for Die %d\n", die);
2500 goto out;
2501 }
2502
2503 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2504 ret = this->wait(mtd, FL_WRITING);
2505out:
2506 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2507 this->wait(mtd, FL_RESETING);
2508 if (!ret)
2509 /* Recalculate device size on boundary change*/
2510 flexonenand_get_size(mtd);
2511
2512 return ret;
2513}
2514
2515/**
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002516 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park916527f2007-09-10 17:13:49 +09002517 * @param mtd MTD device structure
2518 *
2519 * OneNAND detection method:
2520 * Compare the the values from command with ones from register
2521 */
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002522static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park916527f2007-09-10 17:13:49 +09002523{
2524 struct onenand_chip *this = mtd->priv;
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002525 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002526 int syscfg;
2527
2528 /* Save system configuration 1 */
2529 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002530
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002531 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002532 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2533 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park916527f2007-09-10 17:13:49 +09002534
2535 /* Send the command for reading device ID from BootRAM */
2536 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2537
2538 /* Read manufacturer and device IDs from BootRAM */
2539 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2540 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2541
Kyungmin Park916527f2007-09-10 17:13:49 +09002542 /* Reset OneNAND to read default register values */
2543 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2544
Kyungmin Parkd438d502008-08-13 09:11:02 +09002545 /* Wait reset */
Ladislav Michld9098ee2016-07-12 20:28:19 +02002546 if (this->wait(mtd, FL_RESETING))
2547 return -ENXIO;
Kyungmin Park916527f2007-09-10 17:13:49 +09002548
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002549 /* Restore system configuration 1 */
2550 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2551
2552 /* Check manufacturer ID */
2553 if (onenand_check_maf(bram_maf_id))
2554 return -ENXIO;
2555
Kyungmin Park916527f2007-09-10 17:13:49 +09002556 /* Read manufacturer and device IDs from Register */
2557 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2558 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2559
2560 /* Check OneNAND device */
2561 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2562 return -ENXIO;
2563
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002564 return 0;
2565}
2566
2567/**
2568 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2569 * @param mtd MTD device structure
2570 *
2571 * OneNAND detection method:
2572 * Compare the the values from command with ones from register
2573 */
2574int onenand_probe(struct mtd_info *mtd)
2575{
2576 struct onenand_chip *this = mtd->priv;
Wolfgang Denk1432c762012-04-19 01:14:17 +00002577 int dev_id, ver_id;
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002578 int density;
2579 int ret;
2580
2581 ret = this->chip_probe(mtd);
2582 if (ret)
2583 return ret;
2584
Wolfgang Denk1432c762012-04-19 01:14:17 +00002585 /* Read device IDs from Register */
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002586 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2587 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2588 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2589
Kyungmin Park916527f2007-09-10 17:13:49 +09002590 /* Flash device information */
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002591 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park916527f2007-09-10 17:13:49 +09002592 this->device_id = dev_id;
Stefan Roese8cf11f32008-11-11 10:29:09 +01002593 this->version_id = ver_id;
Kyungmin Park916527f2007-09-10 17:13:49 +09002594
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01002595 /* Check OneNAND features */
2596 onenand_check_features(mtd);
2597
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002598 density = onenand_get_density(dev_id);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302599 if (FLEXONENAND(this)) {
2600 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2601 /* Maximum possible erase regions */
2602 mtd->numeraseregions = this->dies << 1;
2603 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2604 * (this->dies << 1));
2605 if (!mtd->eraseregions)
2606 return -ENOMEM;
2607 }
2608
2609 /*
2610 * For Flex-OneNAND, chipsize represents maximum possible device size.
2611 * mtd->size represents the actual device size.
2612 */
Kyungmin Park916527f2007-09-10 17:13:49 +09002613 this->chipsize = (16 << density) << 20;
2614
2615 /* OneNAND page size & block size */
2616 /* The data buffer size is equal to page size */
Kyungmin Parkd438d502008-08-13 09:11:02 +09002617 mtd->writesize =
Kyungmin Park916527f2007-09-10 17:13:49 +09002618 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302619 /* We use the full BufferRAM */
Lukasz Majewskie26fd3d2011-11-09 10:30:06 +01002620 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302621 mtd->writesize <<= 1;
2622
Kyungmin Parkd438d502008-08-13 09:11:02 +09002623 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park916527f2007-09-10 17:13:49 +09002624 /* Pagers per block is always 64 in OneNAND */
Kyungmin Parkd438d502008-08-13 09:11:02 +09002625 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302626 /*
2627 * Flex-OneNAND SLC area has 64 pages per block.
2628 * Flex-OneNAND MLC area has 128 pages per block.
2629 * Expose MLC erase size to find erase_shift and page_mask.
2630 */
2631 if (FLEXONENAND(this))
2632 mtd->erasesize <<= 1;
Kyungmin Park916527f2007-09-10 17:13:49 +09002633
2634 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Parkd438d502008-08-13 09:11:02 +09002635 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park916527f2007-09-10 17:13:49 +09002636 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Parkd438d502008-08-13 09:11:02 +09002637 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302638 /* Set density mask. it is used for DDP */
2639 if (ONENAND_IS_DDP(this))
2640 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09002641 /* It's real page size */
2642 this->writesize = mtd->writesize;
Kyungmin Park916527f2007-09-10 17:13:49 +09002643
2644 /* REVIST: Multichip handling */
2645
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302646 if (FLEXONENAND(this))
2647 flexonenand_get_size(mtd);
2648 else
2649 mtd->size = this->chipsize;
Kyungmin Park916527f2007-09-10 17:13:49 +09002650
Kyungmin Parkd438d502008-08-13 09:11:02 +09002651 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002652 mtd->_erase = onenand_erase;
2653 mtd->_read = onenand_read;
2654 mtd->_write = onenand_write;
2655 mtd->_read_oob = onenand_read_oob;
2656 mtd->_write_oob = onenand_write_oob;
2657 mtd->_sync = onenand_sync;
2658 mtd->_block_isbad = onenand_block_isbad;
2659 mtd->_block_markbad = onenand_block_markbad;
Fathi BOUDRA195ccfc2008-08-06 10:06:20 +02002660
Kyungmin Park916527f2007-09-10 17:13:49 +09002661 return 0;
2662}
2663
2664/**
2665 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2666 * @param mtd MTD device structure
2667 * @param maxchips Number of chips to scan for
2668 *
2669 * This fills out all the not initialized function pointers
2670 * with the defaults.
2671 * The flash ID is read and the mtd/chip structures are
2672 * filled with the appropriate values.
2673 */
2674int onenand_scan(struct mtd_info *mtd, int maxchips)
2675{
Stefan Roese1ae39862008-12-02 11:06:47 +01002676 int i;
Kyungmin Park916527f2007-09-10 17:13:49 +09002677 struct onenand_chip *this = mtd->priv;
2678
2679 if (!this->read_word)
2680 this->read_word = onenand_readw;
2681 if (!this->write_word)
2682 this->write_word = onenand_writew;
2683
2684 if (!this->command)
2685 this->command = onenand_command;
2686 if (!this->wait)
2687 this->wait = onenand_wait;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002688 if (!this->bbt_wait)
2689 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park916527f2007-09-10 17:13:49 +09002690
2691 if (!this->read_bufferram)
2692 this->read_bufferram = onenand_read_bufferram;
2693 if (!this->write_bufferram)
2694 this->write_bufferram = onenand_write_bufferram;
2695
Lukasz Majewski6b3967b2011-11-09 11:25:32 +01002696 if (!this->chip_probe)
2697 this->chip_probe = onenand_chip_probe;
2698
Kyungmin Park1714f512008-11-13 15:14:33 +09002699 if (!this->block_markbad)
2700 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002701 if (!this->scan_bbt)
2702 this->scan_bbt = onenand_default_bbt;
2703
Kyungmin Park916527f2007-09-10 17:13:49 +09002704 if (onenand_probe(mtd))
2705 return -ENXIO;
2706
2707 /* Set Sync. Burst Read after probing */
2708 if (this->mmcontrol) {
2709 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2710 this->read_bufferram = onenand_sync_read_bufferram;
2711 }
2712
Kyungmin Parkbfd7f382008-08-19 08:42:53 +09002713 /* Allocate buffers, if necessary */
2714 if (!this->page_buf) {
2715 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2716 if (!this->page_buf) {
2717 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2718 return -ENOMEM;
2719 }
2720 this->options |= ONENAND_PAGEBUF_ALLOC;
2721 }
2722 if (!this->oob_buf) {
2723 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2724 if (!this->oob_buf) {
2725 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2726 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2727 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2728 kfree(this->page_buf);
2729 }
2730 return -ENOMEM;
2731 }
2732 this->options |= ONENAND_OOBBUF_ALLOC;
2733 }
2734
Stefan Roese1ae39862008-12-02 11:06:47 +01002735 this->state = FL_READY;
2736
2737 /*
2738 * Allow subpage writes up to oobsize.
2739 */
2740 switch (mtd->oobsize) {
Amul Kumar Sahacacbe912009-11-06 17:15:31 +05302741 case 128:
2742 this->ecclayout = &onenand_oob_128;
2743 mtd->subpage_sft = 0;
2744 break;
2745
Stefan Roese1ae39862008-12-02 11:06:47 +01002746 case 64:
2747 this->ecclayout = &onenand_oob_64;
2748 mtd->subpage_sft = 2;
2749 break;
2750
2751 case 32:
2752 this->ecclayout = &onenand_oob_32;
2753 mtd->subpage_sft = 1;
2754 break;
2755
2756 default:
2757 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2758 mtd->oobsize);
2759 mtd->subpage_sft = 0;
2760 /* To prevent kernel oops */
2761 this->ecclayout = &onenand_oob_32;
2762 break;
2763 }
2764
2765 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2766
2767 /*
2768 * The number of bytes available for a client to place data into
2769 * the out of band area
2770 */
2771 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha68ec9c82013-10-04 13:47:58 +05302772
2773 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roese1ae39862008-12-02 11:06:47 +01002774 this->ecclayout->oobfree[i].length; i++)
2775 this->ecclayout->oobavail +=
2776 this->ecclayout->oobfree[i].length;
2777 mtd->oobavail = this->ecclayout->oobavail;
2778
2779 mtd->ecclayout = this->ecclayout;
2780
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002781 /* Unlock whole block */
2782 onenand_unlock_all(mtd);
Kyungmin Park916527f2007-09-10 17:13:49 +09002783
Kyungmin Parkef0921d2008-11-04 09:24:07 +09002784 return this->scan_bbt(mtd);
Kyungmin Park916527f2007-09-10 17:13:49 +09002785}
2786
2787/**
2788 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2789 * @param mtd MTD device structure
2790 */
2791void onenand_release(struct mtd_info *mtd)
2792{
2793}