blob: 693bb78b87cfe287d306f9518acd6719af2a8200 [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>
Ladislav Michl243af2f2016-07-12 20:28:19 +020023#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070024#include <dm/devres.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000025#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090026#include <linux/mtd/mtd.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020027#include "linux/mtd/flashchip.h"
Kyungmin Park79e90462007-09-10 17:13:49 +090028#include <linux/mtd/onenand.h>
29
30#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090031#include <linux/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020032#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090033
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090034/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053035static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090036{
37 void *ret = dst;
38 short *d = dst;
39 const short *s = src;
40
41 len >>= 1;
42 while (len-- > 0)
43 *d++ = *s++;
44 return ret;
45}
46
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010047/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053048 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
49 * For now, we expose only 64 out of 80 ecc bytes
50 */
51static struct nand_ecclayout onenand_oob_128 = {
52 .eccbytes = 64,
53 .eccpos = {
54 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
55 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
56 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
57 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
58 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
59 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
60 102, 103, 104, 105
61 },
62 .oobfree = {
63 {2, 4}, {18, 4}, {34, 4}, {50, 4},
64 {66, 4}, {82, 4}, {98, 4}, {114, 4}
65 }
66};
67
68/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010069 * onenand_oob_64 - oob info for large (2KB) page
70 */
71static struct nand_ecclayout onenand_oob_64 = {
72 .eccbytes = 20,
73 .eccpos = {
74 8, 9, 10, 11, 12,
75 24, 25, 26, 27, 28,
76 40, 41, 42, 43, 44,
77 56, 57, 58, 59, 60,
78 },
79 .oobfree = {
80 {2, 3}, {14, 2}, {18, 3}, {30, 2},
81 {34, 3}, {46, 2}, {50, 3}, {62, 2}
82 }
83};
84
85/**
86 * onenand_oob_32 - oob info for middle (1KB) page
87 */
88static struct nand_ecclayout onenand_oob_32 = {
89 .eccbytes = 10,
90 .eccpos = {
91 8, 9, 10, 11, 12,
92 24, 25, 26, 27, 28,
93 },
94 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
95};
96
Marek Vasut41120122013-12-26 01:01:24 +010097/*
98 * Warning! This array is used with the memcpy_16() function, thus
99 * it must be aligned to 2 bytes. GCC can make this array unaligned
100 * as the array is made of unsigned char, which memcpy16() doesn't
101 * like and will cause unaligned access.
102 */
103static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park79e90462007-09-10 17:13:49 +0900104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900120};
121
122/**
123 * onenand_readw - [OneNAND Interface] Read OneNAND register
124 * @param addr address to read
125 *
126 * Read OneNAND register
127 */
128static unsigned short onenand_readw(void __iomem * addr)
129{
130 return readw(addr);
131}
132
133/**
134 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
135 * @param value value to write
136 * @param addr address to write
137 *
138 * Write OneNAND register with value
139 */
140static void onenand_writew(unsigned short value, void __iomem * addr)
141{
142 writew(value, addr);
143}
144
145/**
146 * onenand_block_address - [DEFAULT] Get block address
147 * @param device the device id
148 * @param block the block
149 * @return translated block address if DDP, otherwise same
150 *
151 * Setup Start Address 1 Register (F100h)
152 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900153static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900154{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900155 /* Device Flash Core select, NAND Flash Block Address */
156 if (block & this->density_mask)
157 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900158
159 return block;
160}
161
162/**
163 * onenand_bufferram_address - [DEFAULT] Get bufferram address
164 * @param device the device id
165 * @param block the block
166 * @return set DBS value if DDP, otherwise 0
167 *
168 * Setup Start Address 2 Register (F101h) for DDP
169 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900170static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900171{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900172 /* Device BufferRAM Select */
173 if (block & this->density_mask)
174 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900175
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900176 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900177}
178
179/**
180 * onenand_page_address - [DEFAULT] Get page address
181 * @param page the page address
182 * @param sector the sector address
183 * @return combined page and sector address
184 *
185 * Setup Start Address 8 Register (F107h)
186 */
187static int onenand_page_address(int page, int sector)
188{
189 /* Flash Page Address, Flash Sector Address */
190 int fpa, fsa;
191
192 fpa = page & ONENAND_FPA_MASK;
193 fsa = sector & ONENAND_FSA_MASK;
194
195 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
196}
197
198/**
199 * onenand_buffer_address - [DEFAULT] Get buffer address
200 * @param dataram1 DataRAM index
201 * @param sectors the sector address
202 * @param count the number of sectors
203 * @return the start buffer value
204 *
205 * Setup Start Buffer Register (F200h)
206 */
207static int onenand_buffer_address(int dataram1, int sectors, int count)
208{
209 int bsa, bsc;
210
211 /* BufferRAM Sector Address */
212 bsa = sectors & ONENAND_BSA_MASK;
213
214 if (dataram1)
215 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
216 else
217 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
218
219 /* BufferRAM Sector Count */
220 bsc = count & ONENAND_BSC_MASK;
221
222 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
223}
224
225/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530226 * flexonenand_block - Return block number for flash address
227 * @param this - OneNAND device structure
228 * @param addr - Address for which block number is needed
229 */
230static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
231{
232 unsigned int boundary, blk, die = 0;
233
234 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
235 die = 1;
236 addr -= this->diesize[0];
237 }
238
239 boundary = this->boundary[die];
240
241 blk = addr >> (this->erase_shift - 1);
242 if (blk > boundary)
243 blk = (blk + boundary + 1) >> 1;
244
245 blk += die ? this->density_mask : 0;
246 return blk;
247}
248
249unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
250{
251 if (!FLEXONENAND(this))
252 return addr >> this->erase_shift;
253 return flexonenand_block(this, addr);
254}
255
256/**
257 * flexonenand_addr - Return address of the block
258 * @this: OneNAND device structure
259 * @block: Block number on Flex-OneNAND
260 *
261 * Return address of the block
262 */
263static loff_t flexonenand_addr(struct onenand_chip *this, int block)
264{
265 loff_t ofs = 0;
266 int die = 0, boundary;
267
268 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
269 block -= this->density_mask;
270 die = 1;
271 ofs = this->diesize[0];
272 }
273
274 boundary = this->boundary[die];
275 ofs += (loff_t) block << (this->erase_shift - 1);
276 if (block > (boundary + 1))
277 ofs += (loff_t) (block - boundary - 1)
278 << (this->erase_shift - 1);
279 return ofs;
280}
281
282loff_t onenand_addr(struct onenand_chip *this, int block)
283{
284 if (!FLEXONENAND(this))
285 return (loff_t) block << this->erase_shift;
286 return flexonenand_addr(this, block);
287}
288
289/**
290 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
291 * @param mtd MTD device structure
292 * @param addr address whose erase region needs to be identified
293 */
294int flexonenand_region(struct mtd_info *mtd, loff_t addr)
295{
296 int i;
297
298 for (i = 0; i < mtd->numeraseregions; i++)
299 if (addr < mtd->eraseregions[i].offset)
300 break;
301 return i - 1;
302}
303
304/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900305 * onenand_get_density - [DEFAULT] Get OneNAND density
306 * @param dev_id OneNAND device ID
307 *
308 * Get OneNAND density from device ID
309 */
310static inline int onenand_get_density(int dev_id)
311{
312 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
313 return (density & ONENAND_DEVICE_DENSITY_MASK);
314}
315
316/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900317 * onenand_command - [DEFAULT] Send command to OneNAND device
318 * @param mtd MTD device structure
319 * @param cmd the command to be sent
320 * @param addr offset to read from or write to
321 * @param len number of bytes to read or write
322 *
323 * Send command to OneNAND device. This function is used for middle/large page
324 * devices (1KB/2KB Bytes per page)
325 */
326static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
327 size_t len)
328{
329 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530330 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900331 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530332
Kyungmin Park79e90462007-09-10 17:13:49 +0900333 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530334 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900335
336 /* Address translation */
337 switch (cmd) {
338 case ONENAND_CMD_UNLOCK:
339 case ONENAND_CMD_LOCK:
340 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900341 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900342 block = -1;
343 page = -1;
344 break;
345
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530346 case FLEXONENAND_CMD_PI_ACCESS:
347 /* addr contains die index */
348 block = addr * this->density_mask;
349 page = -1;
350 break;
351
Kyungmin Park79e90462007-09-10 17:13:49 +0900352 case ONENAND_CMD_ERASE:
353 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530354 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900355 page = -1;
356 break;
357
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530358 case FLEXONENAND_CMD_READ_PI:
359 cmd = ONENAND_CMD_READ;
360 block = addr * this->density_mask;
361 page = 0;
362 break;
363
Kyungmin Park79e90462007-09-10 17:13:49 +0900364 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530365 block = onenand_block(this, addr);
366 page = (int) (addr
367 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900368 page &= this->page_mask;
369 break;
370 }
371
372 /* NOTE: The setting order of the registers is very important! */
373 if (cmd == ONENAND_CMD_BUFFERRAM) {
374 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900375 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900376 this->write_word(value,
377 this->base + ONENAND_REG_START_ADDRESS2);
378
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100379 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530380 ONENAND_SET_BUFFERRAM0(this);
381 else
382 /* Switch to the next data buffer */
383 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900384
385 return 0;
386 }
387
388 if (block != -1) {
389 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900390 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900391 this->write_word(value,
392 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900393
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530394 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900395 value = onenand_bufferram_address(this, block);
396 this->write_word(value,
397 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900398 }
399
400 if (page != -1) {
401 int dataram;
402
403 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530404 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900405 case ONENAND_CMD_READ:
406 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100407 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530408 dataram = ONENAND_SET_BUFFERRAM0(this);
409 else
410 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
411
Kyungmin Park79e90462007-09-10 17:13:49 +0900412 break;
413
414 default:
415 dataram = ONENAND_CURRENT_BUFFERRAM(this);
416 break;
417 }
418
419 /* Write 'FPA, FSA' of Flash */
420 value = onenand_page_address(page, sectors);
421 this->write_word(value,
422 this->base + ONENAND_REG_START_ADDRESS8);
423
424 /* Write 'BSA, BSC' of DataRAM */
425 value = onenand_buffer_address(dataram, sectors, count);
426 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900427 }
428
429 /* Interrupt clear */
430 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
431 /* Write command */
432 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
433
434 return 0;
435}
436
437/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530438 * onenand_read_ecc - return ecc status
439 * @param this onenand chip structure
440 */
441static int onenand_read_ecc(struct onenand_chip *this)
442{
443 int ecc, i;
444
445 if (!FLEXONENAND(this))
446 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
447
448 for (i = 0; i < 4; i++) {
449 ecc = this->read_word(this->base
450 + ((ONENAND_REG_ECC_STATUS + i) << 1));
451 if (likely(!ecc))
452 continue;
453 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
454 return ONENAND_ECC_2BIT_ALL;
455 }
456
457 return 0;
458}
459
460/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900461 * onenand_wait - [DEFAULT] wait until the command is done
462 * @param mtd MTD device structure
463 * @param state state to select the max. timeout value
464 *
465 * Wait for command done. This applies to all OneNAND command
466 * Read can take up to 30us, erase up to 2ms and program up to 350us
467 * according to general OneNAND specs
468 */
469static int onenand_wait(struct mtd_info *mtd, int state)
470{
471 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +0900472 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530473 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900474
Ladislav Michl243af2f2016-07-12 20:28:19 +0200475 /* Wait at most 20ms ... */
476 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
477 u32 time_start = get_timer(0);
478 do {
479 WATCHDOG_RESET();
480 if (get_timer(time_start) > timeo)
481 return -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +0900482 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +0200483 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park79e90462007-09-10 17:13:49 +0900484
485 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
486
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530487 if (interrupt & ONENAND_INT_READ) {
488 int ecc = onenand_read_ecc(this);
489 if (ecc & ONENAND_ECC_2BIT_ALL) {
490 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
491 return -EBADMSG;
492 }
493 }
494
Kyungmin Park79e90462007-09-10 17:13:49 +0900495 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900496 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
497 if (ctrl & ONENAND_CTRL_LOCK)
498 printk("onenand_wait: it's locked error = 0x%04x\n",
499 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900500
Kyungmin Park79e90462007-09-10 17:13:49 +0900501 return -EIO;
502 }
503
Kyungmin Park79e90462007-09-10 17:13:49 +0900504
505 return 0;
506}
507
508/**
509 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
510 * @param mtd MTD data structure
511 * @param area BufferRAM area
512 * @return offset given area
513 *
514 * Return BufferRAM offset given area
515 */
516static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
517{
518 struct onenand_chip *this = mtd->priv;
519
520 if (ONENAND_CURRENT_BUFFERRAM(this)) {
521 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900522 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900523 if (area == ONENAND_SPARERAM)
524 return mtd->oobsize;
525 }
526
527 return 0;
528}
529
530/**
531 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
532 * @param mtd MTD data structure
533 * @param area BufferRAM area
534 * @param buffer the databuffer to put/get data
535 * @param offset offset to read from or write to
536 * @param count number of bytes to read/write
537 *
538 * Read the BufferRAM area
539 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900540static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900541 unsigned char *buffer, int offset,
542 size_t count)
543{
544 struct onenand_chip *this = mtd->priv;
545 void __iomem *bufferram;
546
547 bufferram = this->base + area;
548 bufferram += onenand_bufferram_offset(mtd, area);
549
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200550 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900551
552 return 0;
553}
554
555/**
556 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
557 * @param mtd MTD data structure
558 * @param area BufferRAM area
559 * @param buffer the databuffer to put/get data
560 * @param offset offset to read from or write to
561 * @param count number of bytes to read/write
562 *
563 * Read the BufferRAM area with Sync. Burst Mode
564 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900565static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900566 unsigned char *buffer, int offset,
567 size_t count)
568{
569 struct onenand_chip *this = mtd->priv;
570 void __iomem *bufferram;
571
572 bufferram = this->base + area;
573 bufferram += onenand_bufferram_offset(mtd, area);
574
575 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
576
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200577 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900578
579 this->mmcontrol(mtd, 0);
580
581 return 0;
582}
583
584/**
585 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
586 * @param mtd MTD data structure
587 * @param area BufferRAM area
588 * @param buffer the databuffer to put/get data
589 * @param offset offset to read from or write to
590 * @param count number of bytes to read/write
591 *
592 * Write the BufferRAM area
593 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900594static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900595 const unsigned char *buffer, int offset,
596 size_t count)
597{
598 struct onenand_chip *this = mtd->priv;
599 void __iomem *bufferram;
600
601 bufferram = this->base + area;
602 bufferram += onenand_bufferram_offset(mtd, area);
603
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200604 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900605
606 return 0;
607}
608
609/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100610 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
611 * @param mtd MTD data structure
612 * @param addr address to check
613 * @return blockpage address
614 *
615 * Get blockpage address at 2x program mode
616 */
617static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
618{
619 struct onenand_chip *this = mtd->priv;
620 int blockpage, block, page;
621
622 /* Calculate the even block number */
623 block = (int) (addr >> this->erase_shift) & ~1;
624 /* Is it the odd plane? */
625 if (addr & this->writesize)
626 block++;
627 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
628 blockpage = (block << 7) | page;
629
630 return blockpage;
631}
632
633/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900634 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
635 * @param mtd MTD data structure
636 * @param addr address to check
637 * @return 1 if there are valid data, otherwise 0
638 *
639 * Check bufferram if there is data we required
640 */
641static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
642{
643 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900644 int blockpage, found = 0;
645 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900646
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900647 if (ONENAND_IS_2PLANE(this))
648 blockpage = onenand_get_2x_blockpage(mtd, addr);
649 else
650 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900651
652 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900653 i = ONENAND_CURRENT_BUFFERRAM(this);
654 if (this->bufferram[i].blockpage == blockpage)
655 found = 1;
656 else {
657 /* Check another BufferRAM */
658 i = ONENAND_NEXT_BUFFERRAM(this);
659 if (this->bufferram[i].blockpage == blockpage) {
660 ONENAND_SET_NEXT_BUFFERRAM(this);
661 found = 1;
662 }
663 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900664
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900665 if (found && ONENAND_IS_DDP(this)) {
666 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530667 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900668 int value = onenand_bufferram_address(this, block);
669 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
670 }
671
672 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900673}
674
675/**
676 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
677 * @param mtd MTD data structure
678 * @param addr address to update
679 * @param valid valid flag
680 *
681 * Update BufferRAM information
682 */
683static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
684 int valid)
685{
686 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900687 int blockpage;
688 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900689
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900690 if (ONENAND_IS_2PLANE(this))
691 blockpage = onenand_get_2x_blockpage(mtd, addr);
692 else
693 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900694
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900695 /* Invalidate another BufferRAM */
696 i = ONENAND_NEXT_BUFFERRAM(this);
697 if (this->bufferram[i].blockpage == blockpage)
698 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900699
700 /* Update BufferRAM */
701 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900702 if (valid)
703 this->bufferram[i].blockpage = blockpage;
704 else
705 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900706
707 return 0;
708}
709
710/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900711 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
712 * @param mtd MTD data structure
713 * @param addr start address to invalidate
714 * @param len length to invalidate
715 *
716 * Invalidate BufferRAM information
717 */
718static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200719 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900720{
721 struct onenand_chip *this = mtd->priv;
722 int i;
723 loff_t end_addr = addr + len;
724
725 /* Invalidate BufferRAM */
726 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900727 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900728
729 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900730 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900731 }
732}
733
734/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900735 * onenand_get_device - [GENERIC] Get chip for selected access
736 * @param mtd MTD device structure
737 * @param new_state the state which is requested
738 *
739 * Get the device and lock it for exclusive access
740 */
741static void onenand_get_device(struct mtd_info *mtd, int new_state)
742{
743 /* Do nothing */
744}
745
746/**
747 * onenand_release_device - [GENERIC] release chip
748 * @param mtd MTD device structure
749 *
750 * Deselect, release chip lock and wake up anyone waiting on the device
751 */
752static void onenand_release_device(struct mtd_info *mtd)
753{
754 /* Do nothing */
755}
756
757/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000758 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900759 * @param mtd MTD device structure
760 * @param buf destination address
761 * @param column oob offset to read from
762 * @param thislen oob length to read
763 */
764static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
765 int column, int thislen)
766{
767 struct onenand_chip *this = mtd->priv;
768 struct nand_oobfree *free;
769 int readcol = column;
770 int readend = column + thislen;
771 int lastgap = 0;
772 unsigned int i;
773 uint8_t *oob_buf = this->oob_buf;
774
775 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530776 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
777 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900778 if (readcol >= lastgap)
779 readcol += free->offset - lastgap;
780 if (readend >= lastgap)
781 readend += free->offset - lastgap;
782 lastgap = free->offset + free->length;
783 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900784 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900785 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530786 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
787 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900788 int free_end = free->offset + free->length;
789 if (free->offset < readend && free_end > readcol) {
790 int st = max_t(int,free->offset,readcol);
791 int ed = min_t(int,free_end,readend);
792 int n = ed - st;
793 memcpy(buf, oob_buf + st, n);
794 buf += n;
795 } else if (column == 0)
796 break;
797 }
798 return 0;
799}
800
801/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530802 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
803 * @param mtd MTD device structure
804 * @param addr address to recover
805 * @param status return value from onenand_wait
806 *
807 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
808 * lower page address and MSB page has higher page address in paired pages.
809 * If power off occurs during MSB page program, the paired LSB page data can
810 * become corrupt. LSB page recovery read is a way to read LSB page though page
811 * data are corrupted. When uncorrectable error occurs as a result of LSB page
812 * read after power up, issue LSB page recovery read.
813 */
814static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
815{
816 struct onenand_chip *this = mtd->priv;
817 int i;
818
819 /* Recovery is only for Flex-OneNAND */
820 if (!FLEXONENAND(this))
821 return status;
822
823 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000824 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530825 return status;
826
827 /* check if address lies in MLC region */
828 i = flexonenand_region(mtd, addr);
829 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
830 return status;
831
832 printk("onenand_recover_lsb:"
833 "Attempting to recover from uncorrectable read\n");
834
835 /* Issue the LSB page recovery command */
836 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
837 return this->wait(mtd, FL_READING);
838}
839
840/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900841 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900842 * @param mtd MTD device structure
843 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900844 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900845 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900846 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900847 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900848static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
849 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900850{
851 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900852 struct mtd_ecc_stats stats;
853 size_t len = ops->len;
854 size_t ooblen = ops->ooblen;
855 u_char *buf = ops->datbuf;
856 u_char *oobbuf = ops->oobbuf;
857 int read = 0, column, thislen;
858 int oobread = 0, oobcolumn, thisooblen, oobsize;
859 int ret = 0, boundary = 0;
860 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900861
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900862 pr_debug("onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
863 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900864
Sergey Lapin3a38a552013-01-14 03:46:50 +0000865 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900866 oobsize = this->ecclayout->oobavail;
867 else
868 oobsize = mtd->oobsize;
869
870 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900871
872 /* Do not allow reads past end of device */
873 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900874 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
875 ops->retlen = 0;
876 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900877 return -EINVAL;
878 }
879
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900880 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900881
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900882 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530883 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900884
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900885 /* Do first load to bufferRAM */
886 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900887 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900888 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900889 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900890 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530891 if (unlikely(ret))
892 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900893 onenand_update_bufferram(mtd, from, !ret);
894 if (ret == -EBADMSG)
895 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900896 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900897 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900898
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900899 thislen = min_t(int, writesize, len - read);
900 column = from & (writesize - 1);
901 if (column + thislen > writesize)
902 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900903
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900904 while (!ret) {
905 /* If there is more to load then start next load */
906 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100907 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900908 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900909 this->command(mtd, ONENAND_CMD_READ, from, writesize);
910 /*
911 * Chip boundary handling in DDP
912 * Now we issued chip 1 read and pointed chip 1
913 * bufferam so we have to point chip 0 bufferam.
914 */
915 if (ONENAND_IS_DDP(this) &&
916 unlikely(from == (this->chipsize >> 1))) {
917 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
918 boundary = 1;
919 } else
920 boundary = 0;
921 ONENAND_SET_PREV_BUFFERRAM(this);
922 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900923
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900924 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900925 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900926
927 /* Read oob area if needed */
928 if (oobbuf) {
929 thisooblen = oobsize - oobcolumn;
930 thisooblen = min_t(int, thisooblen, ooblen - oobread);
931
Sergey Lapin3a38a552013-01-14 03:46:50 +0000932 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900933 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
934 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900935 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900936 oobread += thisooblen;
937 oobbuf += thisooblen;
938 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900939 }
940
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100941 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530942 this->command(mtd, ONENAND_CMD_READ, from, writesize);
943 ret = this->wait(mtd, FL_READING);
944 if (unlikely(ret))
945 ret = onenand_recover_lsb(mtd, from, ret);
946 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000947 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530948 ret = 0;
949 }
950
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900951 /* See if we are done */
952 read += thislen;
953 if (read == len)
954 break;
955 /* Set up for next read from bufferRAM */
956 if (unlikely(boundary))
957 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100958 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530959 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900960 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900961 thislen = min_t(int, writesize, len - read);
962 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900963
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100964 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530965 /* Now wait for load */
966 ret = this->wait(mtd, FL_READING);
967 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000968 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530969 ret = 0;
970 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900971 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900972
973 /*
974 * Return success, if no ECC failures, else -EBADMSG
975 * fs driver will take care of that, because
976 * retlen == desired len and result == -EBADMSG
977 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900978 ops->retlen = read;
979 ops->oobretlen = oobread;
980
981 if (ret)
982 return ret;
983
984 if (mtd->ecc_stats.failed - stats.failed)
985 return -EBADMSG;
986
Paul Burton700a76c2013-09-04 15:16:56 +0100987 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
988 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900989}
990
991/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900992 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
993 * @param mtd MTD device structure
994 * @param from offset to read from
995 * @param ops oob operation description structure
996 *
997 * OneNAND read out-of-band data from the spare area
998 */
999static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1000 struct mtd_oob_ops *ops)
1001{
1002 struct onenand_chip *this = mtd->priv;
1003 struct mtd_ecc_stats stats;
1004 int read = 0, thislen, column, oobsize;
1005 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001006 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001007 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301008 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001009
1010 from += ops->ooboffs;
1011
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001012 pr_debug("onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
1013 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001014
1015 /* Initialize return length value */
1016 ops->oobretlen = 0;
1017
Sergey Lapin3a38a552013-01-14 03:46:50 +00001018 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001019 oobsize = this->ecclayout->oobavail;
1020 else
1021 oobsize = mtd->oobsize;
1022
1023 column = from & (mtd->oobsize - 1);
1024
1025 if (unlikely(column >= oobsize)) {
1026 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1027 return -EINVAL;
1028 }
1029
1030 /* Do not allow reads past end of device */
1031 if (unlikely(from >= mtd->size ||
1032 column + len > ((mtd->size >> this->page_shift) -
1033 (from >> this->page_shift)) * oobsize)) {
1034 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1035 return -EINVAL;
1036 }
1037
1038 stats = mtd->ecc_stats;
1039
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001040 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1041 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301042
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001043 while (read < len) {
1044 thislen = oobsize - column;
1045 thislen = min_t(int, thislen, len);
1046
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001047 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301048 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001049
1050 onenand_update_bufferram(mtd, from, 0);
1051
1052 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301053 if (unlikely(ret))
1054 ret = onenand_recover_lsb(mtd, from, ret);
1055
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001056 if (ret && ret != -EBADMSG) {
1057 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1058 break;
1059 }
1060
Sergey Lapin3a38a552013-01-14 03:46:50 +00001061 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001062 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1063 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001064 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001065
1066 read += thislen;
1067
1068 if (read == len)
1069 break;
1070
1071 buf += thislen;
1072
1073 /* Read more? */
1074 if (read < len) {
1075 /* Page size */
1076 from += mtd->writesize;
1077 column = 0;
1078 }
1079 }
1080
1081 ops->oobretlen = read;
1082
1083 if (ret)
1084 return ret;
1085
1086 if (mtd->ecc_stats.failed - stats.failed)
1087 return -EBADMSG;
1088
1089 return 0;
1090}
1091
1092/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001093 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1094 * @param mtd MTD device structure
1095 * @param from offset to read from
1096 * @param len number of bytes to read
1097 * @param retlen pointer to variable to store the number of read bytes
1098 * @param buf the databuffer to put data
1099 *
1100 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1101*/
1102int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1103 size_t * retlen, u_char * buf)
1104{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001105 struct mtd_oob_ops ops = {
1106 .len = len,
1107 .ooblen = 0,
1108 .datbuf = buf,
1109 .oobbuf = NULL,
1110 };
1111 int ret;
1112
1113 onenand_get_device(mtd, FL_READING);
1114 ret = onenand_read_ops_nolock(mtd, from, &ops);
1115 onenand_release_device(mtd);
1116
1117 *retlen = ops.retlen;
1118 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001119}
1120
1121/**
1122 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1123 * @param mtd MTD device structure
1124 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001125 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001126 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001127 * OneNAND main and/or out-of-band
1128 */
1129int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1130 struct mtd_oob_ops *ops)
1131{
1132 int ret;
1133
1134 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001135 case MTD_OPS_PLACE_OOB:
1136 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001137 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001138 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001139 /* Not implemented yet */
1140 default:
1141 return -EINVAL;
1142 }
1143
1144 onenand_get_device(mtd, FL_READING);
1145 if (ops->datbuf)
1146 ret = onenand_read_ops_nolock(mtd, from, ops);
1147 else
1148 ret = onenand_read_oob_nolock(mtd, from, ops);
1149 onenand_release_device(mtd);
1150
1151 return ret;
1152}
1153
1154/**
1155 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1156 * @param mtd MTD device structure
1157 * @param state state to select the max. timeout value
1158 *
1159 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001160 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001161static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001162{
1163 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001164 unsigned int interrupt;
1165 unsigned int ctrl;
1166
Ladislav Michl243af2f2016-07-12 20:28:19 +02001167 /* Wait at most 20ms ... */
1168 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
1169 u32 time_start = get_timer(0);
1170 do {
1171 WATCHDOG_RESET();
1172 if (get_timer(time_start) > timeo)
1173 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001174 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +02001175 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001176
1177 /* To get correct interrupt status in timeout case */
1178 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1179 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1180
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001181 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301182 int ecc = onenand_read_ecc(this);
1183 if (ecc & ONENAND_ECC_2BIT_ALL) {
1184 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1185 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001186 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301187 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001188 } else {
1189 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1190 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1191 return ONENAND_BBT_READ_FATAL_ERROR;
1192 }
1193
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001194 /* Initial bad block case: 0x2400 or 0x0400 */
1195 if (ctrl & ONENAND_CTRL_ERROR) {
1196 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1197 return ONENAND_BBT_READ_ERROR;
1198 }
1199
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001200 return 0;
1201}
1202
1203/**
1204 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1205 * @param mtd MTD device structure
1206 * @param from offset to read from
1207 * @param ops oob operation description structure
1208 *
1209 * OneNAND read out-of-band data from the spare area for bbt scan
1210 */
1211int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1212 struct mtd_oob_ops *ops)
1213{
1214 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001215 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301216 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001217 size_t len = ops->ooblen;
1218 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001219
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001220 pr_debug("onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
1221 (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001222
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001223 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1224 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301225
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001226 /* Initialize return value */
1227 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001228
1229 /* Do not allow reads past end of device */
1230 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001231 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1232 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001233 }
1234
1235 /* Grab the lock and see if the device is available */
1236 onenand_get_device(mtd, FL_READING);
1237
1238 column = from & (mtd->oobsize - 1);
1239
1240 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001241
Kyungmin Park79e90462007-09-10 17:13:49 +09001242 thislen = mtd->oobsize - column;
1243 thislen = min_t(int, thislen, len);
1244
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001245 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301246 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001247
1248 onenand_update_bufferram(mtd, from, 0);
1249
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001250 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301251 if (unlikely(ret))
1252 ret = onenand_recover_lsb(mtd, from, ret);
1253
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001254 if (ret)
1255 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001256
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001257 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001258 read += thislen;
1259 if (read == len)
1260 break;
1261
Kyungmin Park79e90462007-09-10 17:13:49 +09001262 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001263
Kyungmin Park79e90462007-09-10 17:13:49 +09001264 /* Read more? */
1265 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001266 /* Update Page size */
1267 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001268 column = 0;
1269 }
1270 }
1271
1272 /* Deselect and wake up anyone waiting on the device */
1273 onenand_release_device(mtd);
1274
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001275 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001276 return ret;
1277}
1278
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001279
Kyungmin Park79e90462007-09-10 17:13:49 +09001280#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1281/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001282 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1283 * @param mtd MTD device structure
1284 * @param buf the databuffer to verify
1285 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001286 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001287static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001288{
1289 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001290 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301291 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001292
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001293 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1294 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301295
1296 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001297 onenand_update_bufferram(mtd, to, 0);
1298 status = this->wait(mtd, FL_READING);
1299 if (status)
1300 return status;
1301
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001302 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001303 for (i = 0; i < mtd->oobsize; i++)
1304 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1305 return -EBADMSG;
1306
1307 return 0;
1308}
1309
1310/**
1311 * onenand_verify - [GENERIC] verify the chip contents after a write
1312 * @param mtd MTD device structure
1313 * @param buf the databuffer to verify
1314 * @param addr offset to read from
1315 * @param len number of bytes to read and compare
1316 */
1317static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1318{
1319 struct onenand_chip *this = mtd->priv;
1320 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001321 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001322 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001323
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001324 while (len != 0) {
1325 thislen = min_t(int, this->writesize, len);
1326 column = addr & (this->writesize - 1);
1327 if (column + thislen > this->writesize)
1328 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001329
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001330 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001331
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001332 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001333
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001334 ret = this->wait(mtd, FL_READING);
1335 if (ret)
1336 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001337
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001338 onenand_update_bufferram(mtd, addr, 1);
1339
1340 dataram = this->base + ONENAND_DATARAM;
1341 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1342
1343 if (memcmp(buf, dataram + column, thislen))
1344 return -EBADMSG;
1345
1346 len -= thislen;
1347 buf += thislen;
1348 addr += thislen;
1349 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001350
1351 return 0;
1352}
1353#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001354#define onenand_verify(...) (0)
1355#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001356#endif
1357
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001358#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001359
1360/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001361 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001362 * @param mtd MTD device structure
1363 * @param oob_buf oob buffer
1364 * @param buf source address
1365 * @param column oob offset to write to
1366 * @param thislen oob length to write
1367 */
1368static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1369 const u_char *buf, int column, int thislen)
1370{
1371 struct onenand_chip *this = mtd->priv;
1372 struct nand_oobfree *free;
1373 int writecol = column;
1374 int writeend = column + thislen;
1375 int lastgap = 0;
1376 unsigned int i;
1377
1378 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301379 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1380 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001381 if (writecol >= lastgap)
1382 writecol += free->offset - lastgap;
1383 if (writeend >= lastgap)
1384 writeend += free->offset - lastgap;
1385 lastgap = free->offset + free->length;
1386 }
1387 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301388 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1389 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001390 int free_end = free->offset + free->length;
1391 if (free->offset < writeend && free_end > writecol) {
1392 int st = max_t(int,free->offset,writecol);
1393 int ed = min_t(int,free_end,writeend);
1394 int n = ed - st;
1395 memcpy(oob_buf + st, buf, n);
1396 buf += n;
1397 } else if (column == 0)
1398 break;
1399 }
1400 return 0;
1401}
1402
1403/**
1404 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1405 * @param mtd MTD device structure
1406 * @param to offset to write to
1407 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001408 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001409 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001410 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001411static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1412 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001413{
1414 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001415 int written = 0, column, thislen, subpage;
1416 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1417 size_t len = ops->len;
1418 size_t ooblen = ops->ooblen;
1419 const u_char *buf = ops->datbuf;
1420 const u_char *oob = ops->oobbuf;
1421 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001422 int ret = 0;
1423
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001424 pr_debug("onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
1425 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001426
1427 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001428 ops->retlen = 0;
1429 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001430
Kyungmin Park79e90462007-09-10 17:13:49 +09001431 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001432 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1433 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001434 return -EINVAL;
1435 }
1436
Sergey Lapin3a38a552013-01-14 03:46:50 +00001437 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001438 oobsize = this->ecclayout->oobavail;
1439 else
1440 oobsize = mtd->oobsize;
1441
1442 oobcolumn = to & (mtd->oobsize - 1);
1443
1444 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001445
1446 /* Loop until all data write */
1447 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001448 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001449
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001450 thislen = min_t(int, mtd->writesize - column, len - written);
1451 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001452
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001453 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001454
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001455 /* Partial page write */
1456 subpage = thislen < mtd->writesize;
1457 if (subpage) {
1458 memset(this->page_buf, 0xff, mtd->writesize);
1459 memcpy(this->page_buf + column, buf, thislen);
1460 wbuf = this->page_buf;
1461 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001462
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001463 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001464
1465 if (oob) {
1466 oobbuf = this->oob_buf;
1467
1468 /* We send data to spare ram with oobsize
1469 * * to prevent byte access */
1470 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001471 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001472 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1473 else
1474 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1475
1476 oobwritten += thisooblen;
1477 oob += thisooblen;
1478 oobcolumn = 0;
1479 } else
1480 oobbuf = (u_char *) ffchars;
1481
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001482 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001483
1484 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001485
1486 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001487
1488 /* In partial page write we don't update bufferram */
1489 onenand_update_bufferram(mtd, to, !ret && !subpage);
1490 if (ONENAND_IS_2PLANE(this)) {
1491 ONENAND_SET_BUFFERRAM1(this);
1492 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1493 }
1494
Kyungmin Park79e90462007-09-10 17:13:49 +09001495 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001496 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001497 break;
1498 }
1499
Kyungmin Park79e90462007-09-10 17:13:49 +09001500 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001501 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001502 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001503 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001504 break;
1505 }
1506
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001507 written += thislen;
1508
Kyungmin Park79e90462007-09-10 17:13:49 +09001509 if (written == len)
1510 break;
1511
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001512 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001513 to += thislen;
1514 buf += thislen;
1515 }
1516
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001517 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001518
1519 return ret;
1520}
1521
1522/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001523 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001524 * @param mtd MTD device structure
1525 * @param to offset to write to
1526 * @param len number of bytes to write
1527 * @param retlen pointer to variable to store the number of written bytes
1528 * @param buf the data to write
1529 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001530 *
1531 * OneNAND write out-of-band
1532 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001533static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1534 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001535{
1536 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001537 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301538 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001539 u_char *oobbuf;
1540 size_t len = ops->ooblen;
1541 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001542 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001543
1544 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001545
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001546 pr_debug("onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
1547 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001548
1549 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001550 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001551
Sergey Lapin3a38a552013-01-14 03:46:50 +00001552 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001553 oobsize = this->ecclayout->oobavail;
1554 else
1555 oobsize = mtd->oobsize;
1556
1557 column = to & (mtd->oobsize - 1);
1558
1559 if (unlikely(column >= oobsize)) {
1560 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001561 return -EINVAL;
1562 }
1563
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001564 /* For compatibility with NAND: Do not allow write past end of page */
1565 if (unlikely(column + len > oobsize)) {
1566 printk(KERN_ERR "onenand_write_oob_nolock: "
1567 "Attempt to write past end of page\n");
1568 return -EINVAL;
1569 }
1570
1571 /* Do not allow reads past end of device */
1572 if (unlikely(to >= mtd->size ||
1573 column + len > ((mtd->size >> this->page_shift) -
1574 (to >> this->page_shift)) * oobsize)) {
1575 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1576 return -EINVAL;
1577 }
1578
1579 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001580
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001581 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1582 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301583
Kyungmin Park79e90462007-09-10 17:13:49 +09001584 /* Loop until all data write */
1585 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001586 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001587
1588 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1589
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001590 /* We send data to spare ram with oobsize
1591 * to prevent byte access */
1592 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001593 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001594 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1595 else
1596 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001597 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001598
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001599 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301600 /* Set main area of DataRAM to 0xff*/
1601 memset(this->page_buf, 0xff, mtd->writesize);
1602 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1603 this->page_buf, 0, mtd->writesize);
1604 }
1605
1606 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001607
1608 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001609 if (ONENAND_IS_2PLANE(this)) {
1610 ONENAND_SET_BUFFERRAM1(this);
1611 onenand_update_bufferram(mtd, to + this->writesize, 0);
1612 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001613
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001614 ret = this->wait(mtd, FL_WRITING);
1615 if (ret) {
1616 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001617 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001618 }
1619
1620 ret = onenand_verify_oob(mtd, oobbuf, to);
1621 if (ret) {
1622 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1623 break;
1624 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001625
1626 written += thislen;
1627 if (written == len)
1628 break;
1629
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001630 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001631 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001632 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001633 }
1634
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001635 ops->oobretlen = written;
1636
1637 return ret;
1638}
1639
1640/**
1641 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1642 * @param mtd MTD device structure
1643 * @param to offset to write to
1644 * @param len number of bytes to write
1645 * @param retlen pointer to variable to store the number of written bytes
1646 * @param buf the data to write
1647 *
1648 * Write with ECC
1649 */
1650int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1651 size_t * retlen, const u_char * buf)
1652{
1653 struct mtd_oob_ops ops = {
1654 .len = len,
1655 .ooblen = 0,
1656 .datbuf = (u_char *) buf,
1657 .oobbuf = NULL,
1658 };
1659 int ret;
1660
1661 onenand_get_device(mtd, FL_WRITING);
1662 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001663 onenand_release_device(mtd);
1664
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001665 *retlen = ops.retlen;
1666 return ret;
1667}
Kyungmin Park79e90462007-09-10 17:13:49 +09001668
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001669/**
1670 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1671 * @param mtd MTD device structure
1672 * @param to offset to write to
1673 * @param ops oob operation description structure
1674 *
1675 * OneNAND write main and/or out-of-band
1676 */
1677int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1678 struct mtd_oob_ops *ops)
1679{
1680 int ret;
1681
1682 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001683 case MTD_OPS_PLACE_OOB:
1684 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001685 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001686 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001687 /* Not implemented yet */
1688 default:
1689 return -EINVAL;
1690 }
1691
1692 onenand_get_device(mtd, FL_WRITING);
1693 if (ops->datbuf)
1694 ret = onenand_write_ops_nolock(mtd, to, ops);
1695 else
1696 ret = onenand_write_oob_nolock(mtd, to, ops);
1697 onenand_release_device(mtd);
1698
1699 return ret;
1700
Kyungmin Park79e90462007-09-10 17:13:49 +09001701}
1702
Kyungmin Park396b0c42008-08-13 09:11:02 +09001703/**
1704 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1705 * @param mtd MTD device structure
1706 * @param ofs offset from device start
1707 * @param allowbbt 1, if its allowed to access the bbt area
1708 *
1709 * Check, if the block is bad, Either by reading the bad block table or
1710 * calling of the scan function.
1711 */
1712static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1713{
1714 struct onenand_chip *this = mtd->priv;
1715 struct bbm_info *bbm = this->bbm;
1716
1717 /* Return info from the table */
1718 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1719}
1720
1721
Kyungmin Park79e90462007-09-10 17:13:49 +09001722/**
1723 * onenand_erase - [MTD Interface] erase block(s)
1724 * @param mtd MTD device structure
1725 * @param instr erase instruction
1726 *
1727 * Erase one ore more blocks
1728 */
1729int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1730{
1731 struct onenand_chip *this = mtd->priv;
1732 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301733 loff_t addr = instr->addr;
1734 unsigned int len = instr->len;
1735 int ret = 0, i;
1736 struct mtd_erase_region_info *region = NULL;
1737 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001738
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001739 pr_debug("onenand_erase: start = 0x%08x, len = %i\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301740 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001741
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301742 if (FLEXONENAND(this)) {
1743 /* Find the eraseregion of this address */
1744 i = flexonenand_region(mtd, addr);
1745 region = &mtd->eraseregions[i];
1746
1747 block_size = region->erasesize;
1748 region_end = region->offset
1749 + region->erasesize * region->numblocks;
1750
1751 /* Start address within region must align on block boundary.
1752 * Erase region's start offset is always block start address.
1753 */
1754 if (unlikely((addr - region->offset) & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001755 pr_debug("onenand_erase:" " Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301756 return -EINVAL;
1757 }
1758 } else {
1759 block_size = 1 << this->erase_shift;
1760
1761 /* Start address must align on block boundary */
1762 if (unlikely(addr & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001763 pr_debug("onenand_erase:" "Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301764 return -EINVAL;
1765 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001766 }
1767
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301768 /* Length must align on block boundary */
1769 if (unlikely(len & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001770 pr_debug("onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001771 return -EINVAL;
1772 }
1773
Kyungmin Park79e90462007-09-10 17:13:49 +09001774 /* Grab the lock and see if the device is available */
1775 onenand_get_device(mtd, FL_ERASING);
1776
1777 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001778 instr->state = MTD_ERASING;
1779
1780 while (len) {
1781
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001782 /* Check if we have a bad block, we do not erase bad blocks */
1783 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1784 printk(KERN_WARNING "onenand_erase: attempt to erase"
1785 " a bad block at addr 0x%08x\n",
1786 (unsigned int) addr);
1787 instr->state = MTD_ERASE_FAILED;
1788 goto erase_exit;
1789 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001790
1791 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1792
Kyungmin Park396b0c42008-08-13 09:11:02 +09001793 onenand_invalidate_bufferram(mtd, addr, block_size);
1794
Kyungmin Park79e90462007-09-10 17:13:49 +09001795 ret = this->wait(mtd, FL_ERASING);
1796 /* Check, if it is write protected */
1797 if (ret) {
1798 if (ret == -EPERM)
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001799 pr_debug("onenand_erase: "
1800 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001801 else
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001802 pr_debug("onenand_erase: "
1803 "Failed erase, block %d\n",
1804 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001805 instr->state = MTD_ERASE_FAILED;
1806 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001807
Kyungmin Park79e90462007-09-10 17:13:49 +09001808 goto erase_exit;
1809 }
1810
1811 len -= block_size;
1812 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301813
1814 if (addr == region_end) {
1815 if (!len)
1816 break;
1817 region++;
1818
1819 block_size = region->erasesize;
1820 region_end = region->offset
1821 + region->erasesize * region->numblocks;
1822
1823 if (len & (block_size - 1)) {
1824 /* This has been checked at MTD
1825 * partitioning level. */
1826 printk("onenand_erase: Unaligned address\n");
1827 goto erase_exit;
1828 }
1829 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001830 }
1831
1832 instr->state = MTD_ERASE_DONE;
1833
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001834erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001835
1836 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1837 /* Do call back function */
1838 if (!ret)
1839 mtd_erase_callback(instr);
1840
1841 /* Deselect and wake up anyone waiting on the device */
1842 onenand_release_device(mtd);
1843
1844 return ret;
1845}
1846
1847/**
1848 * onenand_sync - [MTD Interface] sync
1849 * @param mtd MTD device structure
1850 *
1851 * Sync is actually a wait for chip ready function
1852 */
1853void onenand_sync(struct mtd_info *mtd)
1854{
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001855 pr_debug("onenand_sync: called\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001856
1857 /* Grab the lock and see if the device is available */
1858 onenand_get_device(mtd, FL_SYNCING);
1859
1860 /* Release it and go back */
1861 onenand_release_device(mtd);
1862}
1863
1864/**
1865 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1866 * @param mtd MTD device structure
1867 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001868 *
1869 * Check whether the block is bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001870 */
1871int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1872{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001873 int ret;
1874
1875 /* Check for invalid offset */
1876 if (ofs > mtd->size)
1877 return -EINVAL;
1878
1879 onenand_get_device(mtd, FL_READING);
1880 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1881 onenand_release_device(mtd);
1882 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001883}
1884
1885/**
Kyungmin Park87b49502008-11-13 15:14:33 +09001886 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1887 * @param mtd MTD device structure
1888 * @param ofs offset from device start
1889 *
1890 * This is the default implementation, which can be overridden by
1891 * a hardware specific driver.
1892 */
1893static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1894{
1895 struct onenand_chip *this = mtd->priv;
1896 struct bbm_info *bbm = this->bbm;
1897 u_char buf[2] = {0, 0};
1898 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001899 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park87b49502008-11-13 15:14:33 +09001900 .ooblen = 2,
1901 .oobbuf = buf,
1902 .ooboffs = 0,
1903 };
1904 int block;
1905
1906 /* Get block number */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301907 block = onenand_block(this, ofs);
Kyungmin Park87b49502008-11-13 15:14:33 +09001908 if (bbm->bbt)
1909 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1910
1911 /* We write two bytes, so we dont have to mess with 16 bit access */
1912 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1913 return onenand_write_oob_nolock(mtd, ofs, &ops);
1914}
1915
1916/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001917 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1918 * @param mtd MTD device structure
1919 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001920 *
1921 * Mark the block as bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001922 */
1923int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1924{
Ladislav Michl7868fba2017-06-20 17:17:45 +02001925 struct onenand_chip *this = mtd->priv;
Kyungmin Park396b0c42008-08-13 09:11:02 +09001926 int ret;
1927
1928 ret = onenand_block_isbad(mtd, ofs);
1929 if (ret) {
1930 /* If it was bad already, return success and do nothing */
1931 if (ret > 0)
1932 return 0;
1933 return ret;
1934 }
1935
Ladislav Michl7868fba2017-06-20 17:17:45 +02001936 onenand_get_device(mtd, FL_WRITING);
1937 ret = this->block_markbad(mtd, ofs);
1938 onenand_release_device(mtd);
1939
Kyungmin Park396b0c42008-08-13 09:11:02 +09001940 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001941}
1942
1943/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001944 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1945 * @param mtd MTD device structure
1946 * @param ofs offset relative to mtd start
1947 * @param len number of bytes to lock or unlock
1948 * @param cmd lock or unlock command
Kyungmin Park79e90462007-09-10 17:13:49 +09001949 *
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001950 * Lock or unlock one or more blocks
Kyungmin Park79e90462007-09-10 17:13:49 +09001951 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001952static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park79e90462007-09-10 17:13:49 +09001953{
1954 struct onenand_chip *this = mtd->priv;
1955 int start, end, block, value, status;
1956
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301957 start = onenand_block(this, ofs);
1958 end = onenand_block(this, ofs + len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001959
1960 /* Continuous lock scheme */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001961 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park79e90462007-09-10 17:13:49 +09001962 /* Set start block address */
1963 this->write_word(start,
1964 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1965 /* Set end block address */
1966 this->write_word(end - 1,
1967 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1968 /* Write unlock command */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001969 this->command(mtd, cmd, 0, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001970
1971 /* There's no return value */
1972 this->wait(mtd, FL_UNLOCKING);
1973
1974 /* Sanity check */
1975 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1976 & ONENAND_CTRL_ONGO)
1977 continue;
1978
1979 /* Check lock status */
1980 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1981 if (!(status & ONENAND_WP_US))
1982 printk(KERN_ERR "wp status = 0x%x\n", status);
1983
1984 return 0;
1985 }
1986
1987 /* Block lock scheme */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301988 for (block = start; block < end; block++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001989 /* Set block address */
1990 value = onenand_block_address(this, block);
1991 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1992 /* Select DataRAM for DDP */
1993 value = onenand_bufferram_address(this, block);
1994 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1995
Kyungmin Park79e90462007-09-10 17:13:49 +09001996 /* Set start block address */
1997 this->write_word(block,
1998 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1999 /* Write unlock command */
2000 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
2001
2002 /* There's no return value */
2003 this->wait(mtd, FL_UNLOCKING);
2004
2005 /* Sanity check */
2006 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2007 & ONENAND_CTRL_ONGO)
2008 continue;
2009
Kyungmin Park79e90462007-09-10 17:13:49 +09002010 /* Check lock status */
2011 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2012 if (!(status & ONENAND_WP_US))
2013 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2014 block, status);
2015 }
2016
2017 return 0;
2018}
2019
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002020#ifdef ONENAND_LINUX
Kyungmin Park79e90462007-09-10 17:13:49 +09002021/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002022 * onenand_lock - [MTD Interface] Lock block(s)
2023 * @param mtd MTD device structure
2024 * @param ofs offset relative to mtd start
2025 * @param len number of bytes to unlock
2026 *
2027 * Lock one or more blocks
2028 */
2029static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2030{
2031 int ret;
2032
2033 onenand_get_device(mtd, FL_LOCKING);
2034 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2035 onenand_release_device(mtd);
2036 return ret;
2037}
2038
2039/**
2040 * onenand_unlock - [MTD Interface] Unlock block(s)
2041 * @param mtd MTD device structure
2042 * @param ofs offset relative to mtd start
2043 * @param len number of bytes to unlock
2044 *
2045 * Unlock one or more blocks
2046 */
2047static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2048{
2049 int ret;
2050
2051 onenand_get_device(mtd, FL_LOCKING);
2052 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2053 onenand_release_device(mtd);
2054 return ret;
2055}
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002056#endif
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002057
2058/**
2059 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2060 * @param this onenand chip data structure
2061 *
2062 * Check lock status
2063 */
2064static int onenand_check_lock_status(struct onenand_chip *this)
2065{
2066 unsigned int value, block, status;
2067 unsigned int end;
2068
2069 end = this->chipsize >> this->erase_shift;
2070 for (block = 0; block < end; block++) {
2071 /* Set block address */
2072 value = onenand_block_address(this, block);
2073 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2074 /* Select DataRAM for DDP */
2075 value = onenand_bufferram_address(this, block);
2076 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2077 /* Set start block address */
2078 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2079
2080 /* Check lock status */
2081 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2082 if (!(status & ONENAND_WP_US)) {
2083 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2084 return 0;
2085 }
2086 }
2087
2088 return 1;
2089}
2090
2091/**
2092 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2093 * @param mtd MTD device structure
2094 *
2095 * Unlock all blocks
2096 */
2097static void onenand_unlock_all(struct mtd_info *mtd)
2098{
2099 struct onenand_chip *this = mtd->priv;
2100 loff_t ofs = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302101 size_t len = mtd->size;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002102
2103 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2104 /* Set start block address */
2105 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2106 /* Write unlock command */
2107 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2108
2109 /* There's no return value */
2110 this->wait(mtd, FL_LOCKING);
2111
2112 /* Sanity check */
2113 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2114 & ONENAND_CTRL_ONGO)
2115 continue;
2116
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002117 /* Check lock status */
2118 if (onenand_check_lock_status(this))
2119 return;
2120
2121 /* Workaround for all block unlock in DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302122 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002123 /* All blocks on another chip */
2124 ofs = this->chipsize >> 1;
2125 len = this->chipsize >> 1;
2126 }
2127 }
2128
2129 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2130}
2131
2132
2133/**
2134 * onenand_check_features - Check and set OneNAND features
2135 * @param mtd MTD data structure
2136 *
2137 * Check and set OneNAND features
2138 * - lock scheme
2139 * - two plane
2140 */
2141static void onenand_check_features(struct mtd_info *mtd)
2142{
2143 struct onenand_chip *this = mtd->priv;
2144 unsigned int density, process;
2145
2146 /* Lock scheme depends on density and process */
2147 density = onenand_get_density(this->device_id);
2148 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2149
2150 /* Lock scheme */
2151 switch (density) {
2152 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002153 if (ONENAND_IS_DDP(this))
2154 this->options |= ONENAND_HAS_2PLANE;
2155 else
2156 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002157
2158 case ONENAND_DEVICE_DENSITY_2Gb:
2159 /* 2Gb DDP don't have 2 plane */
2160 if (!ONENAND_IS_DDP(this))
2161 this->options |= ONENAND_HAS_2PLANE;
2162 this->options |= ONENAND_HAS_UNLOCK_ALL;
2163
2164 case ONENAND_DEVICE_DENSITY_1Gb:
2165 /* A-Die has all block unlock */
2166 if (process)
2167 this->options |= ONENAND_HAS_UNLOCK_ALL;
2168 break;
2169
2170 default:
2171 /* Some OneNAND has continuous lock scheme */
2172 if (!process)
2173 this->options |= ONENAND_HAS_CONT_LOCK;
2174 break;
2175 }
2176
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302177 if (ONENAND_IS_MLC(this))
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002178 this->options |= ONENAND_HAS_4KB_PAGE;
2179
2180 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302181 this->options &= ~ONENAND_HAS_2PLANE;
2182
2183 if (FLEXONENAND(this)) {
2184 this->options &= ~ONENAND_HAS_CONT_LOCK;
2185 this->options |= ONENAND_HAS_UNLOCK_ALL;
2186 }
2187
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002188 if (this->options & ONENAND_HAS_CONT_LOCK)
2189 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2190 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2191 printk(KERN_DEBUG "Chip support all block unlock\n");
2192 if (this->options & ONENAND_HAS_2PLANE)
2193 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002194 if (this->options & ONENAND_HAS_4KB_PAGE)
2195 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2196
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002197}
2198
2199/**
Kyungmin Park79e90462007-09-10 17:13:49 +09002200 * onenand_print_device_info - Print device ID
2201 * @param device device ID
2202 *
2203 * Print device ID
2204 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002205char *onenand_print_device_info(int device, int version)
Kyungmin Park79e90462007-09-10 17:13:49 +09002206{
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302207 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002208 char *dev_info = malloc(80);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002209 char *p = dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002210
2211 vcc = device & ONENAND_DEVICE_VCC_MASK;
2212 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2213 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302214 density = onenand_get_density(device);
2215 flexonenand = device & DEVICE_IS_FLEXONENAND;
2216 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park79e90462007-09-10 17:13:49 +09002217 demuxed ? "" : "Muxed ",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302218 flexonenand ? "Flex-" : "",
Kyungmin Park79e90462007-09-10 17:13:49 +09002219 ddp ? "(DDP)" : "",
2220 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002221
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002222 sprintf(p, "\nOneNAND version = 0x%04x", version);
2223 printk("%s\n", dev_info);
2224
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002225 return dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002226}
2227
2228static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra891d81b2010-10-11 21:48:03 +02002229 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002230 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002231};
2232
2233/**
2234 * onenand_check_maf - Check manufacturer ID
2235 * @param manuf manufacturer ID
2236 *
2237 * Check manufacturer ID
2238 */
2239static int onenand_check_maf(int manuf)
2240{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002241 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park79e90462007-09-10 17:13:49 +09002242 int i;
Marek Vasute3334742011-11-06 00:59:52 +01002243#ifdef ONENAND_DEBUG
2244 char *name;
2245#endif
Kyungmin Park79e90462007-09-10 17:13:49 +09002246
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302247 for (i = 0; i < size; i++)
Kyungmin Park79e90462007-09-10 17:13:49 +09002248 if (manuf == onenand_manuf_ids[i].id)
2249 break;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002250
Marek Vasute3334742011-11-06 00:59:52 +01002251#ifdef ONENAND_DEBUG
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002252 if (i < size)
2253 name = onenand_manuf_ids[i].name;
2254 else
2255 name = "Unknown";
Kyungmin Park79e90462007-09-10 17:13:49 +09002256
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002257 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park79e90462007-09-10 17:13:49 +09002258#endif
2259
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002260 return i == size;
Kyungmin Park79e90462007-09-10 17:13:49 +09002261}
2262
2263/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302264* flexonenand_get_boundary - Reads the SLC boundary
2265* @param onenand_info - onenand info structure
2266*
2267* Fill up boundary[] field in onenand_chip
2268**/
2269static int flexonenand_get_boundary(struct mtd_info *mtd)
2270{
2271 struct onenand_chip *this = mtd->priv;
2272 unsigned int die, bdry;
Marek Vasute3334742011-11-06 00:59:52 +01002273 int syscfg, locked;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302274
2275 /* Disable ECC */
2276 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2277 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2278
2279 for (die = 0; die < this->dies; die++) {
2280 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2281 this->wait(mtd, FL_SYNCING);
2282
2283 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002284 this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302285
2286 bdry = this->read_word(this->base + ONENAND_DATARAM);
2287 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2288 locked = 0;
2289 else
2290 locked = 1;
2291 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2292
2293 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002294 this->wait(mtd, FL_RESETING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302295
2296 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2297 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2298 }
2299
2300 /* Enable ECC */
2301 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2302 return 0;
2303}
2304
2305/**
2306 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2307 * boundary[], diesize[], mtd->size, mtd->erasesize,
2308 * mtd->eraseregions
2309 * @param mtd - MTD device structure
2310 */
2311static void flexonenand_get_size(struct mtd_info *mtd)
2312{
2313 struct onenand_chip *this = mtd->priv;
2314 int die, i, eraseshift, density;
2315 int blksperdie, maxbdry;
2316 loff_t ofs;
2317
2318 density = onenand_get_density(this->device_id);
2319 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2320 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2321 maxbdry = blksperdie - 1;
2322 eraseshift = this->erase_shift - 1;
2323
2324 mtd->numeraseregions = this->dies << 1;
2325
2326 /* This fills up the device boundary */
2327 flexonenand_get_boundary(mtd);
2328 die = 0;
2329 ofs = 0;
2330 i = -1;
2331 for (; die < this->dies; die++) {
2332 if (!die || this->boundary[die-1] != maxbdry) {
2333 i++;
2334 mtd->eraseregions[i].offset = ofs;
2335 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2336 mtd->eraseregions[i].numblocks =
2337 this->boundary[die] + 1;
2338 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2339 eraseshift++;
2340 } else {
2341 mtd->numeraseregions -= 1;
2342 mtd->eraseregions[i].numblocks +=
2343 this->boundary[die] + 1;
2344 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2345 }
2346 if (this->boundary[die] != maxbdry) {
2347 i++;
2348 mtd->eraseregions[i].offset = ofs;
2349 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2350 mtd->eraseregions[i].numblocks = maxbdry ^
2351 this->boundary[die];
2352 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2353 eraseshift--;
2354 } else
2355 mtd->numeraseregions -= 1;
2356 }
2357
2358 /* Expose MLC erase size except when all blocks are SLC */
2359 mtd->erasesize = 1 << this->erase_shift;
2360 if (mtd->numeraseregions == 1)
2361 mtd->erasesize >>= 1;
2362
2363 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2364 for (i = 0; i < mtd->numeraseregions; i++)
2365 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2366 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2367 mtd->eraseregions[i].erasesize,
2368 mtd->eraseregions[i].numblocks);
2369
2370 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2371 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2372 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2373 << (this->erase_shift - 1);
2374 mtd->size += this->diesize[die];
2375 }
2376}
2377
2378/**
2379 * flexonenand_check_blocks_erased - Check if blocks are erased
2380 * @param mtd_info - mtd info structure
2381 * @param start - first erase block to check
2382 * @param end - last erase block to check
2383 *
2384 * Converting an unerased block from MLC to SLC
2385 * causes byte values to change. Since both data and its ECC
2386 * have changed, reads on the block give uncorrectable error.
2387 * This might lead to the block being detected as bad.
2388 *
2389 * Avoid this by ensuring that the block to be converted is
2390 * erased.
2391 */
2392static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2393 int start, int end)
2394{
2395 struct onenand_chip *this = mtd->priv;
2396 int i, ret;
2397 int block;
2398 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002399 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302400 .ooboffs = 0,
2401 .ooblen = mtd->oobsize,
2402 .datbuf = NULL,
2403 .oobbuf = this->oob_buf,
2404 };
2405 loff_t addr;
2406
2407 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2408
2409 for (block = start; block <= end; block++) {
2410 addr = flexonenand_addr(this, block);
2411 if (onenand_block_isbad_nolock(mtd, addr, 0))
2412 continue;
2413
2414 /*
2415 * Since main area write results in ECC write to spare,
2416 * it is sufficient to check only ECC bytes for change.
2417 */
2418 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2419 if (ret)
2420 return ret;
2421
2422 for (i = 0; i < mtd->oobsize; i++)
2423 if (this->oob_buf[i] != 0xff)
2424 break;
2425
2426 if (i != mtd->oobsize) {
2427 printk(KERN_WARNING "Block %d not erased.\n", block);
2428 return 1;
2429 }
2430 }
2431
2432 return 0;
2433}
2434
2435/**
2436 * flexonenand_set_boundary - Writes the SLC boundary
2437 * @param mtd - mtd info structure
2438 */
2439int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2440 int boundary, int lock)
2441{
2442 struct onenand_chip *this = mtd->priv;
2443 int ret, density, blksperdie, old, new, thisboundary;
2444 loff_t addr;
2445
2446 if (die >= this->dies)
2447 return -EINVAL;
2448
2449 if (boundary == this->boundary[die])
2450 return 0;
2451
2452 density = onenand_get_density(this->device_id);
2453 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2454 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2455
2456 if (boundary >= blksperdie) {
2457 printk("flexonenand_set_boundary:"
2458 "Invalid boundary value. "
2459 "Boundary not changed.\n");
2460 return -EINVAL;
2461 }
2462
2463 /* Check if converting blocks are erased */
2464 old = this->boundary[die] + (die * this->density_mask);
2465 new = boundary + (die * this->density_mask);
2466 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2467 + 1, max(old, new));
2468 if (ret) {
2469 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2470 return ret;
2471 }
2472
2473 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2474 this->wait(mtd, FL_SYNCING);
2475
2476 /* Check is boundary is locked */
2477 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2478 ret = this->wait(mtd, FL_READING);
2479
2480 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2481 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2482 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2483 goto out;
2484 }
2485
2486 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2487 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2488
2489 boundary &= FLEXONENAND_PI_MASK;
2490 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2491
2492 addr = die ? this->diesize[0] : 0;
2493 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2494 ret = this->wait(mtd, FL_ERASING);
2495 if (ret) {
2496 printk("flexonenand_set_boundary:"
2497 "Failed PI erase for Die %d\n", die);
2498 goto out;
2499 }
2500
2501 this->write_word(boundary, this->base + ONENAND_DATARAM);
2502 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2503 ret = this->wait(mtd, FL_WRITING);
2504 if (ret) {
2505 printk("flexonenand_set_boundary:"
2506 "Failed PI write for Die %d\n", die);
2507 goto out;
2508 }
2509
2510 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2511 ret = this->wait(mtd, FL_WRITING);
2512out:
2513 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2514 this->wait(mtd, FL_RESETING);
2515 if (!ret)
2516 /* Recalculate device size on boundary change*/
2517 flexonenand_get_size(mtd);
2518
2519 return ret;
2520}
2521
2522/**
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002523 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park79e90462007-09-10 17:13:49 +09002524 * @param mtd MTD device structure
2525 *
2526 * OneNAND detection method:
2527 * Compare the the values from command with ones from register
2528 */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002529static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park79e90462007-09-10 17:13:49 +09002530{
2531 struct onenand_chip *this = mtd->priv;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002532 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002533 int syscfg;
2534
2535 /* Save system configuration 1 */
2536 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002537
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002538 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002539 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2540 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park79e90462007-09-10 17:13:49 +09002541
2542 /* Send the command for reading device ID from BootRAM */
2543 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2544
2545 /* Read manufacturer and device IDs from BootRAM */
2546 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2547 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2548
Kyungmin Park79e90462007-09-10 17:13:49 +09002549 /* Reset OneNAND to read default register values */
2550 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2551
Kyungmin Park396b0c42008-08-13 09:11:02 +09002552 /* Wait reset */
Ladislav Michl243af2f2016-07-12 20:28:19 +02002553 if (this->wait(mtd, FL_RESETING))
2554 return -ENXIO;
Kyungmin Park79e90462007-09-10 17:13:49 +09002555
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002556 /* Restore system configuration 1 */
2557 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2558
2559 /* Check manufacturer ID */
2560 if (onenand_check_maf(bram_maf_id))
2561 return -ENXIO;
2562
Kyungmin Park79e90462007-09-10 17:13:49 +09002563 /* Read manufacturer and device IDs from Register */
2564 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2565 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2566
2567 /* Check OneNAND device */
2568 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2569 return -ENXIO;
Kyungmin Park05d23182008-03-17 08:54:06 +09002570
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002571 return 0;
2572}
2573
2574/**
2575 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2576 * @param mtd MTD device structure
2577 *
2578 * OneNAND detection method:
2579 * Compare the the values from command with ones from register
2580 */
2581int onenand_probe(struct mtd_info *mtd)
2582{
2583 struct onenand_chip *this = mtd->priv;
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002584 int dev_id, ver_id;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002585 int density;
2586 int ret;
2587
2588 ret = this->chip_probe(mtd);
2589 if (ret)
2590 return ret;
2591
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002592 /* Read device IDs from Register */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002593 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2594 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2595 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2596
Kyungmin Park79e90462007-09-10 17:13:49 +09002597 /* Flash device information */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002598 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park79e90462007-09-10 17:13:49 +09002599 this->device_id = dev_id;
Stefan Roese7c9aafe2008-11-11 10:29:09 +01002600 this->version_id = ver_id;
Kyungmin Park79e90462007-09-10 17:13:49 +09002601
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002602 /* Check OneNAND features */
2603 onenand_check_features(mtd);
2604
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002605 density = onenand_get_density(dev_id);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302606 if (FLEXONENAND(this)) {
2607 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2608 /* Maximum possible erase regions */
2609 mtd->numeraseregions = this->dies << 1;
2610 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2611 * (this->dies << 1));
2612 if (!mtd->eraseregions)
2613 return -ENOMEM;
2614 }
2615
2616 /*
2617 * For Flex-OneNAND, chipsize represents maximum possible device size.
2618 * mtd->size represents the actual device size.
2619 */
Kyungmin Park79e90462007-09-10 17:13:49 +09002620 this->chipsize = (16 << density) << 20;
2621
2622 /* OneNAND page size & block size */
2623 /* The data buffer size is equal to page size */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002624 mtd->writesize =
Kyungmin Park79e90462007-09-10 17:13:49 +09002625 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302626 /* We use the full BufferRAM */
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002627 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302628 mtd->writesize <<= 1;
2629
Kyungmin Park396b0c42008-08-13 09:11:02 +09002630 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park79e90462007-09-10 17:13:49 +09002631 /* Pagers per block is always 64 in OneNAND */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002632 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302633 /*
2634 * Flex-OneNAND SLC area has 64 pages per block.
2635 * Flex-OneNAND MLC area has 128 pages per block.
2636 * Expose MLC erase size to find erase_shift and page_mask.
2637 */
2638 if (FLEXONENAND(this))
2639 mtd->erasesize <<= 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002640
2641 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002642 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002643 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Park396b0c42008-08-13 09:11:02 +09002644 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302645 /* Set density mask. it is used for DDP */
2646 if (ONENAND_IS_DDP(this))
2647 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002648 /* It's real page size */
2649 this->writesize = mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002650
2651 /* REVIST: Multichip handling */
2652
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302653 if (FLEXONENAND(this))
2654 flexonenand_get_size(mtd);
2655 else
2656 mtd->size = this->chipsize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002657
Kyungmin Park396b0c42008-08-13 09:11:02 +09002658 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002659 mtd->_erase = onenand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002660 mtd->_read_oob = onenand_read_oob;
2661 mtd->_write_oob = onenand_write_oob;
2662 mtd->_sync = onenand_sync;
2663 mtd->_block_isbad = onenand_block_isbad;
2664 mtd->_block_markbad = onenand_block_markbad;
Ladislav Michl7dbad5c2016-07-12 20:28:21 +02002665 mtd->writebufsize = mtd->writesize;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002666
Kyungmin Park79e90462007-09-10 17:13:49 +09002667 return 0;
2668}
2669
2670/**
2671 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2672 * @param mtd MTD device structure
2673 * @param maxchips Number of chips to scan for
2674 *
2675 * This fills out all the not initialized function pointers
2676 * with the defaults.
2677 * The flash ID is read and the mtd/chip structures are
2678 * filled with the appropriate values.
2679 */
2680int onenand_scan(struct mtd_info *mtd, int maxchips)
2681{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002682 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002683 struct onenand_chip *this = mtd->priv;
2684
2685 if (!this->read_word)
2686 this->read_word = onenand_readw;
2687 if (!this->write_word)
2688 this->write_word = onenand_writew;
2689
2690 if (!this->command)
2691 this->command = onenand_command;
2692 if (!this->wait)
2693 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002694 if (!this->bbt_wait)
2695 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002696
2697 if (!this->read_bufferram)
2698 this->read_bufferram = onenand_read_bufferram;
2699 if (!this->write_bufferram)
2700 this->write_bufferram = onenand_write_bufferram;
2701
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002702 if (!this->chip_probe)
2703 this->chip_probe = onenand_chip_probe;
2704
Kyungmin Park87b49502008-11-13 15:14:33 +09002705 if (!this->block_markbad)
2706 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002707 if (!this->scan_bbt)
2708 this->scan_bbt = onenand_default_bbt;
2709
Kyungmin Park79e90462007-09-10 17:13:49 +09002710 if (onenand_probe(mtd))
2711 return -ENXIO;
2712
2713 /* Set Sync. Burst Read after probing */
2714 if (this->mmcontrol) {
2715 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2716 this->read_bufferram = onenand_sync_read_bufferram;
2717 }
2718
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002719 /* Allocate buffers, if necessary */
2720 if (!this->page_buf) {
2721 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2722 if (!this->page_buf) {
2723 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2724 return -ENOMEM;
2725 }
2726 this->options |= ONENAND_PAGEBUF_ALLOC;
2727 }
2728 if (!this->oob_buf) {
2729 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2730 if (!this->oob_buf) {
2731 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2732 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2733 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2734 kfree(this->page_buf);
2735 }
2736 return -ENOMEM;
2737 }
2738 this->options |= ONENAND_OOBBUF_ALLOC;
2739 }
2740
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002741 this->state = FL_READY;
2742
2743 /*
2744 * Allow subpage writes up to oobsize.
2745 */
2746 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302747 case 128:
2748 this->ecclayout = &onenand_oob_128;
2749 mtd->subpage_sft = 0;
2750 break;
2751
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002752 case 64:
2753 this->ecclayout = &onenand_oob_64;
2754 mtd->subpage_sft = 2;
2755 break;
2756
2757 case 32:
2758 this->ecclayout = &onenand_oob_32;
2759 mtd->subpage_sft = 1;
2760 break;
2761
2762 default:
2763 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2764 mtd->oobsize);
2765 mtd->subpage_sft = 0;
2766 /* To prevent kernel oops */
2767 this->ecclayout = &onenand_oob_32;
2768 break;
2769 }
2770
2771 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2772
2773 /*
2774 * The number of bytes available for a client to place data into
2775 * the out of band area
2776 */
2777 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302778
2779 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002780 this->ecclayout->oobfree[i].length; i++)
2781 this->ecclayout->oobavail +=
2782 this->ecclayout->oobfree[i].length;
2783 mtd->oobavail = this->ecclayout->oobavail;
2784
2785 mtd->ecclayout = this->ecclayout;
2786
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002787 /* Unlock whole block */
2788 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002789
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002790 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002791}
2792
2793/**
2794 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2795 * @param mtd MTD device structure
2796 */
2797void onenand_release(struct mtd_info *mtd)
2798{
2799}