blob: 762b01c1b0f0953ea774974ef66a103f42673382 [file] [log] [blame]
Kyungmin Park79e90462007-09-10 17:13:49 +09001/*
2 * linux/drivers/mtd/onenand/onenand_base.c
3 *
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09007 * Credits:
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
11 *
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053012 * Rohit Hagargundgi <h.rohit at samsung.com>,
13 * Amul Kumar Saha <amul.saha@samsung.com>:
14 * Flex-OneNAND support
15 * Copyright (C) Samsung Electronics, 2009
16 *
Kyungmin Park79e90462007-09-10 17:13:49 +090017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Ladislav Michl243af2f2016-07-12 20:28:19 +020024#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000027#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090028#include <linux/mtd/mtd.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060029#include <linux/printk.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#include "linux/mtd/flashchip.h"
Kyungmin Park79e90462007-09-10 17:13:49 +090031#include <linux/mtd/onenand.h>
32
33#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090034#include <linux/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020035#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090036
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090037/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053038static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090039{
40 void *ret = dst;
41 short *d = dst;
42 const short *s = src;
43
44 len >>= 1;
45 while (len-- > 0)
46 *d++ = *s++;
47 return ret;
48}
49
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010050/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053051 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
52 * For now, we expose only 64 out of 80 ecc bytes
53 */
54static struct nand_ecclayout onenand_oob_128 = {
55 .eccbytes = 64,
56 .eccpos = {
57 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
58 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
59 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
60 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
61 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
62 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
63 102, 103, 104, 105
64 },
65 .oobfree = {
66 {2, 4}, {18, 4}, {34, 4}, {50, 4},
67 {66, 4}, {82, 4}, {98, 4}, {114, 4}
68 }
69};
70
71/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010072 * onenand_oob_64 - oob info for large (2KB) page
73 */
74static struct nand_ecclayout onenand_oob_64 = {
75 .eccbytes = 20,
76 .eccpos = {
77 8, 9, 10, 11, 12,
78 24, 25, 26, 27, 28,
79 40, 41, 42, 43, 44,
80 56, 57, 58, 59, 60,
81 },
82 .oobfree = {
83 {2, 3}, {14, 2}, {18, 3}, {30, 2},
84 {34, 3}, {46, 2}, {50, 3}, {62, 2}
85 }
86};
87
88/**
89 * onenand_oob_32 - oob info for middle (1KB) page
90 */
91static struct nand_ecclayout onenand_oob_32 = {
92 .eccbytes = 10,
93 .eccpos = {
94 8, 9, 10, 11, 12,
95 24, 25, 26, 27, 28,
96 },
97 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
98};
99
Marek Vasut41120122013-12-26 01:01:24 +0100100/*
101 * Warning! This array is used with the memcpy_16() function, thus
102 * it must be aligned to 2 bytes. GCC can make this array unaligned
103 * as the array is made of unsigned char, which memcpy16() doesn't
104 * like and will cause unaligned access.
105 */
106static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park79e90462007-09-10 17:13:49 +0900107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900123};
124
125/**
126 * onenand_readw - [OneNAND Interface] Read OneNAND register
127 * @param addr address to read
128 *
129 * Read OneNAND register
130 */
131static unsigned short onenand_readw(void __iomem * addr)
132{
133 return readw(addr);
134}
135
136/**
137 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
138 * @param value value to write
139 * @param addr address to write
140 *
141 * Write OneNAND register with value
142 */
143static void onenand_writew(unsigned short value, void __iomem * addr)
144{
145 writew(value, addr);
146}
147
148/**
149 * onenand_block_address - [DEFAULT] Get block address
150 * @param device the device id
151 * @param block the block
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100152 * Return: translated block address if DDP, otherwise same
Kyungmin Park79e90462007-09-10 17:13:49 +0900153 *
154 * Setup Start Address 1 Register (F100h)
155 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900156static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900157{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900158 /* Device Flash Core select, NAND Flash Block Address */
159 if (block & this->density_mask)
160 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900161
162 return block;
163}
164
165/**
166 * onenand_bufferram_address - [DEFAULT] Get bufferram address
167 * @param device the device id
168 * @param block the block
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100169 * Return: set DBS value if DDP, otherwise 0
Kyungmin Park79e90462007-09-10 17:13:49 +0900170 *
171 * Setup Start Address 2 Register (F101h) for DDP
172 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900173static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900174{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900175 /* Device BufferRAM Select */
176 if (block & this->density_mask)
177 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900178
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900179 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900180}
181
182/**
183 * onenand_page_address - [DEFAULT] Get page address
184 * @param page the page address
185 * @param sector the sector address
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100186 * Return: combined page and sector address
Kyungmin Park79e90462007-09-10 17:13:49 +0900187 *
188 * Setup Start Address 8 Register (F107h)
189 */
190static int onenand_page_address(int page, int sector)
191{
192 /* Flash Page Address, Flash Sector Address */
193 int fpa, fsa;
194
195 fpa = page & ONENAND_FPA_MASK;
196 fsa = sector & ONENAND_FSA_MASK;
197
198 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
199}
200
201/**
202 * onenand_buffer_address - [DEFAULT] Get buffer address
203 * @param dataram1 DataRAM index
204 * @param sectors the sector address
205 * @param count the number of sectors
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100206 * Return: the start buffer value
Kyungmin Park79e90462007-09-10 17:13:49 +0900207 *
208 * Setup Start Buffer Register (F200h)
209 */
210static int onenand_buffer_address(int dataram1, int sectors, int count)
211{
212 int bsa, bsc;
213
214 /* BufferRAM Sector Address */
215 bsa = sectors & ONENAND_BSA_MASK;
216
217 if (dataram1)
218 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
219 else
220 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
221
222 /* BufferRAM Sector Count */
223 bsc = count & ONENAND_BSC_MASK;
224
225 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
226}
227
228/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530229 * flexonenand_block - Return block number for flash address
230 * @param this - OneNAND device structure
231 * @param addr - Address for which block number is needed
232 */
233static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
234{
235 unsigned int boundary, blk, die = 0;
236
237 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
238 die = 1;
239 addr -= this->diesize[0];
240 }
241
242 boundary = this->boundary[die];
243
244 blk = addr >> (this->erase_shift - 1);
245 if (blk > boundary)
246 blk = (blk + boundary + 1) >> 1;
247
248 blk += die ? this->density_mask : 0;
249 return blk;
250}
251
252unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
253{
254 if (!FLEXONENAND(this))
255 return addr >> this->erase_shift;
256 return flexonenand_block(this, addr);
257}
258
259/**
260 * flexonenand_addr - Return address of the block
261 * @this: OneNAND device structure
262 * @block: Block number on Flex-OneNAND
263 *
264 * Return address of the block
265 */
266static loff_t flexonenand_addr(struct onenand_chip *this, int block)
267{
268 loff_t ofs = 0;
269 int die = 0, boundary;
270
271 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
272 block -= this->density_mask;
273 die = 1;
274 ofs = this->diesize[0];
275 }
276
277 boundary = this->boundary[die];
278 ofs += (loff_t) block << (this->erase_shift - 1);
279 if (block > (boundary + 1))
280 ofs += (loff_t) (block - boundary - 1)
281 << (this->erase_shift - 1);
282 return ofs;
283}
284
285loff_t onenand_addr(struct onenand_chip *this, int block)
286{
287 if (!FLEXONENAND(this))
288 return (loff_t) block << this->erase_shift;
289 return flexonenand_addr(this, block);
290}
291
292/**
293 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
294 * @param mtd MTD device structure
295 * @param addr address whose erase region needs to be identified
296 */
297int flexonenand_region(struct mtd_info *mtd, loff_t addr)
298{
299 int i;
300
301 for (i = 0; i < mtd->numeraseregions; i++)
302 if (addr < mtd->eraseregions[i].offset)
303 break;
304 return i - 1;
305}
306
307/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900308 * onenand_get_density - [DEFAULT] Get OneNAND density
309 * @param dev_id OneNAND device ID
310 *
311 * Get OneNAND density from device ID
312 */
313static inline int onenand_get_density(int dev_id)
314{
315 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
316 return (density & ONENAND_DEVICE_DENSITY_MASK);
317}
318
319/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900320 * onenand_command - [DEFAULT] Send command to OneNAND device
321 * @param mtd MTD device structure
322 * @param cmd the command to be sent
323 * @param addr offset to read from or write to
324 * @param len number of bytes to read or write
325 *
326 * Send command to OneNAND device. This function is used for middle/large page
327 * devices (1KB/2KB Bytes per page)
328 */
329static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
330 size_t len)
331{
332 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530333 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900334 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530335
Kyungmin Park79e90462007-09-10 17:13:49 +0900336 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530337 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900338
339 /* Address translation */
340 switch (cmd) {
341 case ONENAND_CMD_UNLOCK:
342 case ONENAND_CMD_LOCK:
343 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900344 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900345 block = -1;
346 page = -1;
347 break;
348
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530349 case FLEXONENAND_CMD_PI_ACCESS:
350 /* addr contains die index */
351 block = addr * this->density_mask;
352 page = -1;
353 break;
354
Kyungmin Park79e90462007-09-10 17:13:49 +0900355 case ONENAND_CMD_ERASE:
356 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530357 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900358 page = -1;
359 break;
360
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530361 case FLEXONENAND_CMD_READ_PI:
362 cmd = ONENAND_CMD_READ;
363 block = addr * this->density_mask;
364 page = 0;
365 break;
366
Kyungmin Park79e90462007-09-10 17:13:49 +0900367 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530368 block = onenand_block(this, addr);
369 page = (int) (addr
370 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900371 page &= this->page_mask;
372 break;
373 }
374
375 /* NOTE: The setting order of the registers is very important! */
376 if (cmd == ONENAND_CMD_BUFFERRAM) {
377 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900378 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900379 this->write_word(value,
380 this->base + ONENAND_REG_START_ADDRESS2);
381
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100382 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530383 ONENAND_SET_BUFFERRAM0(this);
384 else
385 /* Switch to the next data buffer */
386 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900387
388 return 0;
389 }
390
391 if (block != -1) {
392 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900393 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900394 this->write_word(value,
395 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900396
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530397 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900398 value = onenand_bufferram_address(this, block);
399 this->write_word(value,
400 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900401 }
402
403 if (page != -1) {
404 int dataram;
405
406 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530407 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900408 case ONENAND_CMD_READ:
409 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100410 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530411 dataram = ONENAND_SET_BUFFERRAM0(this);
412 else
413 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
414
Kyungmin Park79e90462007-09-10 17:13:49 +0900415 break;
416
417 default:
418 dataram = ONENAND_CURRENT_BUFFERRAM(this);
419 break;
420 }
421
422 /* Write 'FPA, FSA' of Flash */
423 value = onenand_page_address(page, sectors);
424 this->write_word(value,
425 this->base + ONENAND_REG_START_ADDRESS8);
426
427 /* Write 'BSA, BSC' of DataRAM */
428 value = onenand_buffer_address(dataram, sectors, count);
429 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900430 }
431
432 /* Interrupt clear */
433 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
434 /* Write command */
435 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
436
437 return 0;
438}
439
440/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530441 * onenand_read_ecc - return ecc status
442 * @param this onenand chip structure
443 */
444static int onenand_read_ecc(struct onenand_chip *this)
445{
446 int ecc, i;
447
448 if (!FLEXONENAND(this))
449 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
450
451 for (i = 0; i < 4; i++) {
452 ecc = this->read_word(this->base
453 + ((ONENAND_REG_ECC_STATUS + i) << 1));
454 if (likely(!ecc))
455 continue;
456 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
457 return ONENAND_ECC_2BIT_ALL;
458 }
459
460 return 0;
461}
462
463/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900464 * onenand_wait - [DEFAULT] wait until the command is done
465 * @param mtd MTD device structure
466 * @param state state to select the max. timeout value
467 *
468 * Wait for command done. This applies to all OneNAND command
469 * Read can take up to 30us, erase up to 2ms and program up to 350us
470 * according to general OneNAND specs
471 */
472static int onenand_wait(struct mtd_info *mtd, int state)
473{
474 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +0900475 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530476 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900477
Ladislav Michl243af2f2016-07-12 20:28:19 +0200478 /* Wait at most 20ms ... */
479 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
480 u32 time_start = get_timer(0);
481 do {
Stefan Roese80877fa2022-09-02 14:10:46 +0200482 schedule();
Ladislav Michl243af2f2016-07-12 20:28:19 +0200483 if (get_timer(time_start) > timeo)
484 return -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +0900485 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +0200486 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park79e90462007-09-10 17:13:49 +0900487
488 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
489
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530490 if (interrupt & ONENAND_INT_READ) {
491 int ecc = onenand_read_ecc(this);
492 if (ecc & ONENAND_ECC_2BIT_ALL) {
493 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
494 return -EBADMSG;
495 }
496 }
497
Kyungmin Park79e90462007-09-10 17:13:49 +0900498 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900499 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
500 if (ctrl & ONENAND_CTRL_LOCK)
501 printk("onenand_wait: it's locked error = 0x%04x\n",
502 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900503
Kyungmin Park79e90462007-09-10 17:13:49 +0900504 return -EIO;
505 }
506
Kyungmin Park79e90462007-09-10 17:13:49 +0900507
508 return 0;
509}
510
511/**
512 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
513 * @param mtd MTD data structure
514 * @param area BufferRAM area
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100515 * Return: offset given area
Kyungmin Park79e90462007-09-10 17:13:49 +0900516 *
517 * Return BufferRAM offset given area
518 */
519static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
520{
521 struct onenand_chip *this = mtd->priv;
522
523 if (ONENAND_CURRENT_BUFFERRAM(this)) {
524 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900525 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900526 if (area == ONENAND_SPARERAM)
527 return mtd->oobsize;
528 }
529
530 return 0;
531}
532
533/**
534 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
535 * @param mtd MTD data structure
536 * @param area BufferRAM area
537 * @param buffer the databuffer to put/get data
538 * @param offset offset to read from or write to
539 * @param count number of bytes to read/write
540 *
541 * Read the BufferRAM area
542 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900543static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900544 unsigned char *buffer, int offset,
545 size_t count)
546{
547 struct onenand_chip *this = mtd->priv;
548 void __iomem *bufferram;
549
550 bufferram = this->base + area;
551 bufferram += onenand_bufferram_offset(mtd, area);
552
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200553 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900554
555 return 0;
556}
557
558/**
559 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
560 * @param mtd MTD data structure
561 * @param area BufferRAM area
562 * @param buffer the databuffer to put/get data
563 * @param offset offset to read from or write to
564 * @param count number of bytes to read/write
565 *
566 * Read the BufferRAM area with Sync. Burst Mode
567 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900568static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900569 unsigned char *buffer, int offset,
570 size_t count)
571{
572 struct onenand_chip *this = mtd->priv;
573 void __iomem *bufferram;
574
575 bufferram = this->base + area;
576 bufferram += onenand_bufferram_offset(mtd, area);
577
578 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
579
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200580 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900581
582 this->mmcontrol(mtd, 0);
583
584 return 0;
585}
586
587/**
588 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
589 * @param mtd MTD data structure
590 * @param area BufferRAM area
591 * @param buffer the databuffer to put/get data
592 * @param offset offset to read from or write to
593 * @param count number of bytes to read/write
594 *
595 * Write the BufferRAM area
596 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900597static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900598 const unsigned char *buffer, int offset,
599 size_t count)
600{
601 struct onenand_chip *this = mtd->priv;
602 void __iomem *bufferram;
603
604 bufferram = this->base + area;
605 bufferram += onenand_bufferram_offset(mtd, area);
606
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200607 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900608
609 return 0;
610}
611
612/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100613 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
614 * @param mtd MTD data structure
615 * @param addr address to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100616 * Return: blockpage address
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100617 *
618 * Get blockpage address at 2x program mode
619 */
620static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
621{
622 struct onenand_chip *this = mtd->priv;
623 int blockpage, block, page;
624
625 /* Calculate the even block number */
626 block = (int) (addr >> this->erase_shift) & ~1;
627 /* Is it the odd plane? */
628 if (addr & this->writesize)
629 block++;
630 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
631 blockpage = (block << 7) | page;
632
633 return blockpage;
634}
635
636/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900637 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
638 * @param mtd MTD data structure
639 * @param addr address to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100640 * Return: 1 if there are valid data, otherwise 0
Kyungmin Park79e90462007-09-10 17:13:49 +0900641 *
642 * Check bufferram if there is data we required
643 */
644static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
645{
646 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900647 int blockpage, found = 0;
648 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900649
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900650 if (ONENAND_IS_2PLANE(this))
651 blockpage = onenand_get_2x_blockpage(mtd, addr);
652 else
653 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900654
655 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900656 i = ONENAND_CURRENT_BUFFERRAM(this);
657 if (this->bufferram[i].blockpage == blockpage)
658 found = 1;
659 else {
660 /* Check another BufferRAM */
661 i = ONENAND_NEXT_BUFFERRAM(this);
662 if (this->bufferram[i].blockpage == blockpage) {
663 ONENAND_SET_NEXT_BUFFERRAM(this);
664 found = 1;
665 }
666 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900667
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900668 if (found && ONENAND_IS_DDP(this)) {
669 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530670 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900671 int value = onenand_bufferram_address(this, block);
672 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
673 }
674
675 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900676}
677
678/**
679 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
680 * @param mtd MTD data structure
681 * @param addr address to update
682 * @param valid valid flag
683 *
684 * Update BufferRAM information
685 */
686static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
687 int valid)
688{
689 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900690 int blockpage;
691 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900692
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900693 if (ONENAND_IS_2PLANE(this))
694 blockpage = onenand_get_2x_blockpage(mtd, addr);
695 else
696 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900697
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900698 /* Invalidate another BufferRAM */
699 i = ONENAND_NEXT_BUFFERRAM(this);
700 if (this->bufferram[i].blockpage == blockpage)
701 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900702
703 /* Update BufferRAM */
704 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900705 if (valid)
706 this->bufferram[i].blockpage = blockpage;
707 else
708 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900709
710 return 0;
711}
712
713/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900714 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
715 * @param mtd MTD data structure
716 * @param addr start address to invalidate
717 * @param len length to invalidate
718 *
719 * Invalidate BufferRAM information
720 */
721static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200722 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900723{
724 struct onenand_chip *this = mtd->priv;
725 int i;
726 loff_t end_addr = addr + len;
727
728 /* Invalidate BufferRAM */
729 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900730 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900731
732 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900733 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900734 }
735}
736
737/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900738 * onenand_get_device - [GENERIC] Get chip for selected access
739 * @param mtd MTD device structure
740 * @param new_state the state which is requested
741 *
742 * Get the device and lock it for exclusive access
743 */
744static void onenand_get_device(struct mtd_info *mtd, int new_state)
745{
746 /* Do nothing */
747}
748
749/**
750 * onenand_release_device - [GENERIC] release chip
751 * @param mtd MTD device structure
752 *
753 * Deselect, release chip lock and wake up anyone waiting on the device
754 */
755static void onenand_release_device(struct mtd_info *mtd)
756{
757 /* Do nothing */
758}
759
760/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000761 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900762 * @param mtd MTD device structure
763 * @param buf destination address
764 * @param column oob offset to read from
765 * @param thislen oob length to read
766 */
767static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
768 int column, int thislen)
769{
770 struct onenand_chip *this = mtd->priv;
771 struct nand_oobfree *free;
772 int readcol = column;
773 int readend = column + thislen;
774 int lastgap = 0;
775 unsigned int i;
776 uint8_t *oob_buf = this->oob_buf;
777
778 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530779 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
780 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900781 if (readcol >= lastgap)
782 readcol += free->offset - lastgap;
783 if (readend >= lastgap)
784 readend += free->offset - lastgap;
785 lastgap = free->offset + free->length;
786 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900787 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900788 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530789 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
790 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900791 int free_end = free->offset + free->length;
792 if (free->offset < readend && free_end > readcol) {
793 int st = max_t(int,free->offset,readcol);
794 int ed = min_t(int,free_end,readend);
795 int n = ed - st;
796 memcpy(buf, oob_buf + st, n);
797 buf += n;
798 } else if (column == 0)
799 break;
800 }
801 return 0;
802}
803
804/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530805 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
806 * @param mtd MTD device structure
807 * @param addr address to recover
808 * @param status return value from onenand_wait
809 *
810 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
811 * lower page address and MSB page has higher page address in paired pages.
812 * If power off occurs during MSB page program, the paired LSB page data can
813 * become corrupt. LSB page recovery read is a way to read LSB page though page
814 * data are corrupted. When uncorrectable error occurs as a result of LSB page
815 * read after power up, issue LSB page recovery read.
816 */
817static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
818{
819 struct onenand_chip *this = mtd->priv;
820 int i;
821
822 /* Recovery is only for Flex-OneNAND */
823 if (!FLEXONENAND(this))
824 return status;
825
826 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000827 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530828 return status;
829
830 /* check if address lies in MLC region */
831 i = flexonenand_region(mtd, addr);
832 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
833 return status;
834
835 printk("onenand_recover_lsb:"
836 "Attempting to recover from uncorrectable read\n");
837
838 /* Issue the LSB page recovery command */
839 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
840 return this->wait(mtd, FL_READING);
841}
842
843/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900844 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900845 * @param mtd MTD device structure
846 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900847 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900848 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900849 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900850 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900851static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
852 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900853{
854 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900855 struct mtd_ecc_stats stats;
856 size_t len = ops->len;
857 size_t ooblen = ops->ooblen;
858 u_char *buf = ops->datbuf;
859 u_char *oobbuf = ops->oobbuf;
860 int read = 0, column, thislen;
861 int oobread = 0, oobcolumn, thisooblen, oobsize;
862 int ret = 0, boundary = 0;
863 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900864
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900865 pr_debug("onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
866 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900867
Sergey Lapin3a38a552013-01-14 03:46:50 +0000868 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900869 oobsize = this->ecclayout->oobavail;
870 else
871 oobsize = mtd->oobsize;
872
873 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900874
875 /* Do not allow reads past end of device */
876 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900877 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
878 ops->retlen = 0;
879 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900880 return -EINVAL;
881 }
882
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900883 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900884
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900885 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530886 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900887
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900888 /* Do first load to bufferRAM */
889 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900890 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900891 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900892 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900893 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530894 if (unlikely(ret))
895 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900896 onenand_update_bufferram(mtd, from, !ret);
897 if (ret == -EBADMSG)
898 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900899 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900900 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900901
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900902 thislen = min_t(int, writesize, len - read);
903 column = from & (writesize - 1);
904 if (column + thislen > writesize)
905 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900906
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900907 while (!ret) {
908 /* If there is more to load then start next load */
909 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100910 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900911 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900912 this->command(mtd, ONENAND_CMD_READ, from, writesize);
913 /*
914 * Chip boundary handling in DDP
915 * Now we issued chip 1 read and pointed chip 1
916 * bufferam so we have to point chip 0 bufferam.
917 */
918 if (ONENAND_IS_DDP(this) &&
919 unlikely(from == (this->chipsize >> 1))) {
920 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
921 boundary = 1;
922 } else
923 boundary = 0;
924 ONENAND_SET_PREV_BUFFERRAM(this);
925 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900926
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900927 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900928 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900929
930 /* Read oob area if needed */
931 if (oobbuf) {
932 thisooblen = oobsize - oobcolumn;
933 thisooblen = min_t(int, thisooblen, ooblen - oobread);
934
Sergey Lapin3a38a552013-01-14 03:46:50 +0000935 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900936 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
937 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900938 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900939 oobread += thisooblen;
940 oobbuf += thisooblen;
941 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900942 }
943
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100944 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530945 this->command(mtd, ONENAND_CMD_READ, from, writesize);
946 ret = this->wait(mtd, FL_READING);
947 if (unlikely(ret))
948 ret = onenand_recover_lsb(mtd, from, ret);
949 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000950 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530951 ret = 0;
952 }
953
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900954 /* See if we are done */
955 read += thislen;
956 if (read == len)
957 break;
958 /* Set up for next read from bufferRAM */
959 if (unlikely(boundary))
960 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100961 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530962 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900963 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900964 thislen = min_t(int, writesize, len - read);
965 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900966
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100967 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530968 /* Now wait for load */
969 ret = this->wait(mtd, FL_READING);
970 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000971 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530972 ret = 0;
973 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900974 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900975
976 /*
977 * Return success, if no ECC failures, else -EBADMSG
978 * fs driver will take care of that, because
979 * retlen == desired len and result == -EBADMSG
980 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900981 ops->retlen = read;
982 ops->oobretlen = oobread;
983
984 if (ret)
985 return ret;
986
987 if (mtd->ecc_stats.failed - stats.failed)
988 return -EBADMSG;
989
Paul Burton700a76c2013-09-04 15:16:56 +0100990 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
991 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900992}
993
994/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900995 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
996 * @param mtd MTD device structure
997 * @param from offset to read from
998 * @param ops oob operation description structure
999 *
1000 * OneNAND read out-of-band data from the spare area
1001 */
1002static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1003 struct mtd_oob_ops *ops)
1004{
1005 struct onenand_chip *this = mtd->priv;
1006 struct mtd_ecc_stats stats;
1007 int read = 0, thislen, column, oobsize;
1008 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001009 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001010 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301011 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001012
1013 from += ops->ooboffs;
1014
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001015 pr_debug("onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
1016 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001017
1018 /* Initialize return length value */
1019 ops->oobretlen = 0;
1020
Sergey Lapin3a38a552013-01-14 03:46:50 +00001021 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001022 oobsize = this->ecclayout->oobavail;
1023 else
1024 oobsize = mtd->oobsize;
1025
1026 column = from & (mtd->oobsize - 1);
1027
1028 if (unlikely(column >= oobsize)) {
1029 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1030 return -EINVAL;
1031 }
1032
1033 /* Do not allow reads past end of device */
1034 if (unlikely(from >= mtd->size ||
1035 column + len > ((mtd->size >> this->page_shift) -
1036 (from >> this->page_shift)) * oobsize)) {
1037 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1038 return -EINVAL;
1039 }
1040
1041 stats = mtd->ecc_stats;
1042
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001043 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1044 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301045
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001046 while (read < len) {
1047 thislen = oobsize - column;
1048 thislen = min_t(int, thislen, len);
1049
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001050 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301051 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001052
1053 onenand_update_bufferram(mtd, from, 0);
1054
1055 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301056 if (unlikely(ret))
1057 ret = onenand_recover_lsb(mtd, from, ret);
1058
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001059 if (ret && ret != -EBADMSG) {
1060 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1061 break;
1062 }
1063
Sergey Lapin3a38a552013-01-14 03:46:50 +00001064 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001065 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1066 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001067 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001068
1069 read += thislen;
1070
1071 if (read == len)
1072 break;
1073
1074 buf += thislen;
1075
1076 /* Read more? */
1077 if (read < len) {
1078 /* Page size */
1079 from += mtd->writesize;
1080 column = 0;
1081 }
1082 }
1083
1084 ops->oobretlen = read;
1085
1086 if (ret)
1087 return ret;
1088
1089 if (mtd->ecc_stats.failed - stats.failed)
1090 return -EBADMSG;
1091
1092 return 0;
1093}
1094
1095/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001096 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1097 * @param mtd MTD device structure
1098 * @param from offset to read from
1099 * @param len number of bytes to read
1100 * @param retlen pointer to variable to store the number of read bytes
1101 * @param buf the databuffer to put data
1102 *
1103 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1104*/
1105int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1106 size_t * retlen, u_char * buf)
1107{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001108 struct mtd_oob_ops ops = {
1109 .len = len,
1110 .ooblen = 0,
1111 .datbuf = buf,
1112 .oobbuf = NULL,
1113 };
1114 int ret;
1115
1116 onenand_get_device(mtd, FL_READING);
1117 ret = onenand_read_ops_nolock(mtd, from, &ops);
1118 onenand_release_device(mtd);
1119
1120 *retlen = ops.retlen;
1121 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001122}
1123
1124/**
1125 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1126 * @param mtd MTD device structure
1127 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001128 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001129 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001130 * OneNAND main and/or out-of-band
1131 */
1132int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1133 struct mtd_oob_ops *ops)
1134{
1135 int ret;
1136
1137 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001138 case MTD_OPS_PLACE_OOB:
1139 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001140 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001141 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001142 /* Not implemented yet */
1143 default:
1144 return -EINVAL;
1145 }
1146
1147 onenand_get_device(mtd, FL_READING);
1148 if (ops->datbuf)
1149 ret = onenand_read_ops_nolock(mtd, from, ops);
1150 else
1151 ret = onenand_read_oob_nolock(mtd, from, ops);
1152 onenand_release_device(mtd);
1153
1154 return ret;
1155}
1156
1157/**
1158 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1159 * @param mtd MTD device structure
1160 * @param state state to select the max. timeout value
1161 *
1162 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001163 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001164static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001165{
1166 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001167 unsigned int interrupt;
1168 unsigned int ctrl;
1169
Ladislav Michl243af2f2016-07-12 20:28:19 +02001170 /* Wait at most 20ms ... */
1171 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
1172 u32 time_start = get_timer(0);
1173 do {
Stefan Roese80877fa2022-09-02 14:10:46 +02001174 schedule();
Ladislav Michl243af2f2016-07-12 20:28:19 +02001175 if (get_timer(time_start) > timeo)
1176 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001177 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +02001178 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001179
1180 /* To get correct interrupt status in timeout case */
1181 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1182 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1183
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001184 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301185 int ecc = onenand_read_ecc(this);
1186 if (ecc & ONENAND_ECC_2BIT_ALL) {
1187 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1188 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001189 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301190 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001191 } else {
1192 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1193 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1194 return ONENAND_BBT_READ_FATAL_ERROR;
1195 }
1196
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001197 /* Initial bad block case: 0x2400 or 0x0400 */
1198 if (ctrl & ONENAND_CTRL_ERROR) {
1199 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1200 return ONENAND_BBT_READ_ERROR;
1201 }
1202
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001203 return 0;
1204}
1205
1206/**
1207 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1208 * @param mtd MTD device structure
1209 * @param from offset to read from
1210 * @param ops oob operation description structure
1211 *
1212 * OneNAND read out-of-band data from the spare area for bbt scan
1213 */
1214int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1215 struct mtd_oob_ops *ops)
1216{
1217 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001218 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301219 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001220 size_t len = ops->ooblen;
1221 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001222
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001223 pr_debug("onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
1224 (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001225
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001226 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1227 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301228
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001229 /* Initialize return value */
1230 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001231
1232 /* Do not allow reads past end of device */
1233 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001234 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1235 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001236 }
1237
1238 /* Grab the lock and see if the device is available */
1239 onenand_get_device(mtd, FL_READING);
1240
1241 column = from & (mtd->oobsize - 1);
1242
1243 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001244
Kyungmin Park79e90462007-09-10 17:13:49 +09001245 thislen = mtd->oobsize - column;
1246 thislen = min_t(int, thislen, len);
1247
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001248 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301249 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001250
1251 onenand_update_bufferram(mtd, from, 0);
1252
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001253 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301254 if (unlikely(ret))
1255 ret = onenand_recover_lsb(mtd, from, ret);
1256
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001257 if (ret)
1258 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001259
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001260 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001261 read += thislen;
1262 if (read == len)
1263 break;
1264
Kyungmin Park79e90462007-09-10 17:13:49 +09001265 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001266
Kyungmin Park79e90462007-09-10 17:13:49 +09001267 /* Read more? */
1268 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001269 /* Update Page size */
1270 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001271 column = 0;
1272 }
1273 }
1274
1275 /* Deselect and wake up anyone waiting on the device */
1276 onenand_release_device(mtd);
1277
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001278 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001279 return ret;
1280}
1281
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001282
Kyungmin Park79e90462007-09-10 17:13:49 +09001283#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1284/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001285 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1286 * @param mtd MTD device structure
1287 * @param buf the databuffer to verify
1288 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001289 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001290static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001291{
1292 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001293 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301294 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001295
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001296 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1297 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301298
1299 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001300 onenand_update_bufferram(mtd, to, 0);
1301 status = this->wait(mtd, FL_READING);
1302 if (status)
1303 return status;
1304
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001305 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001306 for (i = 0; i < mtd->oobsize; i++)
1307 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1308 return -EBADMSG;
1309
1310 return 0;
1311}
1312
1313/**
1314 * onenand_verify - [GENERIC] verify the chip contents after a write
1315 * @param mtd MTD device structure
1316 * @param buf the databuffer to verify
1317 * @param addr offset to read from
1318 * @param len number of bytes to read and compare
1319 */
1320static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1321{
1322 struct onenand_chip *this = mtd->priv;
1323 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001324 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001325 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001326
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001327 while (len != 0) {
1328 thislen = min_t(int, this->writesize, len);
1329 column = addr & (this->writesize - 1);
1330 if (column + thislen > this->writesize)
1331 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001332
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001333 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001334
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001335 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001336
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001337 ret = this->wait(mtd, FL_READING);
1338 if (ret)
1339 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001340
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001341 onenand_update_bufferram(mtd, addr, 1);
1342
1343 dataram = this->base + ONENAND_DATARAM;
1344 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1345
1346 if (memcmp(buf, dataram + column, thislen))
1347 return -EBADMSG;
1348
1349 len -= thislen;
1350 buf += thislen;
1351 addr += thislen;
1352 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001353
1354 return 0;
1355}
1356#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001357#define onenand_verify(...) (0)
1358#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001359#endif
1360
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001361#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001362
1363/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001364 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001365 * @param mtd MTD device structure
1366 * @param oob_buf oob buffer
1367 * @param buf source address
1368 * @param column oob offset to write to
1369 * @param thislen oob length to write
1370 */
1371static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1372 const u_char *buf, int column, int thislen)
1373{
1374 struct onenand_chip *this = mtd->priv;
1375 struct nand_oobfree *free;
1376 int writecol = column;
1377 int writeend = column + thislen;
1378 int lastgap = 0;
1379 unsigned int i;
1380
1381 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301382 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1383 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001384 if (writecol >= lastgap)
1385 writecol += free->offset - lastgap;
1386 if (writeend >= lastgap)
1387 writeend += free->offset - lastgap;
1388 lastgap = free->offset + free->length;
1389 }
1390 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301391 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1392 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001393 int free_end = free->offset + free->length;
1394 if (free->offset < writeend && free_end > writecol) {
1395 int st = max_t(int,free->offset,writecol);
1396 int ed = min_t(int,free_end,writeend);
1397 int n = ed - st;
1398 memcpy(oob_buf + st, buf, n);
1399 buf += n;
1400 } else if (column == 0)
1401 break;
1402 }
1403 return 0;
1404}
1405
1406/**
1407 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1408 * @param mtd MTD device structure
1409 * @param to offset to write to
1410 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001411 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001412 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001413 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001414static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1415 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001416{
1417 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001418 int written = 0, column, thislen, subpage;
1419 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1420 size_t len = ops->len;
1421 size_t ooblen = ops->ooblen;
1422 const u_char *buf = ops->datbuf;
1423 const u_char *oob = ops->oobbuf;
1424 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001425 int ret = 0;
1426
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001427 pr_debug("onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
1428 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001429
1430 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001431 ops->retlen = 0;
1432 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001433
Kyungmin Park79e90462007-09-10 17:13:49 +09001434 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001435 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1436 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001437 return -EINVAL;
1438 }
1439
Sergey Lapin3a38a552013-01-14 03:46:50 +00001440 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001441 oobsize = this->ecclayout->oobavail;
1442 else
1443 oobsize = mtd->oobsize;
1444
1445 oobcolumn = to & (mtd->oobsize - 1);
1446
1447 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001448
1449 /* Loop until all data write */
1450 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001451 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001452
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001453 thislen = min_t(int, mtd->writesize - column, len - written);
1454 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001455
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001456 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001457
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001458 /* Partial page write */
1459 subpage = thislen < mtd->writesize;
1460 if (subpage) {
1461 memset(this->page_buf, 0xff, mtd->writesize);
1462 memcpy(this->page_buf + column, buf, thislen);
1463 wbuf = this->page_buf;
1464 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001465
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001466 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001467
1468 if (oob) {
1469 oobbuf = this->oob_buf;
1470
1471 /* We send data to spare ram with oobsize
1472 * * to prevent byte access */
1473 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001474 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001475 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1476 else
1477 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1478
1479 oobwritten += thisooblen;
1480 oob += thisooblen;
1481 oobcolumn = 0;
1482 } else
1483 oobbuf = (u_char *) ffchars;
1484
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001485 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001486
1487 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001488
1489 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001490
1491 /* In partial page write we don't update bufferram */
1492 onenand_update_bufferram(mtd, to, !ret && !subpage);
1493 if (ONENAND_IS_2PLANE(this)) {
1494 ONENAND_SET_BUFFERRAM1(this);
1495 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1496 }
1497
Kyungmin Park79e90462007-09-10 17:13:49 +09001498 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001499 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001500 break;
1501 }
1502
Kyungmin Park79e90462007-09-10 17:13:49 +09001503 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001504 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001505 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001506 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001507 break;
1508 }
1509
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001510 written += thislen;
1511
Kyungmin Park79e90462007-09-10 17:13:49 +09001512 if (written == len)
1513 break;
1514
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001515 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001516 to += thislen;
1517 buf += thislen;
1518 }
1519
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001520 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001521
1522 return ret;
1523}
1524
1525/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001526 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001527 * @param mtd MTD device structure
1528 * @param to offset to write to
1529 * @param len number of bytes to write
1530 * @param retlen pointer to variable to store the number of written bytes
1531 * @param buf the data to write
1532 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001533 *
1534 * OneNAND write out-of-band
1535 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001536static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1537 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001538{
1539 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001540 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301541 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001542 u_char *oobbuf;
1543 size_t len = ops->ooblen;
1544 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001545 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001546
1547 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001548
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001549 pr_debug("onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
1550 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001551
1552 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001553 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001554
Sergey Lapin3a38a552013-01-14 03:46:50 +00001555 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001556 oobsize = this->ecclayout->oobavail;
1557 else
1558 oobsize = mtd->oobsize;
1559
1560 column = to & (mtd->oobsize - 1);
1561
1562 if (unlikely(column >= oobsize)) {
1563 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001564 return -EINVAL;
1565 }
1566
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001567 /* For compatibility with NAND: Do not allow write past end of page */
1568 if (unlikely(column + len > oobsize)) {
1569 printk(KERN_ERR "onenand_write_oob_nolock: "
1570 "Attempt to write past end of page\n");
1571 return -EINVAL;
1572 }
1573
1574 /* Do not allow reads past end of device */
1575 if (unlikely(to >= mtd->size ||
1576 column + len > ((mtd->size >> this->page_shift) -
1577 (to >> this->page_shift)) * oobsize)) {
1578 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1579 return -EINVAL;
1580 }
1581
1582 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001583
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001584 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1585 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301586
Kyungmin Park79e90462007-09-10 17:13:49 +09001587 /* Loop until all data write */
1588 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001589 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001590
1591 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1592
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001593 /* We send data to spare ram with oobsize
1594 * to prevent byte access */
1595 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001596 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001597 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1598 else
1599 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001600 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001601
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001602 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301603 /* Set main area of DataRAM to 0xff*/
1604 memset(this->page_buf, 0xff, mtd->writesize);
1605 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1606 this->page_buf, 0, mtd->writesize);
1607 }
1608
1609 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001610
1611 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001612 if (ONENAND_IS_2PLANE(this)) {
1613 ONENAND_SET_BUFFERRAM1(this);
1614 onenand_update_bufferram(mtd, to + this->writesize, 0);
1615 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001616
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001617 ret = this->wait(mtd, FL_WRITING);
1618 if (ret) {
1619 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001620 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001621 }
1622
1623 ret = onenand_verify_oob(mtd, oobbuf, to);
1624 if (ret) {
1625 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1626 break;
1627 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001628
1629 written += thislen;
1630 if (written == len)
1631 break;
1632
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001633 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001634 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001635 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001636 }
1637
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001638 ops->oobretlen = written;
1639
1640 return ret;
1641}
1642
1643/**
1644 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1645 * @param mtd MTD device structure
1646 * @param to offset to write to
1647 * @param len number of bytes to write
1648 * @param retlen pointer to variable to store the number of written bytes
1649 * @param buf the data to write
1650 *
1651 * Write with ECC
1652 */
1653int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1654 size_t * retlen, const u_char * buf)
1655{
1656 struct mtd_oob_ops ops = {
1657 .len = len,
1658 .ooblen = 0,
1659 .datbuf = (u_char *) buf,
1660 .oobbuf = NULL,
1661 };
1662 int ret;
1663
1664 onenand_get_device(mtd, FL_WRITING);
1665 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001666 onenand_release_device(mtd);
1667
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001668 *retlen = ops.retlen;
1669 return ret;
1670}
Kyungmin Park79e90462007-09-10 17:13:49 +09001671
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001672/**
1673 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1674 * @param mtd MTD device structure
1675 * @param to offset to write to
1676 * @param ops oob operation description structure
1677 *
1678 * OneNAND write main and/or out-of-band
1679 */
1680int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1681 struct mtd_oob_ops *ops)
1682{
1683 int ret;
1684
1685 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001686 case MTD_OPS_PLACE_OOB:
1687 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001688 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001689 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001690 /* Not implemented yet */
1691 default:
1692 return -EINVAL;
1693 }
1694
1695 onenand_get_device(mtd, FL_WRITING);
1696 if (ops->datbuf)
1697 ret = onenand_write_ops_nolock(mtd, to, ops);
1698 else
1699 ret = onenand_write_oob_nolock(mtd, to, ops);
1700 onenand_release_device(mtd);
1701
1702 return ret;
1703
Kyungmin Park79e90462007-09-10 17:13:49 +09001704}
1705
Kyungmin Park396b0c42008-08-13 09:11:02 +09001706/**
1707 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1708 * @param mtd MTD device structure
1709 * @param ofs offset from device start
1710 * @param allowbbt 1, if its allowed to access the bbt area
1711 *
1712 * Check, if the block is bad, Either by reading the bad block table or
1713 * calling of the scan function.
1714 */
1715static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1716{
1717 struct onenand_chip *this = mtd->priv;
1718 struct bbm_info *bbm = this->bbm;
1719
1720 /* Return info from the table */
1721 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1722}
1723
1724
Kyungmin Park79e90462007-09-10 17:13:49 +09001725/**
1726 * onenand_erase - [MTD Interface] erase block(s)
1727 * @param mtd MTD device structure
1728 * @param instr erase instruction
1729 *
1730 * Erase one ore more blocks
1731 */
1732int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1733{
1734 struct onenand_chip *this = mtd->priv;
1735 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301736 loff_t addr = instr->addr;
1737 unsigned int len = instr->len;
1738 int ret = 0, i;
1739 struct mtd_erase_region_info *region = NULL;
1740 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001741
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001742 pr_debug("onenand_erase: start = 0x%08x, len = %i\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301743 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001744
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301745 if (FLEXONENAND(this)) {
1746 /* Find the eraseregion of this address */
1747 i = flexonenand_region(mtd, addr);
1748 region = &mtd->eraseregions[i];
1749
1750 block_size = region->erasesize;
1751 region_end = region->offset
1752 + region->erasesize * region->numblocks;
1753
1754 /* Start address within region must align on block boundary.
1755 * Erase region's start offset is always block start address.
1756 */
1757 if (unlikely((addr - region->offset) & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001758 pr_debug("onenand_erase:" " Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301759 return -EINVAL;
1760 }
1761 } else {
1762 block_size = 1 << this->erase_shift;
1763
1764 /* Start address must align on block boundary */
1765 if (unlikely(addr & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001766 pr_debug("onenand_erase:" "Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301767 return -EINVAL;
1768 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001769 }
1770
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301771 /* Length must align on block boundary */
1772 if (unlikely(len & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001773 pr_debug("onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001774 return -EINVAL;
1775 }
1776
Kyungmin Park79e90462007-09-10 17:13:49 +09001777 /* Grab the lock and see if the device is available */
1778 onenand_get_device(mtd, FL_ERASING);
1779
1780 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001781 instr->state = MTD_ERASING;
1782
1783 while (len) {
1784
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001785 /* Check if we have a bad block, we do not erase bad blocks */
1786 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1787 printk(KERN_WARNING "onenand_erase: attempt to erase"
1788 " a bad block at addr 0x%08x\n",
1789 (unsigned int) addr);
1790 instr->state = MTD_ERASE_FAILED;
1791 goto erase_exit;
1792 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001793
1794 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1795
Kyungmin Park396b0c42008-08-13 09:11:02 +09001796 onenand_invalidate_bufferram(mtd, addr, block_size);
1797
Kyungmin Park79e90462007-09-10 17:13:49 +09001798 ret = this->wait(mtd, FL_ERASING);
1799 /* Check, if it is write protected */
1800 if (ret) {
1801 if (ret == -EPERM)
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001802 pr_debug("onenand_erase: "
1803 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001804 else
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001805 pr_debug("onenand_erase: "
1806 "Failed erase, block %d\n",
1807 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001808 instr->state = MTD_ERASE_FAILED;
1809 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001810
Kyungmin Park79e90462007-09-10 17:13:49 +09001811 goto erase_exit;
1812 }
1813
1814 len -= block_size;
1815 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301816
1817 if (addr == region_end) {
1818 if (!len)
1819 break;
1820 region++;
1821
1822 block_size = region->erasesize;
1823 region_end = region->offset
1824 + region->erasesize * region->numblocks;
1825
1826 if (len & (block_size - 1)) {
1827 /* This has been checked at MTD
1828 * partitioning level. */
1829 printk("onenand_erase: Unaligned address\n");
1830 goto erase_exit;
1831 }
1832 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001833 }
1834
1835 instr->state = MTD_ERASE_DONE;
1836
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001837erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001838
1839 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +09001840
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
Wolfgang Denk62fb2b42021-09-27 17:42:39 +02002307 * boundary[], diesize[], mtd->size, mtd->erasesize,
2308 * mtd->eraseregions
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302309 * @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
Pali Rohár1b42e852020-10-31 17:32:50 +01002658 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002659 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002660 mtd->_erase = onenand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002661 mtd->_read_oob = onenand_read_oob;
2662 mtd->_write_oob = onenand_write_oob;
2663 mtd->_sync = onenand_sync;
2664 mtd->_block_isbad = onenand_block_isbad;
2665 mtd->_block_markbad = onenand_block_markbad;
Ladislav Michl7dbad5c2016-07-12 20:28:21 +02002666 mtd->writebufsize = mtd->writesize;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002667
Kyungmin Park79e90462007-09-10 17:13:49 +09002668 return 0;
2669}
2670
2671/**
2672 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2673 * @param mtd MTD device structure
2674 * @param maxchips Number of chips to scan for
2675 *
2676 * This fills out all the not initialized function pointers
2677 * with the defaults.
2678 * The flash ID is read and the mtd/chip structures are
2679 * filled with the appropriate values.
2680 */
2681int onenand_scan(struct mtd_info *mtd, int maxchips)
2682{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002683 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002684 struct onenand_chip *this = mtd->priv;
2685
2686 if (!this->read_word)
2687 this->read_word = onenand_readw;
2688 if (!this->write_word)
2689 this->write_word = onenand_writew;
2690
2691 if (!this->command)
2692 this->command = onenand_command;
2693 if (!this->wait)
2694 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002695 if (!this->bbt_wait)
2696 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002697
2698 if (!this->read_bufferram)
2699 this->read_bufferram = onenand_read_bufferram;
2700 if (!this->write_bufferram)
2701 this->write_bufferram = onenand_write_bufferram;
2702
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002703 if (!this->chip_probe)
2704 this->chip_probe = onenand_chip_probe;
2705
Kyungmin Park87b49502008-11-13 15:14:33 +09002706 if (!this->block_markbad)
2707 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002708 if (!this->scan_bbt)
2709 this->scan_bbt = onenand_default_bbt;
2710
Kyungmin Park79e90462007-09-10 17:13:49 +09002711 if (onenand_probe(mtd))
2712 return -ENXIO;
2713
2714 /* Set Sync. Burst Read after probing */
2715 if (this->mmcontrol) {
2716 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2717 this->read_bufferram = onenand_sync_read_bufferram;
2718 }
2719
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002720 /* Allocate buffers, if necessary */
2721 if (!this->page_buf) {
2722 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2723 if (!this->page_buf) {
2724 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2725 return -ENOMEM;
2726 }
2727 this->options |= ONENAND_PAGEBUF_ALLOC;
2728 }
2729 if (!this->oob_buf) {
2730 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2731 if (!this->oob_buf) {
2732 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2733 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2734 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2735 kfree(this->page_buf);
2736 }
2737 return -ENOMEM;
2738 }
2739 this->options |= ONENAND_OOBBUF_ALLOC;
2740 }
2741
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002742 this->state = FL_READY;
2743
2744 /*
2745 * Allow subpage writes up to oobsize.
2746 */
2747 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302748 case 128:
2749 this->ecclayout = &onenand_oob_128;
2750 mtd->subpage_sft = 0;
2751 break;
2752
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002753 case 64:
2754 this->ecclayout = &onenand_oob_64;
2755 mtd->subpage_sft = 2;
2756 break;
2757
2758 case 32:
2759 this->ecclayout = &onenand_oob_32;
2760 mtd->subpage_sft = 1;
2761 break;
2762
2763 default:
2764 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2765 mtd->oobsize);
2766 mtd->subpage_sft = 0;
2767 /* To prevent kernel oops */
2768 this->ecclayout = &onenand_oob_32;
2769 break;
2770 }
2771
2772 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2773
2774 /*
2775 * The number of bytes available for a client to place data into
2776 * the out of band area
2777 */
2778 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302779
2780 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002781 this->ecclayout->oobfree[i].length; i++)
2782 this->ecclayout->oobavail +=
2783 this->ecclayout->oobfree[i].length;
2784 mtd->oobavail = this->ecclayout->oobavail;
2785
2786 mtd->ecclayout = this->ecclayout;
2787
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002788 /* Unlock whole block */
2789 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002790
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002791 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002792}
2793
2794/**
2795 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2796 * @param mtd MTD device structure
2797 */
2798void onenand_release(struct mtd_info *mtd)
2799{
2800}