blob: 0580b509f898e13fc629d223e5330a4643222c67 [file] [log] [blame]
Kyungmin Park79e90462007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053012 * Rohit Hagargundgi <h.rohit at samsung.com>,
13 * Amul Kumar Saha <amul.saha@samsung.com>:
14 * Flex-OneNAND support
15 * Copyright (C) Samsung Electronics, 2009
16 *
Kyungmin Park79e90462007-09-10 17:13:49 +090017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Ladislav Michl243af2f2016-07-12 20:28:19 +020024#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <dm/devres.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000026#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090027#include <linux/mtd/mtd.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020028#include "linux/mtd/flashchip.h"
Kyungmin Park79e90462007-09-10 17:13:49 +090029#include <linux/mtd/onenand.h>
30
31#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090032#include <linux/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020033#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090034
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090035/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053036static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090037{
38 void *ret = dst;
39 short *d = dst;
40 const short *s = src;
41
42 len >>= 1;
43 while (len-- > 0)
44 *d++ = *s++;
45 return ret;
46}
47
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010048/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053049 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
50 * For now, we expose only 64 out of 80 ecc bytes
51 */
52static struct nand_ecclayout onenand_oob_128 = {
53 .eccbytes = 64,
54 .eccpos = {
55 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
56 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
57 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
58 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
59 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
60 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
61 102, 103, 104, 105
62 },
63 .oobfree = {
64 {2, 4}, {18, 4}, {34, 4}, {50, 4},
65 {66, 4}, {82, 4}, {98, 4}, {114, 4}
66 }
67};
68
69/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010070 * onenand_oob_64 - oob info for large (2KB) page
71 */
72static struct nand_ecclayout onenand_oob_64 = {
73 .eccbytes = 20,
74 .eccpos = {
75 8, 9, 10, 11, 12,
76 24, 25, 26, 27, 28,
77 40, 41, 42, 43, 44,
78 56, 57, 58, 59, 60,
79 },
80 .oobfree = {
81 {2, 3}, {14, 2}, {18, 3}, {30, 2},
82 {34, 3}, {46, 2}, {50, 3}, {62, 2}
83 }
84};
85
86/**
87 * onenand_oob_32 - oob info for middle (1KB) page
88 */
89static struct nand_ecclayout onenand_oob_32 = {
90 .eccbytes = 10,
91 .eccpos = {
92 8, 9, 10, 11, 12,
93 24, 25, 26, 27, 28,
94 },
95 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
96};
97
Marek Vasut41120122013-12-26 01:01:24 +010098/*
99 * Warning! This array is used with the memcpy_16() function, thus
100 * it must be aligned to 2 bytes. GCC can make this array unaligned
101 * as the array is made of unsigned char, which memcpy16() doesn't
102 * like and will cause unaligned access.
103 */
104static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park79e90462007-09-10 17:13:49 +0900105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900121};
122
123/**
124 * onenand_readw - [OneNAND Interface] Read OneNAND register
125 * @param addr address to read
126 *
127 * Read OneNAND register
128 */
129static unsigned short onenand_readw(void __iomem * addr)
130{
131 return readw(addr);
132}
133
134/**
135 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
136 * @param value value to write
137 * @param addr address to write
138 *
139 * Write OneNAND register with value
140 */
141static void onenand_writew(unsigned short value, void __iomem * addr)
142{
143 writew(value, addr);
144}
145
146/**
147 * onenand_block_address - [DEFAULT] Get block address
148 * @param device the device id
149 * @param block the block
150 * @return translated block address if DDP, otherwise same
151 *
152 * Setup Start Address 1 Register (F100h)
153 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900154static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900155{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900156 /* Device Flash Core select, NAND Flash Block Address */
157 if (block & this->density_mask)
158 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900159
160 return block;
161}
162
163/**
164 * onenand_bufferram_address - [DEFAULT] Get bufferram address
165 * @param device the device id
166 * @param block the block
167 * @return set DBS value if DDP, otherwise 0
168 *
169 * Setup Start Address 2 Register (F101h) for DDP
170 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900171static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900172{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900173 /* Device BufferRAM Select */
174 if (block & this->density_mask)
175 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900176
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900177 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900178}
179
180/**
181 * onenand_page_address - [DEFAULT] Get page address
182 * @param page the page address
183 * @param sector the sector address
184 * @return combined page and sector address
185 *
186 * Setup Start Address 8 Register (F107h)
187 */
188static int onenand_page_address(int page, int sector)
189{
190 /* Flash Page Address, Flash Sector Address */
191 int fpa, fsa;
192
193 fpa = page & ONENAND_FPA_MASK;
194 fsa = sector & ONENAND_FSA_MASK;
195
196 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
197}
198
199/**
200 * onenand_buffer_address - [DEFAULT] Get buffer address
201 * @param dataram1 DataRAM index
202 * @param sectors the sector address
203 * @param count the number of sectors
204 * @return the start buffer value
205 *
206 * Setup Start Buffer Register (F200h)
207 */
208static int onenand_buffer_address(int dataram1, int sectors, int count)
209{
210 int bsa, bsc;
211
212 /* BufferRAM Sector Address */
213 bsa = sectors & ONENAND_BSA_MASK;
214
215 if (dataram1)
216 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
217 else
218 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
219
220 /* BufferRAM Sector Count */
221 bsc = count & ONENAND_BSC_MASK;
222
223 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
224}
225
226/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530227 * flexonenand_block - Return block number for flash address
228 * @param this - OneNAND device structure
229 * @param addr - Address for which block number is needed
230 */
231static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
232{
233 unsigned int boundary, blk, die = 0;
234
235 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
236 die = 1;
237 addr -= this->diesize[0];
238 }
239
240 boundary = this->boundary[die];
241
242 blk = addr >> (this->erase_shift - 1);
243 if (blk > boundary)
244 blk = (blk + boundary + 1) >> 1;
245
246 blk += die ? this->density_mask : 0;
247 return blk;
248}
249
250unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
251{
252 if (!FLEXONENAND(this))
253 return addr >> this->erase_shift;
254 return flexonenand_block(this, addr);
255}
256
257/**
258 * flexonenand_addr - Return address of the block
259 * @this: OneNAND device structure
260 * @block: Block number on Flex-OneNAND
261 *
262 * Return address of the block
263 */
264static loff_t flexonenand_addr(struct onenand_chip *this, int block)
265{
266 loff_t ofs = 0;
267 int die = 0, boundary;
268
269 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
270 block -= this->density_mask;
271 die = 1;
272 ofs = this->diesize[0];
273 }
274
275 boundary = this->boundary[die];
276 ofs += (loff_t) block << (this->erase_shift - 1);
277 if (block > (boundary + 1))
278 ofs += (loff_t) (block - boundary - 1)
279 << (this->erase_shift - 1);
280 return ofs;
281}
282
283loff_t onenand_addr(struct onenand_chip *this, int block)
284{
285 if (!FLEXONENAND(this))
286 return (loff_t) block << this->erase_shift;
287 return flexonenand_addr(this, block);
288}
289
290/**
291 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
292 * @param mtd MTD device structure
293 * @param addr address whose erase region needs to be identified
294 */
295int flexonenand_region(struct mtd_info *mtd, loff_t addr)
296{
297 int i;
298
299 for (i = 0; i < mtd->numeraseregions; i++)
300 if (addr < mtd->eraseregions[i].offset)
301 break;
302 return i - 1;
303}
304
305/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900306 * onenand_get_density - [DEFAULT] Get OneNAND density
307 * @param dev_id OneNAND device ID
308 *
309 * Get OneNAND density from device ID
310 */
311static inline int onenand_get_density(int dev_id)
312{
313 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
314 return (density & ONENAND_DEVICE_DENSITY_MASK);
315}
316
317/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900318 * onenand_command - [DEFAULT] Send command to OneNAND device
319 * @param mtd MTD device structure
320 * @param cmd the command to be sent
321 * @param addr offset to read from or write to
322 * @param len number of bytes to read or write
323 *
324 * Send command to OneNAND device. This function is used for middle/large page
325 * devices (1KB/2KB Bytes per page)
326 */
327static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
328 size_t len)
329{
330 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530331 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900332 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530333
Kyungmin Park79e90462007-09-10 17:13:49 +0900334 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530335 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900336
337 /* Address translation */
338 switch (cmd) {
339 case ONENAND_CMD_UNLOCK:
340 case ONENAND_CMD_LOCK:
341 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900342 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900343 block = -1;
344 page = -1;
345 break;
346
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530347 case FLEXONENAND_CMD_PI_ACCESS:
348 /* addr contains die index */
349 block = addr * this->density_mask;
350 page = -1;
351 break;
352
Kyungmin Park79e90462007-09-10 17:13:49 +0900353 case ONENAND_CMD_ERASE:
354 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530355 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900356 page = -1;
357 break;
358
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530359 case FLEXONENAND_CMD_READ_PI:
360 cmd = ONENAND_CMD_READ;
361 block = addr * this->density_mask;
362 page = 0;
363 break;
364
Kyungmin Park79e90462007-09-10 17:13:49 +0900365 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530366 block = onenand_block(this, addr);
367 page = (int) (addr
368 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900369 page &= this->page_mask;
370 break;
371 }
372
373 /* NOTE: The setting order of the registers is very important! */
374 if (cmd == ONENAND_CMD_BUFFERRAM) {
375 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900376 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900377 this->write_word(value,
378 this->base + ONENAND_REG_START_ADDRESS2);
379
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100380 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530381 ONENAND_SET_BUFFERRAM0(this);
382 else
383 /* Switch to the next data buffer */
384 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900385
386 return 0;
387 }
388
389 if (block != -1) {
390 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900391 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900392 this->write_word(value,
393 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900394
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530395 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900396 value = onenand_bufferram_address(this, block);
397 this->write_word(value,
398 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900399 }
400
401 if (page != -1) {
402 int dataram;
403
404 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530405 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900406 case ONENAND_CMD_READ:
407 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100408 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530409 dataram = ONENAND_SET_BUFFERRAM0(this);
410 else
411 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
412
Kyungmin Park79e90462007-09-10 17:13:49 +0900413 break;
414
415 default:
416 dataram = ONENAND_CURRENT_BUFFERRAM(this);
417 break;
418 }
419
420 /* Write 'FPA, FSA' of Flash */
421 value = onenand_page_address(page, sectors);
422 this->write_word(value,
423 this->base + ONENAND_REG_START_ADDRESS8);
424
425 /* Write 'BSA, BSC' of DataRAM */
426 value = onenand_buffer_address(dataram, sectors, count);
427 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900428 }
429
430 /* Interrupt clear */
431 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
432 /* Write command */
433 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
434
435 return 0;
436}
437
438/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530439 * onenand_read_ecc - return ecc status
440 * @param this onenand chip structure
441 */
442static int onenand_read_ecc(struct onenand_chip *this)
443{
444 int ecc, i;
445
446 if (!FLEXONENAND(this))
447 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
448
449 for (i = 0; i < 4; i++) {
450 ecc = this->read_word(this->base
451 + ((ONENAND_REG_ECC_STATUS + i) << 1));
452 if (likely(!ecc))
453 continue;
454 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
455 return ONENAND_ECC_2BIT_ALL;
456 }
457
458 return 0;
459}
460
461/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900462 * onenand_wait - [DEFAULT] wait until the command is done
463 * @param mtd MTD device structure
464 * @param state state to select the max. timeout value
465 *
466 * Wait for command done. This applies to all OneNAND command
467 * Read can take up to 30us, erase up to 2ms and program up to 350us
468 * according to general OneNAND specs
469 */
470static int onenand_wait(struct mtd_info *mtd, int state)
471{
472 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +0900473 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530474 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900475
Ladislav Michl243af2f2016-07-12 20:28:19 +0200476 /* Wait at most 20ms ... */
477 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
478 u32 time_start = get_timer(0);
479 do {
480 WATCHDOG_RESET();
481 if (get_timer(time_start) > timeo)
482 return -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +0900483 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +0200484 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park79e90462007-09-10 17:13:49 +0900485
486 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
487
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530488 if (interrupt & ONENAND_INT_READ) {
489 int ecc = onenand_read_ecc(this);
490 if (ecc & ONENAND_ECC_2BIT_ALL) {
491 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
492 return -EBADMSG;
493 }
494 }
495
Kyungmin Park79e90462007-09-10 17:13:49 +0900496 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900497 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
498 if (ctrl & ONENAND_CTRL_LOCK)
499 printk("onenand_wait: it's locked error = 0x%04x\n",
500 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900501
Kyungmin Park79e90462007-09-10 17:13:49 +0900502 return -EIO;
503 }
504
Kyungmin Park79e90462007-09-10 17:13:49 +0900505
506 return 0;
507}
508
509/**
510 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
511 * @param mtd MTD data structure
512 * @param area BufferRAM area
513 * @return offset given area
514 *
515 * Return BufferRAM offset given area
516 */
517static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
518{
519 struct onenand_chip *this = mtd->priv;
520
521 if (ONENAND_CURRENT_BUFFERRAM(this)) {
522 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900523 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900524 if (area == ONENAND_SPARERAM)
525 return mtd->oobsize;
526 }
527
528 return 0;
529}
530
531/**
532 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
533 * @param mtd MTD data structure
534 * @param area BufferRAM area
535 * @param buffer the databuffer to put/get data
536 * @param offset offset to read from or write to
537 * @param count number of bytes to read/write
538 *
539 * Read the BufferRAM area
540 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900541static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900542 unsigned char *buffer, int offset,
543 size_t count)
544{
545 struct onenand_chip *this = mtd->priv;
546 void __iomem *bufferram;
547
548 bufferram = this->base + area;
549 bufferram += onenand_bufferram_offset(mtd, area);
550
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200551 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900552
553 return 0;
554}
555
556/**
557 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
558 * @param mtd MTD data structure
559 * @param area BufferRAM area
560 * @param buffer the databuffer to put/get data
561 * @param offset offset to read from or write to
562 * @param count number of bytes to read/write
563 *
564 * Read the BufferRAM area with Sync. Burst Mode
565 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900566static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900567 unsigned char *buffer, int offset,
568 size_t count)
569{
570 struct onenand_chip *this = mtd->priv;
571 void __iomem *bufferram;
572
573 bufferram = this->base + area;
574 bufferram += onenand_bufferram_offset(mtd, area);
575
576 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
577
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200578 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900579
580 this->mmcontrol(mtd, 0);
581
582 return 0;
583}
584
585/**
586 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
587 * @param mtd MTD data structure
588 * @param area BufferRAM area
589 * @param buffer the databuffer to put/get data
590 * @param offset offset to read from or write to
591 * @param count number of bytes to read/write
592 *
593 * Write the BufferRAM area
594 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900595static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900596 const unsigned char *buffer, int offset,
597 size_t count)
598{
599 struct onenand_chip *this = mtd->priv;
600 void __iomem *bufferram;
601
602 bufferram = this->base + area;
603 bufferram += onenand_bufferram_offset(mtd, area);
604
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200605 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900606
607 return 0;
608}
609
610/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100611 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
612 * @param mtd MTD data structure
613 * @param addr address to check
614 * @return blockpage address
615 *
616 * Get blockpage address at 2x program mode
617 */
618static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
619{
620 struct onenand_chip *this = mtd->priv;
621 int blockpage, block, page;
622
623 /* Calculate the even block number */
624 block = (int) (addr >> this->erase_shift) & ~1;
625 /* Is it the odd plane? */
626 if (addr & this->writesize)
627 block++;
628 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
629 blockpage = (block << 7) | page;
630
631 return blockpage;
632}
633
634/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900635 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
636 * @param mtd MTD data structure
637 * @param addr address to check
638 * @return 1 if there are valid data, otherwise 0
639 *
640 * Check bufferram if there is data we required
641 */
642static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
643{
644 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900645 int blockpage, found = 0;
646 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900647
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900648 if (ONENAND_IS_2PLANE(this))
649 blockpage = onenand_get_2x_blockpage(mtd, addr);
650 else
651 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900652
653 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900654 i = ONENAND_CURRENT_BUFFERRAM(this);
655 if (this->bufferram[i].blockpage == blockpage)
656 found = 1;
657 else {
658 /* Check another BufferRAM */
659 i = ONENAND_NEXT_BUFFERRAM(this);
660 if (this->bufferram[i].blockpage == blockpage) {
661 ONENAND_SET_NEXT_BUFFERRAM(this);
662 found = 1;
663 }
664 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900665
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900666 if (found && ONENAND_IS_DDP(this)) {
667 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530668 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900669 int value = onenand_bufferram_address(this, block);
670 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
671 }
672
673 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900674}
675
676/**
677 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
678 * @param mtd MTD data structure
679 * @param addr address to update
680 * @param valid valid flag
681 *
682 * Update BufferRAM information
683 */
684static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
685 int valid)
686{
687 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900688 int blockpage;
689 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900690
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900691 if (ONENAND_IS_2PLANE(this))
692 blockpage = onenand_get_2x_blockpage(mtd, addr);
693 else
694 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900695
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900696 /* Invalidate another BufferRAM */
697 i = ONENAND_NEXT_BUFFERRAM(this);
698 if (this->bufferram[i].blockpage == blockpage)
699 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900700
701 /* Update BufferRAM */
702 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900703 if (valid)
704 this->bufferram[i].blockpage = blockpage;
705 else
706 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900707
708 return 0;
709}
710
711/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900712 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
713 * @param mtd MTD data structure
714 * @param addr start address to invalidate
715 * @param len length to invalidate
716 *
717 * Invalidate BufferRAM information
718 */
719static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200720 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900721{
722 struct onenand_chip *this = mtd->priv;
723 int i;
724 loff_t end_addr = addr + len;
725
726 /* Invalidate BufferRAM */
727 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900728 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900729
730 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900731 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900732 }
733}
734
735/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900736 * onenand_get_device - [GENERIC] Get chip for selected access
737 * @param mtd MTD device structure
738 * @param new_state the state which is requested
739 *
740 * Get the device and lock it for exclusive access
741 */
742static void onenand_get_device(struct mtd_info *mtd, int new_state)
743{
744 /* Do nothing */
745}
746
747/**
748 * onenand_release_device - [GENERIC] release chip
749 * @param mtd MTD device structure
750 *
751 * Deselect, release chip lock and wake up anyone waiting on the device
752 */
753static void onenand_release_device(struct mtd_info *mtd)
754{
755 /* Do nothing */
756}
757
758/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000759 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900760 * @param mtd MTD device structure
761 * @param buf destination address
762 * @param column oob offset to read from
763 * @param thislen oob length to read
764 */
765static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
766 int column, int thislen)
767{
768 struct onenand_chip *this = mtd->priv;
769 struct nand_oobfree *free;
770 int readcol = column;
771 int readend = column + thislen;
772 int lastgap = 0;
773 unsigned int i;
774 uint8_t *oob_buf = this->oob_buf;
775
776 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530777 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
778 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900779 if (readcol >= lastgap)
780 readcol += free->offset - lastgap;
781 if (readend >= lastgap)
782 readend += free->offset - lastgap;
783 lastgap = free->offset + free->length;
784 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900785 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900786 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530787 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
788 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900789 int free_end = free->offset + free->length;
790 if (free->offset < readend && free_end > readcol) {
791 int st = max_t(int,free->offset,readcol);
792 int ed = min_t(int,free_end,readend);
793 int n = ed - st;
794 memcpy(buf, oob_buf + st, n);
795 buf += n;
796 } else if (column == 0)
797 break;
798 }
799 return 0;
800}
801
802/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530803 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
804 * @param mtd MTD device structure
805 * @param addr address to recover
806 * @param status return value from onenand_wait
807 *
808 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
809 * lower page address and MSB page has higher page address in paired pages.
810 * If power off occurs during MSB page program, the paired LSB page data can
811 * become corrupt. LSB page recovery read is a way to read LSB page though page
812 * data are corrupted. When uncorrectable error occurs as a result of LSB page
813 * read after power up, issue LSB page recovery read.
814 */
815static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
816{
817 struct onenand_chip *this = mtd->priv;
818 int i;
819
820 /* Recovery is only for Flex-OneNAND */
821 if (!FLEXONENAND(this))
822 return status;
823
824 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000825 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530826 return status;
827
828 /* check if address lies in MLC region */
829 i = flexonenand_region(mtd, addr);
830 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
831 return status;
832
833 printk("onenand_recover_lsb:"
834 "Attempting to recover from uncorrectable read\n");
835
836 /* Issue the LSB page recovery command */
837 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
838 return this->wait(mtd, FL_READING);
839}
840
841/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900842 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900843 * @param mtd MTD device structure
844 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900845 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900846 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900847 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900848 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900849static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
850 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900851{
852 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900853 struct mtd_ecc_stats stats;
854 size_t len = ops->len;
855 size_t ooblen = ops->ooblen;
856 u_char *buf = ops->datbuf;
857 u_char *oobbuf = ops->oobbuf;
858 int read = 0, column, thislen;
859 int oobread = 0, oobcolumn, thisooblen, oobsize;
860 int ret = 0, boundary = 0;
861 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900862
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900863 pr_debug("onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
864 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900865
Sergey Lapin3a38a552013-01-14 03:46:50 +0000866 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900867 oobsize = this->ecclayout->oobavail;
868 else
869 oobsize = mtd->oobsize;
870
871 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900872
873 /* Do not allow reads past end of device */
874 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900875 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
876 ops->retlen = 0;
877 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900878 return -EINVAL;
879 }
880
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900881 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900882
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900883 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530884 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900885
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900886 /* Do first load to bufferRAM */
887 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900888 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900889 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900890 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900891 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530892 if (unlikely(ret))
893 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900894 onenand_update_bufferram(mtd, from, !ret);
895 if (ret == -EBADMSG)
896 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900897 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900898 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900899
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900900 thislen = min_t(int, writesize, len - read);
901 column = from & (writesize - 1);
902 if (column + thislen > writesize)
903 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900904
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900905 while (!ret) {
906 /* If there is more to load then start next load */
907 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100908 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900909 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900910 this->command(mtd, ONENAND_CMD_READ, from, writesize);
911 /*
912 * Chip boundary handling in DDP
913 * Now we issued chip 1 read and pointed chip 1
914 * bufferam so we have to point chip 0 bufferam.
915 */
916 if (ONENAND_IS_DDP(this) &&
917 unlikely(from == (this->chipsize >> 1))) {
918 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
919 boundary = 1;
920 } else
921 boundary = 0;
922 ONENAND_SET_PREV_BUFFERRAM(this);
923 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900924
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900925 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900926 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900927
928 /* Read oob area if needed */
929 if (oobbuf) {
930 thisooblen = oobsize - oobcolumn;
931 thisooblen = min_t(int, thisooblen, ooblen - oobread);
932
Sergey Lapin3a38a552013-01-14 03:46:50 +0000933 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900934 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
935 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900936 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900937 oobread += thisooblen;
938 oobbuf += thisooblen;
939 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900940 }
941
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100942 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530943 this->command(mtd, ONENAND_CMD_READ, from, writesize);
944 ret = this->wait(mtd, FL_READING);
945 if (unlikely(ret))
946 ret = onenand_recover_lsb(mtd, from, ret);
947 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000948 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530949 ret = 0;
950 }
951
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900952 /* See if we are done */
953 read += thislen;
954 if (read == len)
955 break;
956 /* Set up for next read from bufferRAM */
957 if (unlikely(boundary))
958 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100959 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530960 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900961 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900962 thislen = min_t(int, writesize, len - read);
963 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900964
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100965 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530966 /* Now wait for load */
967 ret = this->wait(mtd, FL_READING);
968 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000969 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530970 ret = 0;
971 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900972 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900973
974 /*
975 * Return success, if no ECC failures, else -EBADMSG
976 * fs driver will take care of that, because
977 * retlen == desired len and result == -EBADMSG
978 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900979 ops->retlen = read;
980 ops->oobretlen = oobread;
981
982 if (ret)
983 return ret;
984
985 if (mtd->ecc_stats.failed - stats.failed)
986 return -EBADMSG;
987
Paul Burton700a76c2013-09-04 15:16:56 +0100988 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
989 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900990}
991
992/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900993 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
994 * @param mtd MTD device structure
995 * @param from offset to read from
996 * @param ops oob operation description structure
997 *
998 * OneNAND read out-of-band data from the spare area
999 */
1000static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1001 struct mtd_oob_ops *ops)
1002{
1003 struct onenand_chip *this = mtd->priv;
1004 struct mtd_ecc_stats stats;
1005 int read = 0, thislen, column, oobsize;
1006 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001007 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001008 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301009 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001010
1011 from += ops->ooboffs;
1012
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001013 pr_debug("onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
1014 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001015
1016 /* Initialize return length value */
1017 ops->oobretlen = 0;
1018
Sergey Lapin3a38a552013-01-14 03:46:50 +00001019 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001020 oobsize = this->ecclayout->oobavail;
1021 else
1022 oobsize = mtd->oobsize;
1023
1024 column = from & (mtd->oobsize - 1);
1025
1026 if (unlikely(column >= oobsize)) {
1027 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1028 return -EINVAL;
1029 }
1030
1031 /* Do not allow reads past end of device */
1032 if (unlikely(from >= mtd->size ||
1033 column + len > ((mtd->size >> this->page_shift) -
1034 (from >> this->page_shift)) * oobsize)) {
1035 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1036 return -EINVAL;
1037 }
1038
1039 stats = mtd->ecc_stats;
1040
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001041 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1042 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301043
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001044 while (read < len) {
1045 thislen = oobsize - column;
1046 thislen = min_t(int, thislen, len);
1047
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001048 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301049 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001050
1051 onenand_update_bufferram(mtd, from, 0);
1052
1053 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301054 if (unlikely(ret))
1055 ret = onenand_recover_lsb(mtd, from, ret);
1056
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001057 if (ret && ret != -EBADMSG) {
1058 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1059 break;
1060 }
1061
Sergey Lapin3a38a552013-01-14 03:46:50 +00001062 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001063 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1064 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001065 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001066
1067 read += thislen;
1068
1069 if (read == len)
1070 break;
1071
1072 buf += thislen;
1073
1074 /* Read more? */
1075 if (read < len) {
1076 /* Page size */
1077 from += mtd->writesize;
1078 column = 0;
1079 }
1080 }
1081
1082 ops->oobretlen = read;
1083
1084 if (ret)
1085 return ret;
1086
1087 if (mtd->ecc_stats.failed - stats.failed)
1088 return -EBADMSG;
1089
1090 return 0;
1091}
1092
1093/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001094 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1095 * @param mtd MTD device structure
1096 * @param from offset to read from
1097 * @param len number of bytes to read
1098 * @param retlen pointer to variable to store the number of read bytes
1099 * @param buf the databuffer to put data
1100 *
1101 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1102*/
1103int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1104 size_t * retlen, u_char * buf)
1105{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001106 struct mtd_oob_ops ops = {
1107 .len = len,
1108 .ooblen = 0,
1109 .datbuf = buf,
1110 .oobbuf = NULL,
1111 };
1112 int ret;
1113
1114 onenand_get_device(mtd, FL_READING);
1115 ret = onenand_read_ops_nolock(mtd, from, &ops);
1116 onenand_release_device(mtd);
1117
1118 *retlen = ops.retlen;
1119 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001120}
1121
1122/**
1123 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1124 * @param mtd MTD device structure
1125 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001126 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001127 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001128 * OneNAND main and/or out-of-band
1129 */
1130int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1131 struct mtd_oob_ops *ops)
1132{
1133 int ret;
1134
1135 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001136 case MTD_OPS_PLACE_OOB:
1137 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001138 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001139 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001140 /* Not implemented yet */
1141 default:
1142 return -EINVAL;
1143 }
1144
1145 onenand_get_device(mtd, FL_READING);
1146 if (ops->datbuf)
1147 ret = onenand_read_ops_nolock(mtd, from, ops);
1148 else
1149 ret = onenand_read_oob_nolock(mtd, from, ops);
1150 onenand_release_device(mtd);
1151
1152 return ret;
1153}
1154
1155/**
1156 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1157 * @param mtd MTD device structure
1158 * @param state state to select the max. timeout value
1159 *
1160 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001161 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001162static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001163{
1164 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001165 unsigned int interrupt;
1166 unsigned int ctrl;
1167
Ladislav Michl243af2f2016-07-12 20:28:19 +02001168 /* Wait at most 20ms ... */
1169 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
1170 u32 time_start = get_timer(0);
1171 do {
1172 WATCHDOG_RESET();
1173 if (get_timer(time_start) > timeo)
1174 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001175 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +02001176 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001177
1178 /* To get correct interrupt status in timeout case */
1179 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1180 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1181
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001182 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301183 int ecc = onenand_read_ecc(this);
1184 if (ecc & ONENAND_ECC_2BIT_ALL) {
1185 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1186 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001187 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301188 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001189 } else {
1190 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1191 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1192 return ONENAND_BBT_READ_FATAL_ERROR;
1193 }
1194
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001195 /* Initial bad block case: 0x2400 or 0x0400 */
1196 if (ctrl & ONENAND_CTRL_ERROR) {
1197 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1198 return ONENAND_BBT_READ_ERROR;
1199 }
1200
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001201 return 0;
1202}
1203
1204/**
1205 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1206 * @param mtd MTD device structure
1207 * @param from offset to read from
1208 * @param ops oob operation description structure
1209 *
1210 * OneNAND read out-of-band data from the spare area for bbt scan
1211 */
1212int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1213 struct mtd_oob_ops *ops)
1214{
1215 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001216 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301217 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001218 size_t len = ops->ooblen;
1219 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001220
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001221 pr_debug("onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
1222 (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001223
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001224 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1225 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301226
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001227 /* Initialize return value */
1228 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001229
1230 /* Do not allow reads past end of device */
1231 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001232 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1233 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001234 }
1235
1236 /* Grab the lock and see if the device is available */
1237 onenand_get_device(mtd, FL_READING);
1238
1239 column = from & (mtd->oobsize - 1);
1240
1241 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001242
Kyungmin Park79e90462007-09-10 17:13:49 +09001243 thislen = mtd->oobsize - column;
1244 thislen = min_t(int, thislen, len);
1245
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001246 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301247 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001248
1249 onenand_update_bufferram(mtd, from, 0);
1250
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001251 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301252 if (unlikely(ret))
1253 ret = onenand_recover_lsb(mtd, from, ret);
1254
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001255 if (ret)
1256 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001257
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001258 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001259 read += thislen;
1260 if (read == len)
1261 break;
1262
Kyungmin Park79e90462007-09-10 17:13:49 +09001263 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001264
Kyungmin Park79e90462007-09-10 17:13:49 +09001265 /* Read more? */
1266 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001267 /* Update Page size */
1268 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001269 column = 0;
1270 }
1271 }
1272
1273 /* Deselect and wake up anyone waiting on the device */
1274 onenand_release_device(mtd);
1275
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001276 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001277 return ret;
1278}
1279
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001280
Kyungmin Park79e90462007-09-10 17:13:49 +09001281#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1282/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001283 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1284 * @param mtd MTD device structure
1285 * @param buf the databuffer to verify
1286 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001287 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001288static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001289{
1290 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001291 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301292 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001293
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001294 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1295 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301296
1297 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001298 onenand_update_bufferram(mtd, to, 0);
1299 status = this->wait(mtd, FL_READING);
1300 if (status)
1301 return status;
1302
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001303 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001304 for (i = 0; i < mtd->oobsize; i++)
1305 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1306 return -EBADMSG;
1307
1308 return 0;
1309}
1310
1311/**
1312 * onenand_verify - [GENERIC] verify the chip contents after a write
1313 * @param mtd MTD device structure
1314 * @param buf the databuffer to verify
1315 * @param addr offset to read from
1316 * @param len number of bytes to read and compare
1317 */
1318static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1319{
1320 struct onenand_chip *this = mtd->priv;
1321 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001322 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001323 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001324
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001325 while (len != 0) {
1326 thislen = min_t(int, this->writesize, len);
1327 column = addr & (this->writesize - 1);
1328 if (column + thislen > this->writesize)
1329 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001330
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001331 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001332
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001333 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001334
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001335 ret = this->wait(mtd, FL_READING);
1336 if (ret)
1337 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001338
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001339 onenand_update_bufferram(mtd, addr, 1);
1340
1341 dataram = this->base + ONENAND_DATARAM;
1342 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1343
1344 if (memcmp(buf, dataram + column, thislen))
1345 return -EBADMSG;
1346
1347 len -= thislen;
1348 buf += thislen;
1349 addr += thislen;
1350 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001351
1352 return 0;
1353}
1354#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001355#define onenand_verify(...) (0)
1356#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001357#endif
1358
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001359#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001360
1361/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001362 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001363 * @param mtd MTD device structure
1364 * @param oob_buf oob buffer
1365 * @param buf source address
1366 * @param column oob offset to write to
1367 * @param thislen oob length to write
1368 */
1369static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1370 const u_char *buf, int column, int thislen)
1371{
1372 struct onenand_chip *this = mtd->priv;
1373 struct nand_oobfree *free;
1374 int writecol = column;
1375 int writeend = column + thislen;
1376 int lastgap = 0;
1377 unsigned int i;
1378
1379 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301380 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1381 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001382 if (writecol >= lastgap)
1383 writecol += free->offset - lastgap;
1384 if (writeend >= lastgap)
1385 writeend += free->offset - lastgap;
1386 lastgap = free->offset + free->length;
1387 }
1388 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301389 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1390 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001391 int free_end = free->offset + free->length;
1392 if (free->offset < writeend && free_end > writecol) {
1393 int st = max_t(int,free->offset,writecol);
1394 int ed = min_t(int,free_end,writeend);
1395 int n = ed - st;
1396 memcpy(oob_buf + st, buf, n);
1397 buf += n;
1398 } else if (column == 0)
1399 break;
1400 }
1401 return 0;
1402}
1403
1404/**
1405 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1406 * @param mtd MTD device structure
1407 * @param to offset to write to
1408 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001409 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001410 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001411 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001412static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1413 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001414{
1415 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001416 int written = 0, column, thislen, subpage;
1417 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1418 size_t len = ops->len;
1419 size_t ooblen = ops->ooblen;
1420 const u_char *buf = ops->datbuf;
1421 const u_char *oob = ops->oobbuf;
1422 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001423 int ret = 0;
1424
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001425 pr_debug("onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
1426 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001427
1428 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001429 ops->retlen = 0;
1430 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001431
Kyungmin Park79e90462007-09-10 17:13:49 +09001432 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001433 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1434 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001435 return -EINVAL;
1436 }
1437
Sergey Lapin3a38a552013-01-14 03:46:50 +00001438 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001439 oobsize = this->ecclayout->oobavail;
1440 else
1441 oobsize = mtd->oobsize;
1442
1443 oobcolumn = to & (mtd->oobsize - 1);
1444
1445 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001446
1447 /* Loop until all data write */
1448 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001449 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001450
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001451 thislen = min_t(int, mtd->writesize - column, len - written);
1452 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001453
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001454 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001455
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001456 /* Partial page write */
1457 subpage = thislen < mtd->writesize;
1458 if (subpage) {
1459 memset(this->page_buf, 0xff, mtd->writesize);
1460 memcpy(this->page_buf + column, buf, thislen);
1461 wbuf = this->page_buf;
1462 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001463
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001464 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001465
1466 if (oob) {
1467 oobbuf = this->oob_buf;
1468
1469 /* We send data to spare ram with oobsize
1470 * * to prevent byte access */
1471 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001472 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001473 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1474 else
1475 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1476
1477 oobwritten += thisooblen;
1478 oob += thisooblen;
1479 oobcolumn = 0;
1480 } else
1481 oobbuf = (u_char *) ffchars;
1482
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001483 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001484
1485 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001486
1487 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001488
1489 /* In partial page write we don't update bufferram */
1490 onenand_update_bufferram(mtd, to, !ret && !subpage);
1491 if (ONENAND_IS_2PLANE(this)) {
1492 ONENAND_SET_BUFFERRAM1(this);
1493 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1494 }
1495
Kyungmin Park79e90462007-09-10 17:13:49 +09001496 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001497 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001498 break;
1499 }
1500
Kyungmin Park79e90462007-09-10 17:13:49 +09001501 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001502 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001503 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001504 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001505 break;
1506 }
1507
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001508 written += thislen;
1509
Kyungmin Park79e90462007-09-10 17:13:49 +09001510 if (written == len)
1511 break;
1512
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001513 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001514 to += thislen;
1515 buf += thislen;
1516 }
1517
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001518 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001519
1520 return ret;
1521}
1522
1523/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001524 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001525 * @param mtd MTD device structure
1526 * @param to offset to write to
1527 * @param len number of bytes to write
1528 * @param retlen pointer to variable to store the number of written bytes
1529 * @param buf the data to write
1530 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001531 *
1532 * OneNAND write out-of-band
1533 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001534static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1535 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001536{
1537 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001538 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301539 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001540 u_char *oobbuf;
1541 size_t len = ops->ooblen;
1542 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001543 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001544
1545 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001546
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001547 pr_debug("onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
1548 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001549
1550 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001551 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001552
Sergey Lapin3a38a552013-01-14 03:46:50 +00001553 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001554 oobsize = this->ecclayout->oobavail;
1555 else
1556 oobsize = mtd->oobsize;
1557
1558 column = to & (mtd->oobsize - 1);
1559
1560 if (unlikely(column >= oobsize)) {
1561 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001562 return -EINVAL;
1563 }
1564
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001565 /* For compatibility with NAND: Do not allow write past end of page */
1566 if (unlikely(column + len > oobsize)) {
1567 printk(KERN_ERR "onenand_write_oob_nolock: "
1568 "Attempt to write past end of page\n");
1569 return -EINVAL;
1570 }
1571
1572 /* Do not allow reads past end of device */
1573 if (unlikely(to >= mtd->size ||
1574 column + len > ((mtd->size >> this->page_shift) -
1575 (to >> this->page_shift)) * oobsize)) {
1576 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1577 return -EINVAL;
1578 }
1579
1580 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001581
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001582 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1583 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301584
Kyungmin Park79e90462007-09-10 17:13:49 +09001585 /* Loop until all data write */
1586 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001587 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001588
1589 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1590
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001591 /* We send data to spare ram with oobsize
1592 * to prevent byte access */
1593 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001594 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001595 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1596 else
1597 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001598 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001599
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001600 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301601 /* Set main area of DataRAM to 0xff*/
1602 memset(this->page_buf, 0xff, mtd->writesize);
1603 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1604 this->page_buf, 0, mtd->writesize);
1605 }
1606
1607 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001608
1609 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001610 if (ONENAND_IS_2PLANE(this)) {
1611 ONENAND_SET_BUFFERRAM1(this);
1612 onenand_update_bufferram(mtd, to + this->writesize, 0);
1613 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001614
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001615 ret = this->wait(mtd, FL_WRITING);
1616 if (ret) {
1617 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001618 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001619 }
1620
1621 ret = onenand_verify_oob(mtd, oobbuf, to);
1622 if (ret) {
1623 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1624 break;
1625 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001626
1627 written += thislen;
1628 if (written == len)
1629 break;
1630
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001631 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001632 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001633 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001634 }
1635
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001636 ops->oobretlen = written;
1637
1638 return ret;
1639}
1640
1641/**
1642 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1643 * @param mtd MTD device structure
1644 * @param to offset to write to
1645 * @param len number of bytes to write
1646 * @param retlen pointer to variable to store the number of written bytes
1647 * @param buf the data to write
1648 *
1649 * Write with ECC
1650 */
1651int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1652 size_t * retlen, const u_char * buf)
1653{
1654 struct mtd_oob_ops ops = {
1655 .len = len,
1656 .ooblen = 0,
1657 .datbuf = (u_char *) buf,
1658 .oobbuf = NULL,
1659 };
1660 int ret;
1661
1662 onenand_get_device(mtd, FL_WRITING);
1663 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001664 onenand_release_device(mtd);
1665
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001666 *retlen = ops.retlen;
1667 return ret;
1668}
Kyungmin Park79e90462007-09-10 17:13:49 +09001669
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001670/**
1671 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1672 * @param mtd MTD device structure
1673 * @param to offset to write to
1674 * @param ops oob operation description structure
1675 *
1676 * OneNAND write main and/or out-of-band
1677 */
1678int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1679 struct mtd_oob_ops *ops)
1680{
1681 int ret;
1682
1683 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001684 case MTD_OPS_PLACE_OOB:
1685 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001686 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001687 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001688 /* Not implemented yet */
1689 default:
1690 return -EINVAL;
1691 }
1692
1693 onenand_get_device(mtd, FL_WRITING);
1694 if (ops->datbuf)
1695 ret = onenand_write_ops_nolock(mtd, to, ops);
1696 else
1697 ret = onenand_write_oob_nolock(mtd, to, ops);
1698 onenand_release_device(mtd);
1699
1700 return ret;
1701
Kyungmin Park79e90462007-09-10 17:13:49 +09001702}
1703
Kyungmin Park396b0c42008-08-13 09:11:02 +09001704/**
1705 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1706 * @param mtd MTD device structure
1707 * @param ofs offset from device start
1708 * @param allowbbt 1, if its allowed to access the bbt area
1709 *
1710 * Check, if the block is bad, Either by reading the bad block table or
1711 * calling of the scan function.
1712 */
1713static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1714{
1715 struct onenand_chip *this = mtd->priv;
1716 struct bbm_info *bbm = this->bbm;
1717
1718 /* Return info from the table */
1719 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1720}
1721
1722
Kyungmin Park79e90462007-09-10 17:13:49 +09001723/**
1724 * onenand_erase - [MTD Interface] erase block(s)
1725 * @param mtd MTD device structure
1726 * @param instr erase instruction
1727 *
1728 * Erase one ore more blocks
1729 */
1730int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1731{
1732 struct onenand_chip *this = mtd->priv;
1733 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301734 loff_t addr = instr->addr;
1735 unsigned int len = instr->len;
1736 int ret = 0, i;
1737 struct mtd_erase_region_info *region = NULL;
1738 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001739
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001740 pr_debug("onenand_erase: start = 0x%08x, len = %i\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301741 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001742
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301743 if (FLEXONENAND(this)) {
1744 /* Find the eraseregion of this address */
1745 i = flexonenand_region(mtd, addr);
1746 region = &mtd->eraseregions[i];
1747
1748 block_size = region->erasesize;
1749 region_end = region->offset
1750 + region->erasesize * region->numblocks;
1751
1752 /* Start address within region must align on block boundary.
1753 * Erase region's start offset is always block start address.
1754 */
1755 if (unlikely((addr - region->offset) & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001756 pr_debug("onenand_erase:" " Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301757 return -EINVAL;
1758 }
1759 } else {
1760 block_size = 1 << this->erase_shift;
1761
1762 /* Start address must align on block boundary */
1763 if (unlikely(addr & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001764 pr_debug("onenand_erase:" "Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301765 return -EINVAL;
1766 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001767 }
1768
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301769 /* Length must align on block boundary */
1770 if (unlikely(len & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001771 pr_debug("onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001772 return -EINVAL;
1773 }
1774
Kyungmin Park79e90462007-09-10 17:13:49 +09001775 /* Grab the lock and see if the device is available */
1776 onenand_get_device(mtd, FL_ERASING);
1777
1778 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001779 instr->state = MTD_ERASING;
1780
1781 while (len) {
1782
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001783 /* Check if we have a bad block, we do not erase bad blocks */
1784 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1785 printk(KERN_WARNING "onenand_erase: attempt to erase"
1786 " a bad block at addr 0x%08x\n",
1787 (unsigned int) addr);
1788 instr->state = MTD_ERASE_FAILED;
1789 goto erase_exit;
1790 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001791
1792 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1793
Kyungmin Park396b0c42008-08-13 09:11:02 +09001794 onenand_invalidate_bufferram(mtd, addr, block_size);
1795
Kyungmin Park79e90462007-09-10 17:13:49 +09001796 ret = this->wait(mtd, FL_ERASING);
1797 /* Check, if it is write protected */
1798 if (ret) {
1799 if (ret == -EPERM)
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001800 pr_debug("onenand_erase: "
1801 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001802 else
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001803 pr_debug("onenand_erase: "
1804 "Failed erase, block %d\n",
1805 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001806 instr->state = MTD_ERASE_FAILED;
1807 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001808
Kyungmin Park79e90462007-09-10 17:13:49 +09001809 goto erase_exit;
1810 }
1811
1812 len -= block_size;
1813 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301814
1815 if (addr == region_end) {
1816 if (!len)
1817 break;
1818 region++;
1819
1820 block_size = region->erasesize;
1821 region_end = region->offset
1822 + region->erasesize * region->numblocks;
1823
1824 if (len & (block_size - 1)) {
1825 /* This has been checked at MTD
1826 * partitioning level. */
1827 printk("onenand_erase: Unaligned address\n");
1828 goto erase_exit;
1829 }
1830 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001831 }
1832
1833 instr->state = MTD_ERASE_DONE;
1834
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001835erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001836
1837 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1838 /* Do call back function */
1839 if (!ret)
1840 mtd_erase_callback(instr);
1841
1842 /* Deselect and wake up anyone waiting on the device */
1843 onenand_release_device(mtd);
1844
1845 return ret;
1846}
1847
1848/**
1849 * onenand_sync - [MTD Interface] sync
1850 * @param mtd MTD device structure
1851 *
1852 * Sync is actually a wait for chip ready function
1853 */
1854void onenand_sync(struct mtd_info *mtd)
1855{
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001856 pr_debug("onenand_sync: called\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001857
1858 /* Grab the lock and see if the device is available */
1859 onenand_get_device(mtd, FL_SYNCING);
1860
1861 /* Release it and go back */
1862 onenand_release_device(mtd);
1863}
1864
1865/**
1866 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1867 * @param mtd MTD device structure
1868 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001869 *
1870 * Check whether the block is bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001871 */
1872int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1873{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001874 int ret;
1875
1876 /* Check for invalid offset */
1877 if (ofs > mtd->size)
1878 return -EINVAL;
1879
1880 onenand_get_device(mtd, FL_READING);
1881 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1882 onenand_release_device(mtd);
1883 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001884}
1885
1886/**
Kyungmin Park87b49502008-11-13 15:14:33 +09001887 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1888 * @param mtd MTD device structure
1889 * @param ofs offset from device start
1890 *
1891 * This is the default implementation, which can be overridden by
1892 * a hardware specific driver.
1893 */
1894static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1895{
1896 struct onenand_chip *this = mtd->priv;
1897 struct bbm_info *bbm = this->bbm;
1898 u_char buf[2] = {0, 0};
1899 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001900 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park87b49502008-11-13 15:14:33 +09001901 .ooblen = 2,
1902 .oobbuf = buf,
1903 .ooboffs = 0,
1904 };
1905 int block;
1906
1907 /* Get block number */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301908 block = onenand_block(this, ofs);
Kyungmin Park87b49502008-11-13 15:14:33 +09001909 if (bbm->bbt)
1910 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1911
1912 /* We write two bytes, so we dont have to mess with 16 bit access */
1913 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1914 return onenand_write_oob_nolock(mtd, ofs, &ops);
1915}
1916
1917/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001918 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1919 * @param mtd MTD device structure
1920 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001921 *
1922 * Mark the block as bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001923 */
1924int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1925{
Ladislav Michl7868fba2017-06-20 17:17:45 +02001926 struct onenand_chip *this = mtd->priv;
Kyungmin Park396b0c42008-08-13 09:11:02 +09001927 int ret;
1928
1929 ret = onenand_block_isbad(mtd, ofs);
1930 if (ret) {
1931 /* If it was bad already, return success and do nothing */
1932 if (ret > 0)
1933 return 0;
1934 return ret;
1935 }
1936
Ladislav Michl7868fba2017-06-20 17:17:45 +02001937 onenand_get_device(mtd, FL_WRITING);
1938 ret = this->block_markbad(mtd, ofs);
1939 onenand_release_device(mtd);
1940
Kyungmin Park396b0c42008-08-13 09:11:02 +09001941 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001942}
1943
1944/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001945 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1946 * @param mtd MTD device structure
1947 * @param ofs offset relative to mtd start
1948 * @param len number of bytes to lock or unlock
1949 * @param cmd lock or unlock command
Kyungmin Park79e90462007-09-10 17:13:49 +09001950 *
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001951 * Lock or unlock one or more blocks
Kyungmin Park79e90462007-09-10 17:13:49 +09001952 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001953static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park79e90462007-09-10 17:13:49 +09001954{
1955 struct onenand_chip *this = mtd->priv;
1956 int start, end, block, value, status;
1957
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301958 start = onenand_block(this, ofs);
1959 end = onenand_block(this, ofs + len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001960
1961 /* Continuous lock scheme */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001962 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park79e90462007-09-10 17:13:49 +09001963 /* Set start block address */
1964 this->write_word(start,
1965 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1966 /* Set end block address */
1967 this->write_word(end - 1,
1968 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1969 /* Write unlock command */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001970 this->command(mtd, cmd, 0, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001971
1972 /* There's no return value */
1973 this->wait(mtd, FL_UNLOCKING);
1974
1975 /* Sanity check */
1976 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1977 & ONENAND_CTRL_ONGO)
1978 continue;
1979
1980 /* Check lock status */
1981 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1982 if (!(status & ONENAND_WP_US))
1983 printk(KERN_ERR "wp status = 0x%x\n", status);
1984
1985 return 0;
1986 }
1987
1988 /* Block lock scheme */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301989 for (block = start; block < end; block++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001990 /* Set block address */
1991 value = onenand_block_address(this, block);
1992 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1993 /* Select DataRAM for DDP */
1994 value = onenand_bufferram_address(this, block);
1995 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1996
Kyungmin Park79e90462007-09-10 17:13:49 +09001997 /* Set start block address */
1998 this->write_word(block,
1999 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2000 /* Write unlock command */
2001 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
2002
2003 /* There's no return value */
2004 this->wait(mtd, FL_UNLOCKING);
2005
2006 /* Sanity check */
2007 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2008 & ONENAND_CTRL_ONGO)
2009 continue;
2010
Kyungmin Park79e90462007-09-10 17:13:49 +09002011 /* Check lock status */
2012 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2013 if (!(status & ONENAND_WP_US))
2014 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2015 block, status);
2016 }
2017
2018 return 0;
2019}
2020
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002021#ifdef ONENAND_LINUX
Kyungmin Park79e90462007-09-10 17:13:49 +09002022/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002023 * onenand_lock - [MTD Interface] Lock block(s)
2024 * @param mtd MTD device structure
2025 * @param ofs offset relative to mtd start
2026 * @param len number of bytes to unlock
2027 *
2028 * Lock one or more blocks
2029 */
2030static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2031{
2032 int ret;
2033
2034 onenand_get_device(mtd, FL_LOCKING);
2035 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2036 onenand_release_device(mtd);
2037 return ret;
2038}
2039
2040/**
2041 * onenand_unlock - [MTD Interface] Unlock block(s)
2042 * @param mtd MTD device structure
2043 * @param ofs offset relative to mtd start
2044 * @param len number of bytes to unlock
2045 *
2046 * Unlock one or more blocks
2047 */
2048static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2049{
2050 int ret;
2051
2052 onenand_get_device(mtd, FL_LOCKING);
2053 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2054 onenand_release_device(mtd);
2055 return ret;
2056}
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002057#endif
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002058
2059/**
2060 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2061 * @param this onenand chip data structure
2062 *
2063 * Check lock status
2064 */
2065static int onenand_check_lock_status(struct onenand_chip *this)
2066{
2067 unsigned int value, block, status;
2068 unsigned int end;
2069
2070 end = this->chipsize >> this->erase_shift;
2071 for (block = 0; block < end; block++) {
2072 /* Set block address */
2073 value = onenand_block_address(this, block);
2074 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2075 /* Select DataRAM for DDP */
2076 value = onenand_bufferram_address(this, block);
2077 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2078 /* Set start block address */
2079 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2080
2081 /* Check lock status */
2082 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2083 if (!(status & ONENAND_WP_US)) {
2084 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2085 return 0;
2086 }
2087 }
2088
2089 return 1;
2090}
2091
2092/**
2093 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2094 * @param mtd MTD device structure
2095 *
2096 * Unlock all blocks
2097 */
2098static void onenand_unlock_all(struct mtd_info *mtd)
2099{
2100 struct onenand_chip *this = mtd->priv;
2101 loff_t ofs = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302102 size_t len = mtd->size;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002103
2104 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2105 /* Set start block address */
2106 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2107 /* Write unlock command */
2108 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2109
2110 /* There's no return value */
2111 this->wait(mtd, FL_LOCKING);
2112
2113 /* Sanity check */
2114 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2115 & ONENAND_CTRL_ONGO)
2116 continue;
2117
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002118 /* Check lock status */
2119 if (onenand_check_lock_status(this))
2120 return;
2121
2122 /* Workaround for all block unlock in DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302123 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002124 /* All blocks on another chip */
2125 ofs = this->chipsize >> 1;
2126 len = this->chipsize >> 1;
2127 }
2128 }
2129
2130 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2131}
2132
2133
2134/**
2135 * onenand_check_features - Check and set OneNAND features
2136 * @param mtd MTD data structure
2137 *
2138 * Check and set OneNAND features
2139 * - lock scheme
2140 * - two plane
2141 */
2142static void onenand_check_features(struct mtd_info *mtd)
2143{
2144 struct onenand_chip *this = mtd->priv;
2145 unsigned int density, process;
2146
2147 /* Lock scheme depends on density and process */
2148 density = onenand_get_density(this->device_id);
2149 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2150
2151 /* Lock scheme */
2152 switch (density) {
2153 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002154 if (ONENAND_IS_DDP(this))
2155 this->options |= ONENAND_HAS_2PLANE;
2156 else
2157 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002158
2159 case ONENAND_DEVICE_DENSITY_2Gb:
2160 /* 2Gb DDP don't have 2 plane */
2161 if (!ONENAND_IS_DDP(this))
2162 this->options |= ONENAND_HAS_2PLANE;
2163 this->options |= ONENAND_HAS_UNLOCK_ALL;
2164
2165 case ONENAND_DEVICE_DENSITY_1Gb:
2166 /* A-Die has all block unlock */
2167 if (process)
2168 this->options |= ONENAND_HAS_UNLOCK_ALL;
2169 break;
2170
2171 default:
2172 /* Some OneNAND has continuous lock scheme */
2173 if (!process)
2174 this->options |= ONENAND_HAS_CONT_LOCK;
2175 break;
2176 }
2177
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302178 if (ONENAND_IS_MLC(this))
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002179 this->options |= ONENAND_HAS_4KB_PAGE;
2180
2181 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302182 this->options &= ~ONENAND_HAS_2PLANE;
2183
2184 if (FLEXONENAND(this)) {
2185 this->options &= ~ONENAND_HAS_CONT_LOCK;
2186 this->options |= ONENAND_HAS_UNLOCK_ALL;
2187 }
2188
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002189 if (this->options & ONENAND_HAS_CONT_LOCK)
2190 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2191 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2192 printk(KERN_DEBUG "Chip support all block unlock\n");
2193 if (this->options & ONENAND_HAS_2PLANE)
2194 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002195 if (this->options & ONENAND_HAS_4KB_PAGE)
2196 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2197
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002198}
2199
2200/**
Kyungmin Park79e90462007-09-10 17:13:49 +09002201 * onenand_print_device_info - Print device ID
2202 * @param device device ID
2203 *
2204 * Print device ID
2205 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002206char *onenand_print_device_info(int device, int version)
Kyungmin Park79e90462007-09-10 17:13:49 +09002207{
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302208 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002209 char *dev_info = malloc(80);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002210 char *p = dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002211
2212 vcc = device & ONENAND_DEVICE_VCC_MASK;
2213 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2214 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302215 density = onenand_get_density(device);
2216 flexonenand = device & DEVICE_IS_FLEXONENAND;
2217 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park79e90462007-09-10 17:13:49 +09002218 demuxed ? "" : "Muxed ",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302219 flexonenand ? "Flex-" : "",
Kyungmin Park79e90462007-09-10 17:13:49 +09002220 ddp ? "(DDP)" : "",
2221 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002222
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002223 sprintf(p, "\nOneNAND version = 0x%04x", version);
2224 printk("%s\n", dev_info);
2225
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002226 return dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002227}
2228
2229static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra891d81b2010-10-11 21:48:03 +02002230 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002231 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002232};
2233
2234/**
2235 * onenand_check_maf - Check manufacturer ID
2236 * @param manuf manufacturer ID
2237 *
2238 * Check manufacturer ID
2239 */
2240static int onenand_check_maf(int manuf)
2241{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002242 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park79e90462007-09-10 17:13:49 +09002243 int i;
Marek Vasute3334742011-11-06 00:59:52 +01002244#ifdef ONENAND_DEBUG
2245 char *name;
2246#endif
Kyungmin Park79e90462007-09-10 17:13:49 +09002247
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302248 for (i = 0; i < size; i++)
Kyungmin Park79e90462007-09-10 17:13:49 +09002249 if (manuf == onenand_manuf_ids[i].id)
2250 break;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002251
Marek Vasute3334742011-11-06 00:59:52 +01002252#ifdef ONENAND_DEBUG
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002253 if (i < size)
2254 name = onenand_manuf_ids[i].name;
2255 else
2256 name = "Unknown";
Kyungmin Park79e90462007-09-10 17:13:49 +09002257
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002258 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park79e90462007-09-10 17:13:49 +09002259#endif
2260
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002261 return i == size;
Kyungmin Park79e90462007-09-10 17:13:49 +09002262}
2263
2264/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302265* flexonenand_get_boundary - Reads the SLC boundary
2266* @param onenand_info - onenand info structure
2267*
2268* Fill up boundary[] field in onenand_chip
2269**/
2270static int flexonenand_get_boundary(struct mtd_info *mtd)
2271{
2272 struct onenand_chip *this = mtd->priv;
2273 unsigned int die, bdry;
Marek Vasute3334742011-11-06 00:59:52 +01002274 int syscfg, locked;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302275
2276 /* Disable ECC */
2277 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2278 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2279
2280 for (die = 0; die < this->dies; die++) {
2281 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2282 this->wait(mtd, FL_SYNCING);
2283
2284 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002285 this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302286
2287 bdry = this->read_word(this->base + ONENAND_DATARAM);
2288 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2289 locked = 0;
2290 else
2291 locked = 1;
2292 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2293
2294 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002295 this->wait(mtd, FL_RESETING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302296
2297 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2298 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2299 }
2300
2301 /* Enable ECC */
2302 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2303 return 0;
2304}
2305
2306/**
2307 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2308 * boundary[], diesize[], mtd->size, mtd->erasesize,
2309 * mtd->eraseregions
2310 * @param mtd - MTD device structure
2311 */
2312static void flexonenand_get_size(struct mtd_info *mtd)
2313{
2314 struct onenand_chip *this = mtd->priv;
2315 int die, i, eraseshift, density;
2316 int blksperdie, maxbdry;
2317 loff_t ofs;
2318
2319 density = onenand_get_density(this->device_id);
2320 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2321 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2322 maxbdry = blksperdie - 1;
2323 eraseshift = this->erase_shift - 1;
2324
2325 mtd->numeraseregions = this->dies << 1;
2326
2327 /* This fills up the device boundary */
2328 flexonenand_get_boundary(mtd);
2329 die = 0;
2330 ofs = 0;
2331 i = -1;
2332 for (; die < this->dies; die++) {
2333 if (!die || this->boundary[die-1] != maxbdry) {
2334 i++;
2335 mtd->eraseregions[i].offset = ofs;
2336 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2337 mtd->eraseregions[i].numblocks =
2338 this->boundary[die] + 1;
2339 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2340 eraseshift++;
2341 } else {
2342 mtd->numeraseregions -= 1;
2343 mtd->eraseregions[i].numblocks +=
2344 this->boundary[die] + 1;
2345 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2346 }
2347 if (this->boundary[die] != maxbdry) {
2348 i++;
2349 mtd->eraseregions[i].offset = ofs;
2350 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2351 mtd->eraseregions[i].numblocks = maxbdry ^
2352 this->boundary[die];
2353 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2354 eraseshift--;
2355 } else
2356 mtd->numeraseregions -= 1;
2357 }
2358
2359 /* Expose MLC erase size except when all blocks are SLC */
2360 mtd->erasesize = 1 << this->erase_shift;
2361 if (mtd->numeraseregions == 1)
2362 mtd->erasesize >>= 1;
2363
2364 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2365 for (i = 0; i < mtd->numeraseregions; i++)
2366 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2367 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2368 mtd->eraseregions[i].erasesize,
2369 mtd->eraseregions[i].numblocks);
2370
2371 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2372 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2373 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2374 << (this->erase_shift - 1);
2375 mtd->size += this->diesize[die];
2376 }
2377}
2378
2379/**
2380 * flexonenand_check_blocks_erased - Check if blocks are erased
2381 * @param mtd_info - mtd info structure
2382 * @param start - first erase block to check
2383 * @param end - last erase block to check
2384 *
2385 * Converting an unerased block from MLC to SLC
2386 * causes byte values to change. Since both data and its ECC
2387 * have changed, reads on the block give uncorrectable error.
2388 * This might lead to the block being detected as bad.
2389 *
2390 * Avoid this by ensuring that the block to be converted is
2391 * erased.
2392 */
2393static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2394 int start, int end)
2395{
2396 struct onenand_chip *this = mtd->priv;
2397 int i, ret;
2398 int block;
2399 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002400 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302401 .ooboffs = 0,
2402 .ooblen = mtd->oobsize,
2403 .datbuf = NULL,
2404 .oobbuf = this->oob_buf,
2405 };
2406 loff_t addr;
2407
2408 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2409
2410 for (block = start; block <= end; block++) {
2411 addr = flexonenand_addr(this, block);
2412 if (onenand_block_isbad_nolock(mtd, addr, 0))
2413 continue;
2414
2415 /*
2416 * Since main area write results in ECC write to spare,
2417 * it is sufficient to check only ECC bytes for change.
2418 */
2419 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2420 if (ret)
2421 return ret;
2422
2423 for (i = 0; i < mtd->oobsize; i++)
2424 if (this->oob_buf[i] != 0xff)
2425 break;
2426
2427 if (i != mtd->oobsize) {
2428 printk(KERN_WARNING "Block %d not erased.\n", block);
2429 return 1;
2430 }
2431 }
2432
2433 return 0;
2434}
2435
2436/**
2437 * flexonenand_set_boundary - Writes the SLC boundary
2438 * @param mtd - mtd info structure
2439 */
2440int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2441 int boundary, int lock)
2442{
2443 struct onenand_chip *this = mtd->priv;
2444 int ret, density, blksperdie, old, new, thisboundary;
2445 loff_t addr;
2446
2447 if (die >= this->dies)
2448 return -EINVAL;
2449
2450 if (boundary == this->boundary[die])
2451 return 0;
2452
2453 density = onenand_get_density(this->device_id);
2454 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2455 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2456
2457 if (boundary >= blksperdie) {
2458 printk("flexonenand_set_boundary:"
2459 "Invalid boundary value. "
2460 "Boundary not changed.\n");
2461 return -EINVAL;
2462 }
2463
2464 /* Check if converting blocks are erased */
2465 old = this->boundary[die] + (die * this->density_mask);
2466 new = boundary + (die * this->density_mask);
2467 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2468 + 1, max(old, new));
2469 if (ret) {
2470 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2471 return ret;
2472 }
2473
2474 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2475 this->wait(mtd, FL_SYNCING);
2476
2477 /* Check is boundary is locked */
2478 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2479 ret = this->wait(mtd, FL_READING);
2480
2481 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2482 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2483 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2484 goto out;
2485 }
2486
2487 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2488 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2489
2490 boundary &= FLEXONENAND_PI_MASK;
2491 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2492
2493 addr = die ? this->diesize[0] : 0;
2494 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2495 ret = this->wait(mtd, FL_ERASING);
2496 if (ret) {
2497 printk("flexonenand_set_boundary:"
2498 "Failed PI erase for Die %d\n", die);
2499 goto out;
2500 }
2501
2502 this->write_word(boundary, this->base + ONENAND_DATARAM);
2503 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2504 ret = this->wait(mtd, FL_WRITING);
2505 if (ret) {
2506 printk("flexonenand_set_boundary:"
2507 "Failed PI write for Die %d\n", die);
2508 goto out;
2509 }
2510
2511 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2512 ret = this->wait(mtd, FL_WRITING);
2513out:
2514 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2515 this->wait(mtd, FL_RESETING);
2516 if (!ret)
2517 /* Recalculate device size on boundary change*/
2518 flexonenand_get_size(mtd);
2519
2520 return ret;
2521}
2522
2523/**
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002524 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park79e90462007-09-10 17:13:49 +09002525 * @param mtd MTD device structure
2526 *
2527 * OneNAND detection method:
2528 * Compare the the values from command with ones from register
2529 */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002530static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park79e90462007-09-10 17:13:49 +09002531{
2532 struct onenand_chip *this = mtd->priv;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002533 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002534 int syscfg;
2535
2536 /* Save system configuration 1 */
2537 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002538
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002539 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002540 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2541 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park79e90462007-09-10 17:13:49 +09002542
2543 /* Send the command for reading device ID from BootRAM */
2544 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2545
2546 /* Read manufacturer and device IDs from BootRAM */
2547 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2548 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2549
Kyungmin Park79e90462007-09-10 17:13:49 +09002550 /* Reset OneNAND to read default register values */
2551 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2552
Kyungmin Park396b0c42008-08-13 09:11:02 +09002553 /* Wait reset */
Ladislav Michl243af2f2016-07-12 20:28:19 +02002554 if (this->wait(mtd, FL_RESETING))
2555 return -ENXIO;
Kyungmin Park79e90462007-09-10 17:13:49 +09002556
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002557 /* Restore system configuration 1 */
2558 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2559
2560 /* Check manufacturer ID */
2561 if (onenand_check_maf(bram_maf_id))
2562 return -ENXIO;
2563
Kyungmin Park79e90462007-09-10 17:13:49 +09002564 /* Read manufacturer and device IDs from Register */
2565 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2566 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2567
2568 /* Check OneNAND device */
2569 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2570 return -ENXIO;
Kyungmin Park05d23182008-03-17 08:54:06 +09002571
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002572 return 0;
2573}
2574
2575/**
2576 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2577 * @param mtd MTD device structure
2578 *
2579 * OneNAND detection method:
2580 * Compare the the values from command with ones from register
2581 */
2582int onenand_probe(struct mtd_info *mtd)
2583{
2584 struct onenand_chip *this = mtd->priv;
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002585 int dev_id, ver_id;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002586 int density;
2587 int ret;
2588
2589 ret = this->chip_probe(mtd);
2590 if (ret)
2591 return ret;
2592
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002593 /* Read device IDs from Register */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002594 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2595 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2596 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2597
Kyungmin Park79e90462007-09-10 17:13:49 +09002598 /* Flash device information */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002599 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park79e90462007-09-10 17:13:49 +09002600 this->device_id = dev_id;
Stefan Roese7c9aafe2008-11-11 10:29:09 +01002601 this->version_id = ver_id;
Kyungmin Park79e90462007-09-10 17:13:49 +09002602
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002603 /* Check OneNAND features */
2604 onenand_check_features(mtd);
2605
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002606 density = onenand_get_density(dev_id);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302607 if (FLEXONENAND(this)) {
2608 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2609 /* Maximum possible erase regions */
2610 mtd->numeraseregions = this->dies << 1;
2611 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2612 * (this->dies << 1));
2613 if (!mtd->eraseregions)
2614 return -ENOMEM;
2615 }
2616
2617 /*
2618 * For Flex-OneNAND, chipsize represents maximum possible device size.
2619 * mtd->size represents the actual device size.
2620 */
Kyungmin Park79e90462007-09-10 17:13:49 +09002621 this->chipsize = (16 << density) << 20;
2622
2623 /* OneNAND page size & block size */
2624 /* The data buffer size is equal to page size */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002625 mtd->writesize =
Kyungmin Park79e90462007-09-10 17:13:49 +09002626 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302627 /* We use the full BufferRAM */
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002628 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302629 mtd->writesize <<= 1;
2630
Kyungmin Park396b0c42008-08-13 09:11:02 +09002631 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park79e90462007-09-10 17:13:49 +09002632 /* Pagers per block is always 64 in OneNAND */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002633 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302634 /*
2635 * Flex-OneNAND SLC area has 64 pages per block.
2636 * Flex-OneNAND MLC area has 128 pages per block.
2637 * Expose MLC erase size to find erase_shift and page_mask.
2638 */
2639 if (FLEXONENAND(this))
2640 mtd->erasesize <<= 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002641
2642 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002643 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002644 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Park396b0c42008-08-13 09:11:02 +09002645 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302646 /* Set density mask. it is used for DDP */
2647 if (ONENAND_IS_DDP(this))
2648 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002649 /* It's real page size */
2650 this->writesize = mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002651
2652 /* REVIST: Multichip handling */
2653
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302654 if (FLEXONENAND(this))
2655 flexonenand_get_size(mtd);
2656 else
2657 mtd->size = this->chipsize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002658
Kyungmin Park396b0c42008-08-13 09:11:02 +09002659 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002660 mtd->_erase = onenand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002661 mtd->_read_oob = onenand_read_oob;
2662 mtd->_write_oob = onenand_write_oob;
2663 mtd->_sync = onenand_sync;
2664 mtd->_block_isbad = onenand_block_isbad;
2665 mtd->_block_markbad = onenand_block_markbad;
Ladislav Michl7dbad5c2016-07-12 20:28:21 +02002666 mtd->writebufsize = mtd->writesize;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002667
Kyungmin Park79e90462007-09-10 17:13:49 +09002668 return 0;
2669}
2670
2671/**
2672 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2673 * @param mtd MTD device structure
2674 * @param maxchips Number of chips to scan for
2675 *
2676 * This fills out all the not initialized function pointers
2677 * with the defaults.
2678 * The flash ID is read and the mtd/chip structures are
2679 * filled with the appropriate values.
2680 */
2681int onenand_scan(struct mtd_info *mtd, int maxchips)
2682{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002683 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002684 struct onenand_chip *this = mtd->priv;
2685
2686 if (!this->read_word)
2687 this->read_word = onenand_readw;
2688 if (!this->write_word)
2689 this->write_word = onenand_writew;
2690
2691 if (!this->command)
2692 this->command = onenand_command;
2693 if (!this->wait)
2694 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002695 if (!this->bbt_wait)
2696 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002697
2698 if (!this->read_bufferram)
2699 this->read_bufferram = onenand_read_bufferram;
2700 if (!this->write_bufferram)
2701 this->write_bufferram = onenand_write_bufferram;
2702
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002703 if (!this->chip_probe)
2704 this->chip_probe = onenand_chip_probe;
2705
Kyungmin Park87b49502008-11-13 15:14:33 +09002706 if (!this->block_markbad)
2707 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002708 if (!this->scan_bbt)
2709 this->scan_bbt = onenand_default_bbt;
2710
Kyungmin Park79e90462007-09-10 17:13:49 +09002711 if (onenand_probe(mtd))
2712 return -ENXIO;
2713
2714 /* Set Sync. Burst Read after probing */
2715 if (this->mmcontrol) {
2716 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2717 this->read_bufferram = onenand_sync_read_bufferram;
2718 }
2719
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002720 /* Allocate buffers, if necessary */
2721 if (!this->page_buf) {
2722 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2723 if (!this->page_buf) {
2724 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2725 return -ENOMEM;
2726 }
2727 this->options |= ONENAND_PAGEBUF_ALLOC;
2728 }
2729 if (!this->oob_buf) {
2730 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2731 if (!this->oob_buf) {
2732 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2733 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2734 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2735 kfree(this->page_buf);
2736 }
2737 return -ENOMEM;
2738 }
2739 this->options |= ONENAND_OOBBUF_ALLOC;
2740 }
2741
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002742 this->state = FL_READY;
2743
2744 /*
2745 * Allow subpage writes up to oobsize.
2746 */
2747 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302748 case 128:
2749 this->ecclayout = &onenand_oob_128;
2750 mtd->subpage_sft = 0;
2751 break;
2752
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002753 case 64:
2754 this->ecclayout = &onenand_oob_64;
2755 mtd->subpage_sft = 2;
2756 break;
2757
2758 case 32:
2759 this->ecclayout = &onenand_oob_32;
2760 mtd->subpage_sft = 1;
2761 break;
2762
2763 default:
2764 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2765 mtd->oobsize);
2766 mtd->subpage_sft = 0;
2767 /* To prevent kernel oops */
2768 this->ecclayout = &onenand_oob_32;
2769 break;
2770 }
2771
2772 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2773
2774 /*
2775 * The number of bytes available for a client to place data into
2776 * the out of band area
2777 */
2778 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302779
2780 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002781 this->ecclayout->oobfree[i].length; i++)
2782 this->ecclayout->oobavail +=
2783 this->ecclayout->oobfree[i].length;
2784 mtd->oobavail = this->ecclayout->oobavail;
2785
2786 mtd->ecclayout = this->ecclayout;
2787
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002788 /* Unlock whole block */
2789 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002790
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002791 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002792}
2793
2794/**
2795 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2796 * @param mtd MTD device structure
2797 */
2798void onenand_release(struct mtd_info *mtd)
2799{
2800}