blob: c8bc76794b561d5bbd3bdbc8995754849b3d0dbb [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
Simon Glass0f2af882020-05-10 11:40:05 -060022#include <log.h>
Ladislav Michl243af2f2016-07-12 20:28:19 +020023#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070024#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000026#include <linux/compat.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090027#include <linux/mtd/mtd.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060028#include <linux/printk.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020029#include "linux/mtd/flashchip.h"
Kyungmin Park79e90462007-09-10 17:13:49 +090030#include <linux/mtd/onenand.h>
31
32#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090033#include <linux/errno.h>
Fathi BOUDRA95feb702008-08-06 10:06:20 +020034#include <malloc.h>
Kyungmin Park79e90462007-09-10 17:13:49 +090035
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090036/* It should access 16-bit instead of 8-bit */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053037static void *memcpy_16(void *dst, const void *src, unsigned int len)
Kyungmin Park6b85c9f2008-03-31 10:40:36 +090038{
39 void *ret = dst;
40 short *d = dst;
41 const short *s = src;
42
43 len >>= 1;
44 while (len-- > 0)
45 *d++ = *s++;
46 return ret;
47}
48
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010049/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +053050 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
51 * For now, we expose only 64 out of 80 ecc bytes
52 */
53static struct nand_ecclayout onenand_oob_128 = {
54 .eccbytes = 64,
55 .eccpos = {
56 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
57 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
58 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
59 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
60 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
61 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
62 102, 103, 104, 105
63 },
64 .oobfree = {
65 {2, 4}, {18, 4}, {34, 4}, {50, 4},
66 {66, 4}, {82, 4}, {98, 4}, {114, 4}
67 }
68};
69
70/**
Stefan Roesedb7ce8c2008-12-02 11:06:47 +010071 * onenand_oob_64 - oob info for large (2KB) page
72 */
73static struct nand_ecclayout onenand_oob_64 = {
74 .eccbytes = 20,
75 .eccpos = {
76 8, 9, 10, 11, 12,
77 24, 25, 26, 27, 28,
78 40, 41, 42, 43, 44,
79 56, 57, 58, 59, 60,
80 },
81 .oobfree = {
82 {2, 3}, {14, 2}, {18, 3}, {30, 2},
83 {34, 3}, {46, 2}, {50, 3}, {62, 2}
84 }
85};
86
87/**
88 * onenand_oob_32 - oob info for middle (1KB) page
89 */
90static struct nand_ecclayout onenand_oob_32 = {
91 .eccbytes = 10,
92 .eccpos = {
93 8, 9, 10, 11, 12,
94 24, 25, 26, 27, 28,
95 },
96 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
97};
98
Marek Vasut41120122013-12-26 01:01:24 +010099/*
100 * Warning! This array is used with the memcpy_16() function, thus
101 * it must be aligned to 2 bytes. GCC can make this array unaligned
102 * as the array is made of unsigned char, which memcpy16() doesn't
103 * like and will cause unaligned access.
104 */
105static const unsigned char __aligned(2) ffchars[] = {
Kyungmin Park79e90462007-09-10 17:13:49 +0900106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Park79e90462007-09-10 17:13:49 +0900122};
123
124/**
125 * onenand_readw - [OneNAND Interface] Read OneNAND register
126 * @param addr address to read
127 *
128 * Read OneNAND register
129 */
130static unsigned short onenand_readw(void __iomem * addr)
131{
132 return readw(addr);
133}
134
135/**
136 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
137 * @param value value to write
138 * @param addr address to write
139 *
140 * Write OneNAND register with value
141 */
142static void onenand_writew(unsigned short value, void __iomem * addr)
143{
144 writew(value, addr);
145}
146
147/**
148 * onenand_block_address - [DEFAULT] Get block address
149 * @param device the device id
150 * @param block the block
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100151 * Return: translated block address if DDP, otherwise same
Kyungmin Park79e90462007-09-10 17:13:49 +0900152 *
153 * Setup Start Address 1 Register (F100h)
154 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900155static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900156{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900157 /* Device Flash Core select, NAND Flash Block Address */
158 if (block & this->density_mask)
159 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Park79e90462007-09-10 17:13:49 +0900160
161 return block;
162}
163
164/**
165 * onenand_bufferram_address - [DEFAULT] Get bufferram address
166 * @param device the device id
167 * @param block the block
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100168 * Return: set DBS value if DDP, otherwise 0
Kyungmin Park79e90462007-09-10 17:13:49 +0900169 *
170 * Setup Start Address 2 Register (F101h) for DDP
171 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900172static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Park79e90462007-09-10 17:13:49 +0900173{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900174 /* Device BufferRAM Select */
175 if (block & this->density_mask)
176 return ONENAND_DDP_CHIP1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900177
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900178 return ONENAND_DDP_CHIP0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900179}
180
181/**
182 * onenand_page_address - [DEFAULT] Get page address
183 * @param page the page address
184 * @param sector the sector address
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100185 * Return: combined page and sector address
Kyungmin Park79e90462007-09-10 17:13:49 +0900186 *
187 * Setup Start Address 8 Register (F107h)
188 */
189static int onenand_page_address(int page, int sector)
190{
191 /* Flash Page Address, Flash Sector Address */
192 int fpa, fsa;
193
194 fpa = page & ONENAND_FPA_MASK;
195 fsa = sector & ONENAND_FSA_MASK;
196
197 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
198}
199
200/**
201 * onenand_buffer_address - [DEFAULT] Get buffer address
202 * @param dataram1 DataRAM index
203 * @param sectors the sector address
204 * @param count the number of sectors
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100205 * Return: the start buffer value
Kyungmin Park79e90462007-09-10 17:13:49 +0900206 *
207 * Setup Start Buffer Register (F200h)
208 */
209static int onenand_buffer_address(int dataram1, int sectors, int count)
210{
211 int bsa, bsc;
212
213 /* BufferRAM Sector Address */
214 bsa = sectors & ONENAND_BSA_MASK;
215
216 if (dataram1)
217 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
218 else
219 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
220
221 /* BufferRAM Sector Count */
222 bsc = count & ONENAND_BSC_MASK;
223
224 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
225}
226
227/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530228 * flexonenand_block - Return block number for flash address
229 * @param this - OneNAND device structure
230 * @param addr - Address for which block number is needed
231 */
232static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
233{
234 unsigned int boundary, blk, die = 0;
235
236 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
237 die = 1;
238 addr -= this->diesize[0];
239 }
240
241 boundary = this->boundary[die];
242
243 blk = addr >> (this->erase_shift - 1);
244 if (blk > boundary)
245 blk = (blk + boundary + 1) >> 1;
246
247 blk += die ? this->density_mask : 0;
248 return blk;
249}
250
251unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
252{
253 if (!FLEXONENAND(this))
254 return addr >> this->erase_shift;
255 return flexonenand_block(this, addr);
256}
257
258/**
259 * flexonenand_addr - Return address of the block
260 * @this: OneNAND device structure
261 * @block: Block number on Flex-OneNAND
262 *
263 * Return address of the block
264 */
265static loff_t flexonenand_addr(struct onenand_chip *this, int block)
266{
267 loff_t ofs = 0;
268 int die = 0, boundary;
269
270 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
271 block -= this->density_mask;
272 die = 1;
273 ofs = this->diesize[0];
274 }
275
276 boundary = this->boundary[die];
277 ofs += (loff_t) block << (this->erase_shift - 1);
278 if (block > (boundary + 1))
279 ofs += (loff_t) (block - boundary - 1)
280 << (this->erase_shift - 1);
281 return ofs;
282}
283
284loff_t onenand_addr(struct onenand_chip *this, int block)
285{
286 if (!FLEXONENAND(this))
287 return (loff_t) block << this->erase_shift;
288 return flexonenand_addr(this, block);
289}
290
291/**
292 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
293 * @param mtd MTD device structure
294 * @param addr address whose erase region needs to be identified
295 */
296int flexonenand_region(struct mtd_info *mtd, loff_t addr)
297{
298 int i;
299
300 for (i = 0; i < mtd->numeraseregions; i++)
301 if (addr < mtd->eraseregions[i].offset)
302 break;
303 return i - 1;
304}
305
306/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900307 * onenand_get_density - [DEFAULT] Get OneNAND density
308 * @param dev_id OneNAND device ID
309 *
310 * Get OneNAND density from device ID
311 */
312static inline int onenand_get_density(int dev_id)
313{
314 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
315 return (density & ONENAND_DEVICE_DENSITY_MASK);
316}
317
318/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900319 * onenand_command - [DEFAULT] Send command to OneNAND device
320 * @param mtd MTD device structure
321 * @param cmd the command to be sent
322 * @param addr offset to read from or write to
323 * @param len number of bytes to read or write
324 *
325 * Send command to OneNAND device. This function is used for middle/large page
326 * devices (1KB/2KB Bytes per page)
327 */
328static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
329 size_t len)
330{
331 struct onenand_chip *this = mtd->priv;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530332 int value;
Kyungmin Park79e90462007-09-10 17:13:49 +0900333 int block, page;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530334
Kyungmin Park79e90462007-09-10 17:13:49 +0900335 /* Now we use page size operation */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530336 int sectors = 0, count = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900337
338 /* Address translation */
339 switch (cmd) {
340 case ONENAND_CMD_UNLOCK:
341 case ONENAND_CMD_LOCK:
342 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900343 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Park79e90462007-09-10 17:13:49 +0900344 block = -1;
345 page = -1;
346 break;
347
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530348 case FLEXONENAND_CMD_PI_ACCESS:
349 /* addr contains die index */
350 block = addr * this->density_mask;
351 page = -1;
352 break;
353
Kyungmin Park79e90462007-09-10 17:13:49 +0900354 case ONENAND_CMD_ERASE:
355 case ONENAND_CMD_BUFFERRAM:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530356 block = onenand_block(this, addr);
Kyungmin Park79e90462007-09-10 17:13:49 +0900357 page = -1;
358 break;
359
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530360 case FLEXONENAND_CMD_READ_PI:
361 cmd = ONENAND_CMD_READ;
362 block = addr * this->density_mask;
363 page = 0;
364 break;
365
Kyungmin Park79e90462007-09-10 17:13:49 +0900366 default:
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530367 block = onenand_block(this, addr);
368 page = (int) (addr
369 - onenand_addr(this, block)) >> this->page_shift;
Kyungmin Park79e90462007-09-10 17:13:49 +0900370 page &= this->page_mask;
371 break;
372 }
373
374 /* NOTE: The setting order of the registers is very important! */
375 if (cmd == ONENAND_CMD_BUFFERRAM) {
376 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900377 value = onenand_bufferram_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900378 this->write_word(value,
379 this->base + ONENAND_REG_START_ADDRESS2);
380
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100381 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530382 ONENAND_SET_BUFFERRAM0(this);
383 else
384 /* Switch to the next data buffer */
385 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900386
387 return 0;
388 }
389
390 if (block != -1) {
391 /* Write 'DFS, FBA' of Flash */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900392 value = onenand_block_address(this, block);
Kyungmin Park79e90462007-09-10 17:13:49 +0900393 this->write_word(value,
394 this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900395
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530396 /* Select DataRAM for DDP */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900397 value = onenand_bufferram_address(this, block);
398 this->write_word(value,
399 this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Park79e90462007-09-10 17:13:49 +0900400 }
401
402 if (page != -1) {
403 int dataram;
404
405 switch (cmd) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530406 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Park79e90462007-09-10 17:13:49 +0900407 case ONENAND_CMD_READ:
408 case ONENAND_CMD_READOOB:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100409 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530410 dataram = ONENAND_SET_BUFFERRAM0(this);
411 else
412 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
413
Kyungmin Park79e90462007-09-10 17:13:49 +0900414 break;
415
416 default:
417 dataram = ONENAND_CURRENT_BUFFERRAM(this);
418 break;
419 }
420
421 /* Write 'FPA, FSA' of Flash */
422 value = onenand_page_address(page, sectors);
423 this->write_word(value,
424 this->base + ONENAND_REG_START_ADDRESS8);
425
426 /* Write 'BSA, BSC' of DataRAM */
427 value = onenand_buffer_address(dataram, sectors, count);
428 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Park79e90462007-09-10 17:13:49 +0900429 }
430
431 /* Interrupt clear */
432 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
433 /* Write command */
434 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
435
436 return 0;
437}
438
439/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530440 * onenand_read_ecc - return ecc status
441 * @param this onenand chip structure
442 */
443static int onenand_read_ecc(struct onenand_chip *this)
444{
445 int ecc, i;
446
447 if (!FLEXONENAND(this))
448 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
449
450 for (i = 0; i < 4; i++) {
451 ecc = this->read_word(this->base
452 + ((ONENAND_REG_ECC_STATUS + i) << 1));
453 if (likely(!ecc))
454 continue;
455 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
456 return ONENAND_ECC_2BIT_ALL;
457 }
458
459 return 0;
460}
461
462/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900463 * onenand_wait - [DEFAULT] wait until the command is done
464 * @param mtd MTD device structure
465 * @param state state to select the max. timeout value
466 *
467 * Wait for command done. This applies to all OneNAND command
468 * Read can take up to 30us, erase up to 2ms and program up to 350us
469 * according to general OneNAND specs
470 */
471static int onenand_wait(struct mtd_info *mtd, int state)
472{
473 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +0900474 unsigned int interrupt = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530475 unsigned int ctrl;
Kyungmin Park79e90462007-09-10 17:13:49 +0900476
Ladislav Michl243af2f2016-07-12 20:28:19 +0200477 /* Wait at most 20ms ... */
478 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
479 u32 time_start = get_timer(0);
480 do {
Stefan Roese80877fa2022-09-02 14:10:46 +0200481 schedule();
Ladislav Michl243af2f2016-07-12 20:28:19 +0200482 if (get_timer(time_start) > timeo)
483 return -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +0900484 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +0200485 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park79e90462007-09-10 17:13:49 +0900486
487 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
488
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530489 if (interrupt & ONENAND_INT_READ) {
490 int ecc = onenand_read_ecc(this);
491 if (ecc & ONENAND_ECC_2BIT_ALL) {
492 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
493 return -EBADMSG;
494 }
495 }
496
Kyungmin Park79e90462007-09-10 17:13:49 +0900497 if (ctrl & ONENAND_CTRL_ERROR) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900498 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
499 if (ctrl & ONENAND_CTRL_LOCK)
500 printk("onenand_wait: it's locked error = 0x%04x\n",
501 ctrl);
Kyungmin Park79e90462007-09-10 17:13:49 +0900502
Kyungmin Park79e90462007-09-10 17:13:49 +0900503 return -EIO;
504 }
505
Kyungmin Park79e90462007-09-10 17:13:49 +0900506 return 0;
507}
508
509/**
510 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
511 * @param mtd MTD data structure
512 * @param area BufferRAM area
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100513 * Return: offset given area
Kyungmin Park79e90462007-09-10 17:13:49 +0900514 *
515 * Return BufferRAM offset given area
516 */
517static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
518{
519 struct onenand_chip *this = mtd->priv;
520
521 if (ONENAND_CURRENT_BUFFERRAM(this)) {
522 if (area == ONENAND_DATARAM)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900523 return mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900524 if (area == ONENAND_SPARERAM)
525 return mtd->oobsize;
526 }
527
528 return 0;
529}
530
531/**
532 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
533 * @param mtd MTD data structure
534 * @param area BufferRAM area
535 * @param buffer the databuffer to put/get data
536 * @param offset offset to read from or write to
537 * @param count number of bytes to read/write
538 *
539 * Read the BufferRAM area
540 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900541static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900542 unsigned char *buffer, int offset,
543 size_t count)
544{
545 struct onenand_chip *this = mtd->priv;
546 void __iomem *bufferram;
547
548 bufferram = this->base + area;
549 bufferram += onenand_bufferram_offset(mtd, area);
550
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200551 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900552
553 return 0;
554}
555
556/**
557 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
558 * @param mtd MTD data structure
559 * @param area BufferRAM area
560 * @param buffer the databuffer to put/get data
561 * @param offset offset to read from or write to
562 * @param count number of bytes to read/write
563 *
564 * Read the BufferRAM area with Sync. Burst Mode
565 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900566static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900567 unsigned char *buffer, int offset,
568 size_t count)
569{
570 struct onenand_chip *this = mtd->priv;
571 void __iomem *bufferram;
572
573 bufferram = this->base + area;
574 bufferram += onenand_bufferram_offset(mtd, area);
575
576 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
577
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200578 memcpy_16(buffer, bufferram + offset, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900579
580 this->mmcontrol(mtd, 0);
581
582 return 0;
583}
584
585/**
586 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
587 * @param mtd MTD data structure
588 * @param area BufferRAM area
589 * @param buffer the databuffer to put/get data
590 * @param offset offset to read from or write to
591 * @param count number of bytes to read/write
592 *
593 * Write the BufferRAM area
594 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900595static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
Kyungmin Park79e90462007-09-10 17:13:49 +0900596 const unsigned char *buffer, int offset,
597 size_t count)
598{
599 struct onenand_chip *this = mtd->priv;
600 void __iomem *bufferram;
601
602 bufferram = this->base + area;
603 bufferram += onenand_bufferram_offset(mtd, area);
604
Wolfgang Denkd7024b62008-05-01 21:30:16 +0200605 memcpy_16(bufferram + offset, buffer, count);
Kyungmin Park79e90462007-09-10 17:13:49 +0900606
607 return 0;
608}
609
610/**
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100611 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
612 * @param mtd MTD data structure
613 * @param addr address to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100614 * Return: blockpage address
Stefan Roese5ed79ae2008-11-11 10:28:53 +0100615 *
616 * Get blockpage address at 2x program mode
617 */
618static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
619{
620 struct onenand_chip *this = mtd->priv;
621 int blockpage, block, page;
622
623 /* Calculate the even block number */
624 block = (int) (addr >> this->erase_shift) & ~1;
625 /* Is it the odd plane? */
626 if (addr & this->writesize)
627 block++;
628 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
629 blockpage = (block << 7) | page;
630
631 return blockpage;
632}
633
634/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900635 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
636 * @param mtd MTD data structure
637 * @param addr address to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100638 * Return: 1 if there are valid data, otherwise 0
Kyungmin Park79e90462007-09-10 17:13:49 +0900639 *
640 * Check bufferram if there is data we required
641 */
642static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
643{
644 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900645 int blockpage, found = 0;
646 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900647
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900648 if (ONENAND_IS_2PLANE(this))
649 blockpage = onenand_get_2x_blockpage(mtd, addr);
650 else
651 blockpage = (int) (addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900652
653 /* Is there valid data? */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900654 i = ONENAND_CURRENT_BUFFERRAM(this);
655 if (this->bufferram[i].blockpage == blockpage)
656 found = 1;
657 else {
658 /* Check another BufferRAM */
659 i = ONENAND_NEXT_BUFFERRAM(this);
660 if (this->bufferram[i].blockpage == blockpage) {
661 ONENAND_SET_NEXT_BUFFERRAM(this);
662 found = 1;
663 }
664 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900665
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900666 if (found && ONENAND_IS_DDP(this)) {
667 /* Select DataRAM for DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530668 int block = onenand_block(this, addr);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900669 int value = onenand_bufferram_address(this, block);
670 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
671 }
672
673 return found;
Kyungmin Park79e90462007-09-10 17:13:49 +0900674}
675
676/**
677 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
678 * @param mtd MTD data structure
679 * @param addr address to update
680 * @param valid valid flag
681 *
682 * Update BufferRAM information
683 */
684static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
685 int valid)
686{
687 struct onenand_chip *this = mtd->priv;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900688 int blockpage;
689 unsigned int i;
Kyungmin Park79e90462007-09-10 17:13:49 +0900690
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900691 if (ONENAND_IS_2PLANE(this))
692 blockpage = onenand_get_2x_blockpage(mtd, addr);
693 else
694 blockpage = (int)(addr >> this->page_shift);
Kyungmin Park79e90462007-09-10 17:13:49 +0900695
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900696 /* Invalidate another BufferRAM */
697 i = ONENAND_NEXT_BUFFERRAM(this);
698 if (this->bufferram[i].blockpage == blockpage)
699 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900700
701 /* Update BufferRAM */
702 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900703 if (valid)
704 this->bufferram[i].blockpage = blockpage;
705 else
706 this->bufferram[i].blockpage = -1;
Kyungmin Park79e90462007-09-10 17:13:49 +0900707
708 return 0;
709}
710
711/**
Kyungmin Park396b0c42008-08-13 09:11:02 +0900712 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
713 * @param mtd MTD data structure
714 * @param addr start address to invalidate
715 * @param len length to invalidate
716 *
717 * Invalidate BufferRAM information
718 */
719static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200720 unsigned int len)
Kyungmin Park396b0c42008-08-13 09:11:02 +0900721{
722 struct onenand_chip *this = mtd->priv;
723 int i;
724 loff_t end_addr = addr + len;
725
726 /* Invalidate BufferRAM */
727 for (i = 0; i < MAX_BUFFERRAM; i++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900728 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900729
730 if (buf_addr >= addr && buf_addr < end_addr)
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900731 this->bufferram[i].blockpage = -1;
Kyungmin Park396b0c42008-08-13 09:11:02 +0900732 }
733}
734
735/**
Kyungmin Park79e90462007-09-10 17:13:49 +0900736 * onenand_get_device - [GENERIC] Get chip for selected access
737 * @param mtd MTD device structure
738 * @param new_state the state which is requested
739 *
740 * Get the device and lock it for exclusive access
741 */
742static void onenand_get_device(struct mtd_info *mtd, int new_state)
743{
744 /* Do nothing */
745}
746
747/**
748 * onenand_release_device - [GENERIC] release chip
749 * @param mtd MTD device structure
750 *
751 * Deselect, release chip lock and wake up anyone waiting on the device
752 */
753static void onenand_release_device(struct mtd_info *mtd)
754{
755 /* Do nothing */
756}
757
758/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000759 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900760 * @param mtd MTD device structure
761 * @param buf destination address
762 * @param column oob offset to read from
763 * @param thislen oob length to read
764 */
765static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
766 int column, int thislen)
767{
768 struct onenand_chip *this = mtd->priv;
769 struct nand_oobfree *free;
770 int readcol = column;
771 int readend = column + thislen;
772 int lastgap = 0;
773 unsigned int i;
774 uint8_t *oob_buf = this->oob_buf;
775
776 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530777 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
778 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900779 if (readcol >= lastgap)
780 readcol += free->offset - lastgap;
781 if (readend >= lastgap)
782 readend += free->offset - lastgap;
783 lastgap = free->offset + free->length;
784 }
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900785 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900786 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +0530787 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
788 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900789 int free_end = free->offset + free->length;
790 if (free->offset < readend && free_end > readcol) {
791 int st = max_t(int,free->offset,readcol);
792 int ed = min_t(int,free_end,readend);
793 int n = ed - st;
794 memcpy(buf, oob_buf + st, n);
795 buf += n;
796 } else if (column == 0)
797 break;
798 }
799 return 0;
800}
801
802/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530803 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
804 * @param mtd MTD device structure
805 * @param addr address to recover
806 * @param status return value from onenand_wait
807 *
808 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
809 * lower page address and MSB page has higher page address in paired pages.
810 * If power off occurs during MSB page program, the paired LSB page data can
811 * become corrupt. LSB page recovery read is a way to read LSB page though page
812 * data are corrupted. When uncorrectable error occurs as a result of LSB page
813 * read after power up, issue LSB page recovery read.
814 */
815static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
816{
817 struct onenand_chip *this = mtd->priv;
818 int i;
819
820 /* Recovery is only for Flex-OneNAND */
821 if (!FLEXONENAND(this))
822 return status;
823
824 /* check if we failed due to uncorrectable error */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000825 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530826 return status;
827
828 /* check if address lies in MLC region */
829 i = flexonenand_region(mtd, addr);
830 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
831 return status;
832
833 printk("onenand_recover_lsb:"
834 "Attempting to recover from uncorrectable read\n");
835
836 /* Issue the LSB page recovery command */
837 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
838 return this->wait(mtd, FL_READING);
839}
840
841/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900842 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Park79e90462007-09-10 17:13:49 +0900843 * @param mtd MTD device structure
844 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900845 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +0900846 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900847 * OneNAND read main and/or out-of-band data
Kyungmin Park79e90462007-09-10 17:13:49 +0900848 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900849static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
850 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +0900851{
852 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900853 struct mtd_ecc_stats stats;
854 size_t len = ops->len;
855 size_t ooblen = ops->ooblen;
856 u_char *buf = ops->datbuf;
857 u_char *oobbuf = ops->oobbuf;
858 int read = 0, column, thislen;
859 int oobread = 0, oobcolumn, thisooblen, oobsize;
860 int ret = 0, boundary = 0;
861 int writesize = this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +0900862
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900863 pr_debug("onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
864 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900865
Sergey Lapin3a38a552013-01-14 03:46:50 +0000866 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900867 oobsize = this->ecclayout->oobavail;
868 else
869 oobsize = mtd->oobsize;
870
871 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +0900872
873 /* Do not allow reads past end of device */
874 if ((from + len) > mtd->size) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900875 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
876 ops->retlen = 0;
877 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900878 return -EINVAL;
879 }
880
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900881 stats = mtd->ecc_stats;
Kyungmin Park79e90462007-09-10 17:13:49 +0900882
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900883 /* Read-while-load method */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530884 /* Note: We can't use this feature in MLC */
Kyungmin Park79e90462007-09-10 17:13:49 +0900885
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900886 /* Do first load to bufferRAM */
887 if (read < len) {
Kyungmin Park79e90462007-09-10 17:13:49 +0900888 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900889 this->main_buf = buf;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900890 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +0900891 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530892 if (unlikely(ret))
893 ret = onenand_recover_lsb(mtd, from, ret);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900894 onenand_update_bufferram(mtd, from, !ret);
895 if (ret == -EBADMSG)
896 ret = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900897 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900898 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900899
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900900 thislen = min_t(int, writesize, len - read);
901 column = from & (writesize - 1);
902 if (column + thislen > writesize)
903 thislen = writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +0900904
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900905 while (!ret) {
906 /* If there is more to load then start next load */
907 from += thislen;
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100908 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900909 this->main_buf = buf + thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900910 this->command(mtd, ONENAND_CMD_READ, from, writesize);
911 /*
912 * Chip boundary handling in DDP
913 * Now we issued chip 1 read and pointed chip 1
914 * bufferam so we have to point chip 0 bufferam.
915 */
916 if (ONENAND_IS_DDP(this) &&
917 unlikely(from == (this->chipsize >> 1))) {
918 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
919 boundary = 1;
920 } else
921 boundary = 0;
922 ONENAND_SET_PREV_BUFFERRAM(this);
923 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900924
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900925 /* While load is going, read from last bufferRAM */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900926 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900927
928 /* Read oob area if needed */
929 if (oobbuf) {
930 thisooblen = oobsize - oobcolumn;
931 thisooblen = min_t(int, thisooblen, ooblen - oobread);
932
Sergey Lapin3a38a552013-01-14 03:46:50 +0000933 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900934 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
935 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +0900936 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900937 oobread += thisooblen;
938 oobbuf += thisooblen;
939 oobcolumn = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900940 }
941
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100942 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530943 this->command(mtd, ONENAND_CMD_READ, from, writesize);
944 ret = this->wait(mtd, FL_READING);
945 if (unlikely(ret))
946 ret = onenand_recover_lsb(mtd, from, ret);
947 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000948 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530949 ret = 0;
950 }
951
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900952 /* See if we are done */
953 read += thislen;
954 if (read == len)
955 break;
956 /* Set up for next read from bufferRAM */
957 if (unlikely(boundary))
958 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100959 if (!ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530960 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Park79e90462007-09-10 17:13:49 +0900961 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900962 thislen = min_t(int, writesize, len - read);
963 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900964
Lukasz Majewski2b4fa322011-11-09 10:30:06 +0100965 if (!ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530966 /* Now wait for load */
967 ret = this->wait(mtd, FL_READING);
968 onenand_update_bufferram(mtd, from, !ret);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000969 if (mtd_is_eccerr(ret))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +0530970 ret = 0;
971 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900972 }
Kyungmin Park79e90462007-09-10 17:13:49 +0900973
974 /*
975 * Return success, if no ECC failures, else -EBADMSG
976 * fs driver will take care of that, because
977 * retlen == desired len and result == -EBADMSG
978 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900979 ops->retlen = read;
980 ops->oobretlen = oobread;
981
982 if (ret)
983 return ret;
984
985 if (mtd->ecc_stats.failed - stats.failed)
986 return -EBADMSG;
987
Paul Burton700a76c2013-09-04 15:16:56 +0100988 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
989 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Park79e90462007-09-10 17:13:49 +0900990}
991
992/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900993 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
994 * @param mtd MTD device structure
995 * @param from offset to read from
996 * @param ops oob operation description structure
997 *
998 * OneNAND read out-of-band data from the spare area
999 */
1000static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1001 struct mtd_oob_ops *ops)
1002{
1003 struct onenand_chip *this = mtd->priv;
1004 struct mtd_ecc_stats stats;
1005 int read = 0, thislen, column, oobsize;
1006 size_t len = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001007 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001008 u_char *buf = ops->oobbuf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301009 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001010
1011 from += ops->ooboffs;
1012
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001013 pr_debug("onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
1014 (unsigned int) from, (int) len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001015
1016 /* Initialize return length value */
1017 ops->oobretlen = 0;
1018
Sergey Lapin3a38a552013-01-14 03:46:50 +00001019 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001020 oobsize = this->ecclayout->oobavail;
1021 else
1022 oobsize = mtd->oobsize;
1023
1024 column = from & (mtd->oobsize - 1);
1025
1026 if (unlikely(column >= oobsize)) {
1027 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1028 return -EINVAL;
1029 }
1030
1031 /* Do not allow reads past end of device */
1032 if (unlikely(from >= mtd->size ||
1033 column + len > ((mtd->size >> this->page_shift) -
1034 (from >> this->page_shift)) * oobsize)) {
1035 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1036 return -EINVAL;
1037 }
1038
1039 stats = mtd->ecc_stats;
1040
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001041 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1042 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301043
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001044 while (read < len) {
1045 thislen = oobsize - column;
1046 thislen = min_t(int, thislen, len);
1047
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001048 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301049 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001050
1051 onenand_update_bufferram(mtd, from, 0);
1052
1053 ret = this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301054 if (unlikely(ret))
1055 ret = onenand_recover_lsb(mtd, from, ret);
1056
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001057 if (ret && ret != -EBADMSG) {
1058 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1059 break;
1060 }
1061
Sergey Lapin3a38a552013-01-14 03:46:50 +00001062 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001063 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1064 else
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001065 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001066
1067 read += thislen;
1068
1069 if (read == len)
1070 break;
1071
1072 buf += thislen;
1073
1074 /* Read more? */
1075 if (read < len) {
1076 /* Page size */
1077 from += mtd->writesize;
1078 column = 0;
1079 }
1080 }
1081
1082 ops->oobretlen = read;
1083
1084 if (ret)
1085 return ret;
1086
1087 if (mtd->ecc_stats.failed - stats.failed)
1088 return -EBADMSG;
1089
1090 return 0;
1091}
1092
1093/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001094 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1095 * @param mtd MTD device structure
1096 * @param from offset to read from
1097 * @param len number of bytes to read
1098 * @param retlen pointer to variable to store the number of read bytes
1099 * @param buf the databuffer to put data
1100 *
1101 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1102*/
1103int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1104 size_t * retlen, u_char * buf)
1105{
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001106 struct mtd_oob_ops ops = {
1107 .len = len,
1108 .ooblen = 0,
1109 .datbuf = buf,
1110 .oobbuf = NULL,
1111 };
1112 int ret;
1113
1114 onenand_get_device(mtd, FL_READING);
1115 ret = onenand_read_ops_nolock(mtd, from, &ops);
1116 onenand_release_device(mtd);
1117
1118 *retlen = ops.retlen;
1119 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001120}
1121
1122/**
1123 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1124 * @param mtd MTD device structure
1125 * @param from offset to read from
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001126 * @param ops oob operations description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001127 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001128 * OneNAND main and/or out-of-band
1129 */
1130int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1131 struct mtd_oob_ops *ops)
1132{
1133 int ret;
1134
1135 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001136 case MTD_OPS_PLACE_OOB:
1137 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001138 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001139 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001140 /* Not implemented yet */
1141 default:
1142 return -EINVAL;
1143 }
1144
1145 onenand_get_device(mtd, FL_READING);
1146 if (ops->datbuf)
1147 ret = onenand_read_ops_nolock(mtd, from, ops);
1148 else
1149 ret = onenand_read_oob_nolock(mtd, from, ops);
1150 onenand_release_device(mtd);
1151
1152 return ret;
1153}
1154
1155/**
1156 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1157 * @param mtd MTD device structure
1158 * @param state state to select the max. timeout value
1159 *
1160 * Wait for command done.
Kyungmin Park79e90462007-09-10 17:13:49 +09001161 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001162static int onenand_bbt_wait(struct mtd_info *mtd, int state)
Kyungmin Park79e90462007-09-10 17:13:49 +09001163{
1164 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001165 unsigned int interrupt;
1166 unsigned int ctrl;
1167
Ladislav Michl243af2f2016-07-12 20:28:19 +02001168 /* Wait at most 20ms ... */
1169 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
1170 u32 time_start = get_timer(0);
1171 do {
Stefan Roese80877fa2022-09-02 14:10:46 +02001172 schedule();
Ladislav Michl243af2f2016-07-12 20:28:19 +02001173 if (get_timer(time_start) > timeo)
1174 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001175 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
Ladislav Michl243af2f2016-07-12 20:28:19 +02001176 } while ((interrupt & ONENAND_INT_MASTER) == 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001177
1178 /* To get correct interrupt status in timeout case */
1179 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1180 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1181
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001182 if (interrupt & ONENAND_INT_READ) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301183 int ecc = onenand_read_ecc(this);
1184 if (ecc & ONENAND_ECC_2BIT_ALL) {
1185 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1186 ", controller = 0x%04x\n", ecc, ctrl);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001187 return ONENAND_BBT_READ_ERROR;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301188 }
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001189 } else {
1190 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1191 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1192 return ONENAND_BBT_READ_FATAL_ERROR;
1193 }
1194
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001195 /* Initial bad block case: 0x2400 or 0x0400 */
1196 if (ctrl & ONENAND_CTRL_ERROR) {
1197 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1198 return ONENAND_BBT_READ_ERROR;
1199 }
1200
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001201 return 0;
1202}
1203
1204/**
1205 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1206 * @param mtd MTD device structure
1207 * @param from offset to read from
1208 * @param ops oob operation description structure
1209 *
1210 * OneNAND read out-of-band data from the spare area for bbt scan
1211 */
1212int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1213 struct mtd_oob_ops *ops)
1214{
1215 struct onenand_chip *this = mtd->priv;
Kyungmin Park79e90462007-09-10 17:13:49 +09001216 int read = 0, thislen, column;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301217 int ret = 0, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001218 size_t len = ops->ooblen;
1219 u_char *buf = ops->oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001220
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001221 pr_debug("onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
1222 (unsigned int) from, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001223
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001224 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1225 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301226
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001227 /* Initialize return value */
1228 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001229
1230 /* Do not allow reads past end of device */
1231 if (unlikely((from + len) > mtd->size)) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001232 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1233 return ONENAND_BBT_READ_FATAL_ERROR;
Kyungmin Park79e90462007-09-10 17:13:49 +09001234 }
1235
1236 /* Grab the lock and see if the device is available */
1237 onenand_get_device(mtd, FL_READING);
1238
1239 column = from & (mtd->oobsize - 1);
1240
1241 while (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001242
Kyungmin Park79e90462007-09-10 17:13:49 +09001243 thislen = mtd->oobsize - column;
1244 thislen = min_t(int, thislen, len);
1245
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001246 this->spare_buf = buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301247 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001248
1249 onenand_update_bufferram(mtd, from, 0);
1250
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001251 ret = this->bbt_wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301252 if (unlikely(ret))
1253 ret = onenand_recover_lsb(mtd, from, ret);
1254
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001255 if (ret)
1256 break;
Kyungmin Park79e90462007-09-10 17:13:49 +09001257
Kyungmin Parkfee18b92009-07-21 11:58:04 +09001258 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001259 read += thislen;
1260 if (read == len)
1261 break;
1262
Kyungmin Park79e90462007-09-10 17:13:49 +09001263 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001264
Kyungmin Park79e90462007-09-10 17:13:49 +09001265 /* Read more? */
1266 if (read < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001267 /* Update Page size */
1268 from += this->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001269 column = 0;
1270 }
1271 }
1272
1273 /* Deselect and wake up anyone waiting on the device */
1274 onenand_release_device(mtd);
1275
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001276 ops->oobretlen = read;
Kyungmin Park79e90462007-09-10 17:13:49 +09001277 return ret;
1278}
1279
1280#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1281/**
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001282 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1283 * @param mtd MTD device structure
1284 * @param buf the databuffer to verify
1285 * @param to offset to read from
Kyungmin Park79e90462007-09-10 17:13:49 +09001286 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001287static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park79e90462007-09-10 17:13:49 +09001288{
1289 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001290 u_char *oob_buf = this->oob_buf;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301291 int status, i, readcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001292
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001293 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1294 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301295
1296 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001297 onenand_update_bufferram(mtd, to, 0);
1298 status = this->wait(mtd, FL_READING);
1299 if (status)
1300 return status;
1301
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001302 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001303 for (i = 0; i < mtd->oobsize; i++)
1304 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1305 return -EBADMSG;
1306
1307 return 0;
1308}
1309
1310/**
1311 * onenand_verify - [GENERIC] verify the chip contents after a write
1312 * @param mtd MTD device structure
1313 * @param buf the databuffer to verify
1314 * @param addr offset to read from
1315 * @param len number of bytes to read and compare
1316 */
1317static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1318{
1319 struct onenand_chip *this = mtd->priv;
1320 void __iomem *dataram;
Kyungmin Park79e90462007-09-10 17:13:49 +09001321 int ret = 0;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001322 int thislen, column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001323
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001324 while (len != 0) {
1325 thislen = min_t(int, this->writesize, len);
1326 column = addr & (this->writesize - 1);
1327 if (column + thislen > this->writesize)
1328 thislen = this->writesize - column;
Kyungmin Park79e90462007-09-10 17:13:49 +09001329
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001330 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001331
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001332 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001333
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001334 ret = this->wait(mtd, FL_READING);
1335 if (ret)
1336 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001337
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001338 onenand_update_bufferram(mtd, addr, 1);
1339
1340 dataram = this->base + ONENAND_DATARAM;
1341 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1342
1343 if (memcmp(buf, dataram + column, thislen))
1344 return -EBADMSG;
1345
1346 len -= thislen;
1347 buf += thislen;
1348 addr += thislen;
1349 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001350
1351 return 0;
1352}
1353#else
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001354#define onenand_verify(...) (0)
1355#define onenand_verify_oob(...) (0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001356#endif
1357
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01001358#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Park79e90462007-09-10 17:13:49 +09001359
1360/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001361 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001362 * @param mtd MTD device structure
1363 * @param oob_buf oob buffer
1364 * @param buf source address
1365 * @param column oob offset to write to
1366 * @param thislen oob length to write
1367 */
1368static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1369 const u_char *buf, int column, int thislen)
1370{
1371 struct onenand_chip *this = mtd->priv;
1372 struct nand_oobfree *free;
1373 int writecol = column;
1374 int writeend = column + thislen;
1375 int lastgap = 0;
1376 unsigned int i;
1377
1378 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301379 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1380 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001381 if (writecol >= lastgap)
1382 writecol += free->offset - lastgap;
1383 if (writeend >= lastgap)
1384 writeend += free->offset - lastgap;
1385 lastgap = free->offset + free->length;
1386 }
1387 free = this->ecclayout->oobfree;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05301388 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1389 i++, free++) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001390 int free_end = free->offset + free->length;
1391 if (free->offset < writeend && free_end > writecol) {
1392 int st = max_t(int,free->offset,writecol);
1393 int ed = min_t(int,free_end,writeend);
1394 int n = ed - st;
1395 memcpy(oob_buf + st, buf, n);
1396 buf += n;
1397 } else if (column == 0)
1398 break;
1399 }
1400 return 0;
1401}
1402
1403/**
1404 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1405 * @param mtd MTD device structure
1406 * @param to offset to write to
1407 * @param ops oob operation description structure
Kyungmin Park79e90462007-09-10 17:13:49 +09001408 *
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001409 * Write main and/or oob with ECC
Kyungmin Park79e90462007-09-10 17:13:49 +09001410 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001411static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1412 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001413{
1414 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001415 int written = 0, column, thislen, subpage;
1416 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1417 size_t len = ops->len;
1418 size_t ooblen = ops->ooblen;
1419 const u_char *buf = ops->datbuf;
1420 const u_char *oob = ops->oobbuf;
1421 u_char *oobbuf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001422 int ret = 0;
1423
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001424 pr_debug("onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
1425 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001426
1427 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001428 ops->retlen = 0;
1429 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001430
Kyungmin Park79e90462007-09-10 17:13:49 +09001431 /* Reject writes, which are not page aligned */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001432 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1433 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001434 return -EINVAL;
1435 }
1436
Sergey Lapin3a38a552013-01-14 03:46:50 +00001437 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001438 oobsize = this->ecclayout->oobavail;
1439 else
1440 oobsize = mtd->oobsize;
1441
1442 oobcolumn = to & (mtd->oobsize - 1);
1443
1444 column = to & (mtd->writesize - 1);
Kyungmin Park79e90462007-09-10 17:13:49 +09001445
1446 /* Loop until all data write */
1447 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001448 u_char *wbuf = (u_char *) buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001449
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001450 thislen = min_t(int, mtd->writesize - column, len - written);
1451 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Park79e90462007-09-10 17:13:49 +09001452
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001453 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001454
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001455 /* Partial page write */
1456 subpage = thislen < mtd->writesize;
1457 if (subpage) {
1458 memset(this->page_buf, 0xff, mtd->writesize);
1459 memcpy(this->page_buf + column, buf, thislen);
1460 wbuf = this->page_buf;
1461 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001462
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001463 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001464
1465 if (oob) {
1466 oobbuf = this->oob_buf;
1467
1468 /* We send data to spare ram with oobsize
1469 * * to prevent byte access */
1470 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001471 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001472 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1473 else
1474 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1475
1476 oobwritten += thisooblen;
1477 oob += thisooblen;
1478 oobcolumn = 0;
1479 } else
1480 oobbuf = (u_char *) ffchars;
1481
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001482 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001483
1484 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001485
1486 ret = this->wait(mtd, FL_WRITING);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001487
1488 /* In partial page write we don't update bufferram */
1489 onenand_update_bufferram(mtd, to, !ret && !subpage);
1490 if (ONENAND_IS_2PLANE(this)) {
1491 ONENAND_SET_BUFFERRAM1(this);
1492 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1493 }
1494
Kyungmin Park79e90462007-09-10 17:13:49 +09001495 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001496 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001497 break;
1498 }
1499
Kyungmin Park79e90462007-09-10 17:13:49 +09001500 /* Only check verify write turn on */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001501 ret = onenand_verify(mtd, buf, to, thislen);
Kyungmin Park79e90462007-09-10 17:13:49 +09001502 if (ret) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001503 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001504 break;
1505 }
1506
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001507 written += thislen;
1508
Kyungmin Park79e90462007-09-10 17:13:49 +09001509 if (written == len)
1510 break;
1511
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001512 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001513 to += thislen;
1514 buf += thislen;
1515 }
1516
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001517 ops->retlen = written;
Kyungmin Park79e90462007-09-10 17:13:49 +09001518
1519 return ret;
1520}
1521
1522/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001523 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001524 * @param mtd MTD device structure
1525 * @param to offset to write to
1526 * @param len number of bytes to write
1527 * @param retlen pointer to variable to store the number of written bytes
1528 * @param buf the data to write
1529 * @param mode operation mode
Kyungmin Park79e90462007-09-10 17:13:49 +09001530 *
1531 * OneNAND write out-of-band
1532 */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001533static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1534 struct mtd_oob_ops *ops)
Kyungmin Park79e90462007-09-10 17:13:49 +09001535{
1536 struct onenand_chip *this = mtd->priv;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001537 int column, ret = 0, oobsize;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301538 int written = 0, oobcmd;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001539 u_char *oobbuf;
1540 size_t len = ops->ooblen;
1541 const u_char *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001542 unsigned int mode = ops->mode;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001543
1544 to += ops->ooboffs;
Kyungmin Park79e90462007-09-10 17:13:49 +09001545
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001546 pr_debug("onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
1547 (unsigned int) to, (int) len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001548
1549 /* Initialize retlen, in case of early exit */
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001550 ops->oobretlen = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001551
Sergey Lapin3a38a552013-01-14 03:46:50 +00001552 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001553 oobsize = this->ecclayout->oobavail;
1554 else
1555 oobsize = mtd->oobsize;
1556
1557 column = to & (mtd->oobsize - 1);
1558
1559 if (unlikely(column >= oobsize)) {
1560 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001561 return -EINVAL;
1562 }
1563
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001564 /* For compatibility with NAND: Do not allow write past end of page */
1565 if (unlikely(column + len > oobsize)) {
1566 printk(KERN_ERR "onenand_write_oob_nolock: "
1567 "Attempt to write past end of page\n");
1568 return -EINVAL;
1569 }
1570
1571 /* Do not allow reads past end of device */
1572 if (unlikely(to >= mtd->size ||
1573 column + len > ((mtd->size >> this->page_shift) -
1574 (to >> this->page_shift)) * oobsize)) {
1575 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1576 return -EINVAL;
1577 }
1578
1579 oobbuf = this->oob_buf;
Kyungmin Park79e90462007-09-10 17:13:49 +09001580
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001581 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1582 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301583
Kyungmin Park79e90462007-09-10 17:13:49 +09001584 /* Loop until all data write */
1585 while (written < len) {
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001586 int thislen = min_t(int, oobsize, len - written);
Kyungmin Park79e90462007-09-10 17:13:49 +09001587
1588 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1589
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001590 /* We send data to spare ram with oobsize
1591 * to prevent byte access */
1592 memset(oobbuf, 0xff, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001593 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001594 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1595 else
1596 memcpy(oobbuf + column, buf, thislen);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001597 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001598
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01001599 if (ONENAND_IS_4KB_PAGE(this)) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301600 /* Set main area of DataRAM to 0xff*/
1601 memset(this->page_buf, 0xff, mtd->writesize);
1602 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1603 this->page_buf, 0, mtd->writesize);
1604 }
1605
1606 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Park79e90462007-09-10 17:13:49 +09001607
1608 onenand_update_bufferram(mtd, to, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001609 if (ONENAND_IS_2PLANE(this)) {
1610 ONENAND_SET_BUFFERRAM1(this);
1611 onenand_update_bufferram(mtd, to + this->writesize, 0);
1612 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001613
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001614 ret = this->wait(mtd, FL_WRITING);
1615 if (ret) {
1616 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
Kyungmin Park79e90462007-09-10 17:13:49 +09001617 break;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001618 }
1619
1620 ret = onenand_verify_oob(mtd, oobbuf, to);
1621 if (ret) {
1622 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1623 break;
1624 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001625
1626 written += thislen;
1627 if (written == len)
1628 break;
1629
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001630 to += mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09001631 buf += thislen;
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001632 column = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001633 }
1634
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001635 ops->oobretlen = written;
1636
1637 return ret;
1638}
1639
1640/**
1641 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1642 * @param mtd MTD device structure
1643 * @param to offset to write to
1644 * @param len number of bytes to write
1645 * @param retlen pointer to variable to store the number of written bytes
1646 * @param buf the data to write
1647 *
1648 * Write with ECC
1649 */
1650int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1651 size_t * retlen, const u_char * buf)
1652{
1653 struct mtd_oob_ops ops = {
1654 .len = len,
1655 .ooblen = 0,
1656 .datbuf = (u_char *) buf,
1657 .oobbuf = NULL,
1658 };
1659 int ret;
1660
1661 onenand_get_device(mtd, FL_WRITING);
1662 ret = onenand_write_ops_nolock(mtd, to, &ops);
Kyungmin Park79e90462007-09-10 17:13:49 +09001663 onenand_release_device(mtd);
1664
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001665 *retlen = ops.retlen;
1666 return ret;
1667}
Kyungmin Park79e90462007-09-10 17:13:49 +09001668
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001669/**
1670 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1671 * @param mtd MTD device structure
1672 * @param to offset to write to
1673 * @param ops oob operation description structure
1674 *
1675 * OneNAND write main and/or out-of-band
1676 */
1677int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1678 struct mtd_oob_ops *ops)
1679{
1680 int ret;
1681
1682 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001683 case MTD_OPS_PLACE_OOB:
1684 case MTD_OPS_AUTO_OOB:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001685 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001686 case MTD_OPS_RAW:
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09001687 /* Not implemented yet */
1688 default:
1689 return -EINVAL;
1690 }
1691
1692 onenand_get_device(mtd, FL_WRITING);
1693 if (ops->datbuf)
1694 ret = onenand_write_ops_nolock(mtd, to, ops);
1695 else
1696 ret = onenand_write_oob_nolock(mtd, to, ops);
1697 onenand_release_device(mtd);
1698
1699 return ret;
1700
Kyungmin Park79e90462007-09-10 17:13:49 +09001701}
1702
Kyungmin Park396b0c42008-08-13 09:11:02 +09001703/**
1704 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1705 * @param mtd MTD device structure
1706 * @param ofs offset from device start
1707 * @param allowbbt 1, if its allowed to access the bbt area
1708 *
1709 * Check, if the block is bad, Either by reading the bad block table or
1710 * calling of the scan function.
1711 */
1712static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1713{
1714 struct onenand_chip *this = mtd->priv;
1715 struct bbm_info *bbm = this->bbm;
1716
1717 /* Return info from the table */
1718 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1719}
1720
Kyungmin Park79e90462007-09-10 17:13:49 +09001721/**
1722 * onenand_erase - [MTD Interface] erase block(s)
1723 * @param mtd MTD device structure
1724 * @param instr erase instruction
1725 *
1726 * Erase one ore more blocks
1727 */
1728int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1729{
1730 struct onenand_chip *this = mtd->priv;
1731 unsigned int block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301732 loff_t addr = instr->addr;
1733 unsigned int len = instr->len;
1734 int ret = 0, i;
1735 struct mtd_erase_region_info *region = NULL;
1736 unsigned int region_end = 0;
Kyungmin Park79e90462007-09-10 17:13:49 +09001737
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001738 pr_debug("onenand_erase: start = 0x%08x, len = %i\n",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301739 (unsigned int) addr, len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001740
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301741 if (FLEXONENAND(this)) {
1742 /* Find the eraseregion of this address */
1743 i = flexonenand_region(mtd, addr);
1744 region = &mtd->eraseregions[i];
1745
1746 block_size = region->erasesize;
1747 region_end = region->offset
1748 + region->erasesize * region->numblocks;
1749
1750 /* Start address within region must align on block boundary.
1751 * Erase region's start offset is always block start address.
1752 */
1753 if (unlikely((addr - region->offset) & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001754 pr_debug("onenand_erase:" " Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301755 return -EINVAL;
1756 }
1757 } else {
1758 block_size = 1 << this->erase_shift;
1759
1760 /* Start address must align on block boundary */
1761 if (unlikely(addr & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001762 pr_debug("onenand_erase:" "Unaligned address\n");
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301763 return -EINVAL;
1764 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001765 }
1766
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301767 /* Length must align on block boundary */
1768 if (unlikely(len & (block_size - 1))) {
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001769 pr_debug("onenand_erase: Length not block aligned\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001770 return -EINVAL;
1771 }
1772
Kyungmin Park79e90462007-09-10 17:13:49 +09001773 /* Grab the lock and see if the device is available */
1774 onenand_get_device(mtd, FL_ERASING);
1775
1776 /* Loop throught the pages */
Kyungmin Park79e90462007-09-10 17:13:49 +09001777 instr->state = MTD_ERASING;
1778
1779 while (len) {
1780
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001781 /* Check if we have a bad block, we do not erase bad blocks */
1782 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1783 printk(KERN_WARNING "onenand_erase: attempt to erase"
1784 " a bad block at addr 0x%08x\n",
1785 (unsigned int) addr);
1786 instr->state = MTD_ERASE_FAILED;
1787 goto erase_exit;
1788 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001789
1790 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1791
Kyungmin Park396b0c42008-08-13 09:11:02 +09001792 onenand_invalidate_bufferram(mtd, addr, block_size);
1793
Kyungmin Park79e90462007-09-10 17:13:49 +09001794 ret = this->wait(mtd, FL_ERASING);
1795 /* Check, if it is write protected */
1796 if (ret) {
1797 if (ret == -EPERM)
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001798 pr_debug("onenand_erase: "
1799 "Device is write protected!!!\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001800 else
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001801 pr_debug("onenand_erase: "
1802 "Failed erase, block %d\n",
1803 onenand_block(this, addr));
Kyungmin Park79e90462007-09-10 17:13:49 +09001804 instr->state = MTD_ERASE_FAILED;
1805 instr->fail_addr = addr;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001806
Kyungmin Park79e90462007-09-10 17:13:49 +09001807 goto erase_exit;
1808 }
1809
1810 len -= block_size;
1811 addr += block_size;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301812
1813 if (addr == region_end) {
1814 if (!len)
1815 break;
1816 region++;
1817
1818 block_size = region->erasesize;
1819 region_end = region->offset
1820 + region->erasesize * region->numblocks;
1821
1822 if (len & (block_size - 1)) {
1823 /* This has been checked at MTD
1824 * partitioning level. */
1825 printk("onenand_erase: Unaligned address\n");
1826 goto erase_exit;
1827 }
1828 }
Kyungmin Park79e90462007-09-10 17:13:49 +09001829 }
1830
1831 instr->state = MTD_ERASE_DONE;
1832
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001833erase_exit:
Kyungmin Park79e90462007-09-10 17:13:49 +09001834
1835 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Kyungmin Park79e90462007-09-10 17:13:49 +09001836
1837 /* Deselect and wake up anyone waiting on the device */
1838 onenand_release_device(mtd);
1839
1840 return ret;
1841}
1842
1843/**
1844 * onenand_sync - [MTD Interface] sync
1845 * @param mtd MTD device structure
1846 *
1847 * Sync is actually a wait for chip ready function
1848 */
1849void onenand_sync(struct mtd_info *mtd)
1850{
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +09001851 pr_debug("onenand_sync: called\n");
Kyungmin Park79e90462007-09-10 17:13:49 +09001852
1853 /* Grab the lock and see if the device is available */
1854 onenand_get_device(mtd, FL_SYNCING);
1855
1856 /* Release it and go back */
1857 onenand_release_device(mtd);
1858}
1859
1860/**
1861 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1862 * @param mtd MTD device structure
1863 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001864 *
1865 * Check whether the block is bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001866 */
1867int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1868{
Kyungmin Park396b0c42008-08-13 09:11:02 +09001869 int ret;
1870
1871 /* Check for invalid offset */
1872 if (ofs > mtd->size)
1873 return -EINVAL;
1874
1875 onenand_get_device(mtd, FL_READING);
1876 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1877 onenand_release_device(mtd);
1878 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001879}
1880
1881/**
Kyungmin Park87b49502008-11-13 15:14:33 +09001882 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1883 * @param mtd MTD device structure
1884 * @param ofs offset from device start
1885 *
1886 * This is the default implementation, which can be overridden by
1887 * a hardware specific driver.
1888 */
1889static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1890{
1891 struct onenand_chip *this = mtd->priv;
1892 struct bbm_info *bbm = this->bbm;
1893 u_char buf[2] = {0, 0};
1894 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001895 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park87b49502008-11-13 15:14:33 +09001896 .ooblen = 2,
1897 .oobbuf = buf,
1898 .ooboffs = 0,
1899 };
1900 int block;
1901
1902 /* Get block number */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301903 block = onenand_block(this, ofs);
Kyungmin Park87b49502008-11-13 15:14:33 +09001904 if (bbm->bbt)
1905 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1906
1907 /* We write two bytes, so we dont have to mess with 16 bit access */
1908 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1909 return onenand_write_oob_nolock(mtd, ofs, &ops);
1910}
1911
1912/**
Kyungmin Park79e90462007-09-10 17:13:49 +09001913 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1914 * @param mtd MTD device structure
1915 * @param ofs offset relative to mtd start
Kyungmin Park396b0c42008-08-13 09:11:02 +09001916 *
1917 * Mark the block as bad
Kyungmin Park79e90462007-09-10 17:13:49 +09001918 */
1919int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1920{
Ladislav Michl7868fba2017-06-20 17:17:45 +02001921 struct onenand_chip *this = mtd->priv;
Kyungmin Park396b0c42008-08-13 09:11:02 +09001922 int ret;
1923
1924 ret = onenand_block_isbad(mtd, ofs);
1925 if (ret) {
1926 /* If it was bad already, return success and do nothing */
1927 if (ret > 0)
1928 return 0;
1929 return ret;
1930 }
1931
Ladislav Michl7868fba2017-06-20 17:17:45 +02001932 onenand_get_device(mtd, FL_WRITING);
1933 ret = this->block_markbad(mtd, ofs);
1934 onenand_release_device(mtd);
1935
Kyungmin Park396b0c42008-08-13 09:11:02 +09001936 return ret;
Kyungmin Park79e90462007-09-10 17:13:49 +09001937}
1938
1939/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001940 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1941 * @param mtd MTD device structure
1942 * @param ofs offset relative to mtd start
1943 * @param len number of bytes to lock or unlock
1944 * @param cmd lock or unlock command
Kyungmin Park79e90462007-09-10 17:13:49 +09001945 *
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001946 * Lock or unlock one or more blocks
Kyungmin Park79e90462007-09-10 17:13:49 +09001947 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001948static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Park79e90462007-09-10 17:13:49 +09001949{
1950 struct onenand_chip *this = mtd->priv;
1951 int start, end, block, value, status;
1952
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301953 start = onenand_block(this, ofs);
1954 end = onenand_block(this, ofs + len);
Kyungmin Park79e90462007-09-10 17:13:49 +09001955
1956 /* Continuous lock scheme */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001957 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Park79e90462007-09-10 17:13:49 +09001958 /* Set start block address */
1959 this->write_word(start,
1960 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1961 /* Set end block address */
1962 this->write_word(end - 1,
1963 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1964 /* Write unlock command */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001965 this->command(mtd, cmd, 0, 0);
Kyungmin Park79e90462007-09-10 17:13:49 +09001966
1967 /* There's no return value */
1968 this->wait(mtd, FL_UNLOCKING);
1969
1970 /* Sanity check */
1971 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1972 & ONENAND_CTRL_ONGO)
1973 continue;
1974
1975 /* Check lock status */
1976 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1977 if (!(status & ONENAND_WP_US))
1978 printk(KERN_ERR "wp status = 0x%x\n", status);
1979
1980 return 0;
1981 }
1982
1983 /* Block lock scheme */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05301984 for (block = start; block < end; block++) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09001985 /* Set block address */
1986 value = onenand_block_address(this, block);
1987 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1988 /* Select DataRAM for DDP */
1989 value = onenand_bufferram_address(this, block);
1990 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1991
Kyungmin Park79e90462007-09-10 17:13:49 +09001992 /* Set start block address */
1993 this->write_word(block,
1994 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1995 /* Write unlock command */
1996 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1997
1998 /* There's no return value */
1999 this->wait(mtd, FL_UNLOCKING);
2000
2001 /* Sanity check */
2002 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2003 & ONENAND_CTRL_ONGO)
2004 continue;
2005
Kyungmin Park79e90462007-09-10 17:13:49 +09002006 /* Check lock status */
2007 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2008 if (!(status & ONENAND_WP_US))
2009 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2010 block, status);
2011 }
2012
2013 return 0;
2014}
2015
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002016#ifdef ONENAND_LINUX
Kyungmin Park79e90462007-09-10 17:13:49 +09002017/**
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002018 * onenand_lock - [MTD Interface] Lock block(s)
2019 * @param mtd MTD device structure
2020 * @param ofs offset relative to mtd start
2021 * @param len number of bytes to unlock
2022 *
2023 * Lock one or more blocks
2024 */
2025static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2026{
2027 int ret;
2028
2029 onenand_get_device(mtd, FL_LOCKING);
2030 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2031 onenand_release_device(mtd);
2032 return ret;
2033}
2034
2035/**
2036 * onenand_unlock - [MTD Interface] Unlock block(s)
2037 * @param mtd MTD device structure
2038 * @param ofs offset relative to mtd start
2039 * @param len number of bytes to unlock
2040 *
2041 * Unlock one or more blocks
2042 */
2043static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2044{
2045 int ret;
2046
2047 onenand_get_device(mtd, FL_LOCKING);
2048 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2049 onenand_release_device(mtd);
2050 return ret;
2051}
Stefan Roese5ed79ae2008-11-11 10:28:53 +01002052#endif
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002053
2054/**
2055 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2056 * @param this onenand chip data structure
2057 *
2058 * Check lock status
2059 */
2060static int onenand_check_lock_status(struct onenand_chip *this)
2061{
2062 unsigned int value, block, status;
2063 unsigned int end;
2064
2065 end = this->chipsize >> this->erase_shift;
2066 for (block = 0; block < end; block++) {
2067 /* Set block address */
2068 value = onenand_block_address(this, block);
2069 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2070 /* Select DataRAM for DDP */
2071 value = onenand_bufferram_address(this, block);
2072 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2073 /* Set start block address */
2074 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2075
2076 /* Check lock status */
2077 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2078 if (!(status & ONENAND_WP_US)) {
2079 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2080 return 0;
2081 }
2082 }
2083
2084 return 1;
2085}
2086
2087/**
2088 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2089 * @param mtd MTD device structure
2090 *
2091 * Unlock all blocks
2092 */
2093static void onenand_unlock_all(struct mtd_info *mtd)
2094{
2095 struct onenand_chip *this = mtd->priv;
2096 loff_t ofs = 0;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302097 size_t len = mtd->size;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002098
2099 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2100 /* Set start block address */
2101 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2102 /* Write unlock command */
2103 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2104
2105 /* There's no return value */
2106 this->wait(mtd, FL_LOCKING);
2107
2108 /* Sanity check */
2109 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2110 & ONENAND_CTRL_ONGO)
2111 continue;
2112
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002113 /* Check lock status */
2114 if (onenand_check_lock_status(this))
2115 return;
2116
2117 /* Workaround for all block unlock in DDP */
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302118 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002119 /* All blocks on another chip */
2120 ofs = this->chipsize >> 1;
2121 len = this->chipsize >> 1;
2122 }
2123 }
2124
2125 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2126}
2127
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002128/**
2129 * onenand_check_features - Check and set OneNAND features
2130 * @param mtd MTD data structure
2131 *
2132 * Check and set OneNAND features
2133 * - lock scheme
2134 * - two plane
2135 */
2136static void onenand_check_features(struct mtd_info *mtd)
2137{
2138 struct onenand_chip *this = mtd->priv;
2139 unsigned int density, process;
2140
2141 /* Lock scheme depends on density and process */
2142 density = onenand_get_density(this->device_id);
2143 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2144
2145 /* Lock scheme */
2146 switch (density) {
2147 case ONENAND_DEVICE_DENSITY_4Gb:
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002148 if (ONENAND_IS_DDP(this))
2149 this->options |= ONENAND_HAS_2PLANE;
2150 else
2151 this->options |= ONENAND_HAS_4KB_PAGE;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002152
2153 case ONENAND_DEVICE_DENSITY_2Gb:
2154 /* 2Gb DDP don't have 2 plane */
2155 if (!ONENAND_IS_DDP(this))
2156 this->options |= ONENAND_HAS_2PLANE;
2157 this->options |= ONENAND_HAS_UNLOCK_ALL;
2158
2159 case ONENAND_DEVICE_DENSITY_1Gb:
2160 /* A-Die has all block unlock */
2161 if (process)
2162 this->options |= ONENAND_HAS_UNLOCK_ALL;
2163 break;
2164
2165 default:
2166 /* Some OneNAND has continuous lock scheme */
2167 if (!process)
2168 this->options |= ONENAND_HAS_CONT_LOCK;
2169 break;
2170 }
2171
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302172 if (ONENAND_IS_MLC(this))
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002173 this->options |= ONENAND_HAS_4KB_PAGE;
2174
2175 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302176 this->options &= ~ONENAND_HAS_2PLANE;
2177
2178 if (FLEXONENAND(this)) {
2179 this->options &= ~ONENAND_HAS_CONT_LOCK;
2180 this->options |= ONENAND_HAS_UNLOCK_ALL;
2181 }
2182
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002183 if (this->options & ONENAND_HAS_CONT_LOCK)
2184 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2185 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2186 printk(KERN_DEBUG "Chip support all block unlock\n");
2187 if (this->options & ONENAND_HAS_2PLANE)
2188 printk(KERN_DEBUG "Chip has 2 plane\n");
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002189 if (this->options & ONENAND_HAS_4KB_PAGE)
2190 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2191
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002192}
2193
2194/**
Kyungmin Park79e90462007-09-10 17:13:49 +09002195 * onenand_print_device_info - Print device ID
2196 * @param device device ID
2197 *
2198 * Print device ID
2199 */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002200char *onenand_print_device_info(int device, int version)
Kyungmin Park79e90462007-09-10 17:13:49 +09002201{
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302202 int vcc, demuxed, ddp, density, flexonenand;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002203 char *dev_info = malloc(80);
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002204 char *p = dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002205
2206 vcc = device & ONENAND_DEVICE_VCC_MASK;
2207 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2208 ddp = device & ONENAND_DEVICE_IS_DDP;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302209 density = onenand_get_density(device);
2210 flexonenand = device & DEVICE_IS_FLEXONENAND;
2211 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
Kyungmin Park79e90462007-09-10 17:13:49 +09002212 demuxed ? "" : "Muxed ",
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302213 flexonenand ? "Flex-" : "",
Kyungmin Park79e90462007-09-10 17:13:49 +09002214 ddp ? "(DDP)" : "",
2215 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002216
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002217 sprintf(p, "\nOneNAND version = 0x%04x", version);
2218 printk("%s\n", dev_info);
2219
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002220 return dev_info;
Kyungmin Park79e90462007-09-10 17:13:49 +09002221}
2222
2223static const struct onenand_manufacturers onenand_manuf_ids[] = {
Enric Balletbo i Serra891d81b2010-10-11 21:48:03 +02002224 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002225 {ONENAND_MFR_SAMSUNG, "Samsung"},
Kyungmin Park79e90462007-09-10 17:13:49 +09002226};
2227
2228/**
2229 * onenand_check_maf - Check manufacturer ID
2230 * @param manuf manufacturer ID
2231 *
2232 * Check manufacturer ID
2233 */
2234static int onenand_check_maf(int manuf)
2235{
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002236 int size = ARRAY_SIZE(onenand_manuf_ids);
Kyungmin Park79e90462007-09-10 17:13:49 +09002237 int i;
Marek Vasute3334742011-11-06 00:59:52 +01002238#ifdef ONENAND_DEBUG
2239 char *name;
2240#endif
Kyungmin Park79e90462007-09-10 17:13:49 +09002241
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302242 for (i = 0; i < size; i++)
Kyungmin Park79e90462007-09-10 17:13:49 +09002243 if (manuf == onenand_manuf_ids[i].id)
2244 break;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002245
Marek Vasute3334742011-11-06 00:59:52 +01002246#ifdef ONENAND_DEBUG
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002247 if (i < size)
2248 name = onenand_manuf_ids[i].name;
2249 else
2250 name = "Unknown";
Kyungmin Park79e90462007-09-10 17:13:49 +09002251
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002252 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
Kyungmin Park79e90462007-09-10 17:13:49 +09002253#endif
2254
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002255 return i == size;
Kyungmin Park79e90462007-09-10 17:13:49 +09002256}
2257
2258/**
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302259* flexonenand_get_boundary - Reads the SLC boundary
2260* @param onenand_info - onenand info structure
2261*
2262* Fill up boundary[] field in onenand_chip
2263**/
2264static int flexonenand_get_boundary(struct mtd_info *mtd)
2265{
2266 struct onenand_chip *this = mtd->priv;
2267 unsigned int die, bdry;
Marek Vasute3334742011-11-06 00:59:52 +01002268 int syscfg, locked;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302269
2270 /* Disable ECC */
2271 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2272 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2273
2274 for (die = 0; die < this->dies; die++) {
2275 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2276 this->wait(mtd, FL_SYNCING);
2277
2278 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002279 this->wait(mtd, FL_READING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302280
2281 bdry = this->read_word(this->base + ONENAND_DATARAM);
2282 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2283 locked = 0;
2284 else
2285 locked = 1;
2286 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2287
2288 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Marek Vasute3334742011-11-06 00:59:52 +01002289 this->wait(mtd, FL_RESETING);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302290
2291 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2292 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2293 }
2294
2295 /* Enable ECC */
2296 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2297 return 0;
2298}
2299
2300/**
2301 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
Wolfgang Denk62fb2b42021-09-27 17:42:39 +02002302 * boundary[], diesize[], mtd->size, mtd->erasesize,
2303 * mtd->eraseregions
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302304 * @param mtd - MTD device structure
2305 */
2306static void flexonenand_get_size(struct mtd_info *mtd)
2307{
2308 struct onenand_chip *this = mtd->priv;
2309 int die, i, eraseshift, density;
2310 int blksperdie, maxbdry;
2311 loff_t ofs;
2312
2313 density = onenand_get_density(this->device_id);
2314 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2315 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2316 maxbdry = blksperdie - 1;
2317 eraseshift = this->erase_shift - 1;
2318
2319 mtd->numeraseregions = this->dies << 1;
2320
2321 /* This fills up the device boundary */
2322 flexonenand_get_boundary(mtd);
2323 die = 0;
2324 ofs = 0;
2325 i = -1;
2326 for (; die < this->dies; die++) {
2327 if (!die || this->boundary[die-1] != maxbdry) {
2328 i++;
2329 mtd->eraseregions[i].offset = ofs;
2330 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2331 mtd->eraseregions[i].numblocks =
2332 this->boundary[die] + 1;
2333 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2334 eraseshift++;
2335 } else {
2336 mtd->numeraseregions -= 1;
2337 mtd->eraseregions[i].numblocks +=
2338 this->boundary[die] + 1;
2339 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2340 }
2341 if (this->boundary[die] != maxbdry) {
2342 i++;
2343 mtd->eraseregions[i].offset = ofs;
2344 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2345 mtd->eraseregions[i].numblocks = maxbdry ^
2346 this->boundary[die];
2347 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2348 eraseshift--;
2349 } else
2350 mtd->numeraseregions -= 1;
2351 }
2352
2353 /* Expose MLC erase size except when all blocks are SLC */
2354 mtd->erasesize = 1 << this->erase_shift;
2355 if (mtd->numeraseregions == 1)
2356 mtd->erasesize >>= 1;
2357
2358 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2359 for (i = 0; i < mtd->numeraseregions; i++)
2360 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2361 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2362 mtd->eraseregions[i].erasesize,
2363 mtd->eraseregions[i].numblocks);
2364
2365 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2366 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2367 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2368 << (this->erase_shift - 1);
2369 mtd->size += this->diesize[die];
2370 }
2371}
2372
2373/**
2374 * flexonenand_check_blocks_erased - Check if blocks are erased
2375 * @param mtd_info - mtd info structure
2376 * @param start - first erase block to check
2377 * @param end - last erase block to check
2378 *
2379 * Converting an unerased block from MLC to SLC
2380 * causes byte values to change. Since both data and its ECC
2381 * have changed, reads on the block give uncorrectable error.
2382 * This might lead to the block being detected as bad.
2383 *
2384 * Avoid this by ensuring that the block to be converted is
2385 * erased.
2386 */
2387static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2388 int start, int end)
2389{
2390 struct onenand_chip *this = mtd->priv;
2391 int i, ret;
2392 int block;
2393 struct mtd_oob_ops ops = {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002394 .mode = MTD_OPS_PLACE_OOB,
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302395 .ooboffs = 0,
2396 .ooblen = mtd->oobsize,
2397 .datbuf = NULL,
2398 .oobbuf = this->oob_buf,
2399 };
2400 loff_t addr;
2401
2402 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2403
2404 for (block = start; block <= end; block++) {
2405 addr = flexonenand_addr(this, block);
2406 if (onenand_block_isbad_nolock(mtd, addr, 0))
2407 continue;
2408
2409 /*
2410 * Since main area write results in ECC write to spare,
2411 * it is sufficient to check only ECC bytes for change.
2412 */
2413 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2414 if (ret)
2415 return ret;
2416
2417 for (i = 0; i < mtd->oobsize; i++)
2418 if (this->oob_buf[i] != 0xff)
2419 break;
2420
2421 if (i != mtd->oobsize) {
2422 printk(KERN_WARNING "Block %d not erased.\n", block);
2423 return 1;
2424 }
2425 }
2426
2427 return 0;
2428}
2429
2430/**
2431 * flexonenand_set_boundary - Writes the SLC boundary
2432 * @param mtd - mtd info structure
2433 */
2434int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2435 int boundary, int lock)
2436{
2437 struct onenand_chip *this = mtd->priv;
2438 int ret, density, blksperdie, old, new, thisboundary;
2439 loff_t addr;
2440
2441 if (die >= this->dies)
2442 return -EINVAL;
2443
2444 if (boundary == this->boundary[die])
2445 return 0;
2446
2447 density = onenand_get_density(this->device_id);
2448 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2449 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2450
2451 if (boundary >= blksperdie) {
2452 printk("flexonenand_set_boundary:"
2453 "Invalid boundary value. "
2454 "Boundary not changed.\n");
2455 return -EINVAL;
2456 }
2457
2458 /* Check if converting blocks are erased */
2459 old = this->boundary[die] + (die * this->density_mask);
2460 new = boundary + (die * this->density_mask);
2461 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2462 + 1, max(old, new));
2463 if (ret) {
2464 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2465 return ret;
2466 }
2467
2468 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2469 this->wait(mtd, FL_SYNCING);
2470
2471 /* Check is boundary is locked */
2472 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2473 ret = this->wait(mtd, FL_READING);
2474
2475 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2476 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2477 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2478 goto out;
2479 }
2480
2481 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2482 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2483
2484 boundary &= FLEXONENAND_PI_MASK;
2485 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2486
2487 addr = die ? this->diesize[0] : 0;
2488 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2489 ret = this->wait(mtd, FL_ERASING);
2490 if (ret) {
2491 printk("flexonenand_set_boundary:"
2492 "Failed PI erase for Die %d\n", die);
2493 goto out;
2494 }
2495
2496 this->write_word(boundary, this->base + ONENAND_DATARAM);
2497 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2498 ret = this->wait(mtd, FL_WRITING);
2499 if (ret) {
2500 printk("flexonenand_set_boundary:"
2501 "Failed PI write for Die %d\n", die);
2502 goto out;
2503 }
2504
2505 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2506 ret = this->wait(mtd, FL_WRITING);
2507out:
2508 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2509 this->wait(mtd, FL_RESETING);
2510 if (!ret)
2511 /* Recalculate device size on boundary change*/
2512 flexonenand_get_size(mtd);
2513
2514 return ret;
2515}
2516
2517/**
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002518 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
Kyungmin Park79e90462007-09-10 17:13:49 +09002519 * @param mtd MTD device structure
2520 *
2521 * OneNAND detection method:
2522 * Compare the the values from command with ones from register
2523 */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002524static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Park79e90462007-09-10 17:13:49 +09002525{
2526 struct onenand_chip *this = mtd->priv;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002527 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002528 int syscfg;
2529
2530 /* Save system configuration 1 */
2531 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002532
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002533 /* Clear Sync. Burst Read mode to read BootRAM */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002534 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2535 this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Park79e90462007-09-10 17:13:49 +09002536
2537 /* Send the command for reading device ID from BootRAM */
2538 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2539
2540 /* Read manufacturer and device IDs from BootRAM */
2541 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2542 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2543
Kyungmin Park79e90462007-09-10 17:13:49 +09002544 /* Reset OneNAND to read default register values */
2545 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2546
Kyungmin Park396b0c42008-08-13 09:11:02 +09002547 /* Wait reset */
Ladislav Michl243af2f2016-07-12 20:28:19 +02002548 if (this->wait(mtd, FL_RESETING))
2549 return -ENXIO;
Kyungmin Park79e90462007-09-10 17:13:49 +09002550
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002551 /* Restore system configuration 1 */
2552 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2553
2554 /* Check manufacturer ID */
2555 if (onenand_check_maf(bram_maf_id))
2556 return -ENXIO;
2557
Kyungmin Park79e90462007-09-10 17:13:49 +09002558 /* Read manufacturer and device IDs from Register */
2559 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2560 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2561
2562 /* Check OneNAND device */
2563 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2564 return -ENXIO;
Kyungmin Park05d23182008-03-17 08:54:06 +09002565
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002566 return 0;
2567}
2568
2569/**
2570 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2571 * @param mtd MTD device structure
2572 *
2573 * OneNAND detection method:
2574 * Compare the the values from command with ones from register
2575 */
2576int onenand_probe(struct mtd_info *mtd)
2577{
2578 struct onenand_chip *this = mtd->priv;
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002579 int dev_id, ver_id;
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002580 int density;
2581 int ret;
2582
2583 ret = this->chip_probe(mtd);
2584 if (ret)
2585 return ret;
2586
Wolfgang Denkb3efbbf2012-04-19 01:14:17 +00002587 /* Read device IDs from Register */
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002588 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2589 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2590 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2591
Kyungmin Park79e90462007-09-10 17:13:49 +09002592 /* Flash device information */
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002593 mtd->name = onenand_print_device_info(dev_id, ver_id);
Kyungmin Park79e90462007-09-10 17:13:49 +09002594 this->device_id = dev_id;
Stefan Roese7c9aafe2008-11-11 10:29:09 +01002595 this->version_id = ver_id;
Kyungmin Park79e90462007-09-10 17:13:49 +09002596
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002597 /* Check OneNAND features */
2598 onenand_check_features(mtd);
2599
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002600 density = onenand_get_density(dev_id);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302601 if (FLEXONENAND(this)) {
2602 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2603 /* Maximum possible erase regions */
2604 mtd->numeraseregions = this->dies << 1;
2605 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2606 * (this->dies << 1));
2607 if (!mtd->eraseregions)
2608 return -ENOMEM;
2609 }
2610
2611 /*
2612 * For Flex-OneNAND, chipsize represents maximum possible device size.
2613 * mtd->size represents the actual device size.
2614 */
Kyungmin Park79e90462007-09-10 17:13:49 +09002615 this->chipsize = (16 << density) << 20;
2616
2617 /* OneNAND page size & block size */
2618 /* The data buffer size is equal to page size */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002619 mtd->writesize =
Kyungmin Park79e90462007-09-10 17:13:49 +09002620 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302621 /* We use the full BufferRAM */
Lukasz Majewski2b4fa322011-11-09 10:30:06 +01002622 if (ONENAND_IS_4KB_PAGE(this))
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302623 mtd->writesize <<= 1;
2624
Kyungmin Park396b0c42008-08-13 09:11:02 +09002625 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park79e90462007-09-10 17:13:49 +09002626 /* Pagers per block is always 64 in OneNAND */
Kyungmin Park396b0c42008-08-13 09:11:02 +09002627 mtd->erasesize = mtd->writesize << 6;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302628 /*
2629 * Flex-OneNAND SLC area has 64 pages per block.
2630 * Flex-OneNAND MLC area has 128 pages per block.
2631 * Expose MLC erase size to find erase_shift and page_mask.
2632 */
2633 if (FLEXONENAND(this))
2634 mtd->erasesize <<= 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002635
2636 this->erase_shift = ffs(mtd->erasesize) - 1;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002637 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park79e90462007-09-10 17:13:49 +09002638 this->ppb_shift = (this->erase_shift - this->page_shift);
Kyungmin Park396b0c42008-08-13 09:11:02 +09002639 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302640 /* Set density mask. it is used for DDP */
2641 if (ONENAND_IS_DDP(this))
2642 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002643 /* It's real page size */
2644 this->writesize = mtd->writesize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002645
2646 /* REVIST: Multichip handling */
2647
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302648 if (FLEXONENAND(this))
2649 flexonenand_get_size(mtd);
2650 else
2651 mtd->size = this->chipsize;
Kyungmin Park79e90462007-09-10 17:13:49 +09002652
Pali Rohár1b42e852020-10-31 17:32:50 +01002653 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
Kyungmin Park396b0c42008-08-13 09:11:02 +09002654 mtd->flags = MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002655 mtd->_erase = onenand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002656 mtd->_read_oob = onenand_read_oob;
2657 mtd->_write_oob = onenand_write_oob;
2658 mtd->_sync = onenand_sync;
2659 mtd->_block_isbad = onenand_block_isbad;
2660 mtd->_block_markbad = onenand_block_markbad;
Ladislav Michl7dbad5c2016-07-12 20:28:21 +02002661 mtd->writebufsize = mtd->writesize;
Fathi BOUDRA95feb702008-08-06 10:06:20 +02002662
Kyungmin Park79e90462007-09-10 17:13:49 +09002663 return 0;
2664}
2665
2666/**
2667 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2668 * @param mtd MTD device structure
2669 * @param maxchips Number of chips to scan for
2670 *
2671 * This fills out all the not initialized function pointers
2672 * with the defaults.
2673 * The flash ID is read and the mtd/chip structures are
2674 * filled with the appropriate values.
2675 */
2676int onenand_scan(struct mtd_info *mtd, int maxchips)
2677{
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002678 int i;
Kyungmin Park79e90462007-09-10 17:13:49 +09002679 struct onenand_chip *this = mtd->priv;
2680
2681 if (!this->read_word)
2682 this->read_word = onenand_readw;
2683 if (!this->write_word)
2684 this->write_word = onenand_writew;
2685
2686 if (!this->command)
2687 this->command = onenand_command;
2688 if (!this->wait)
2689 this->wait = onenand_wait;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002690 if (!this->bbt_wait)
2691 this->bbt_wait = onenand_bbt_wait;
Kyungmin Park79e90462007-09-10 17:13:49 +09002692
2693 if (!this->read_bufferram)
2694 this->read_bufferram = onenand_read_bufferram;
2695 if (!this->write_bufferram)
2696 this->write_bufferram = onenand_write_bufferram;
2697
Lukasz Majewski9d7ba322011-11-09 11:25:32 +01002698 if (!this->chip_probe)
2699 this->chip_probe = onenand_chip_probe;
2700
Kyungmin Park87b49502008-11-13 15:14:33 +09002701 if (!this->block_markbad)
2702 this->block_markbad = onenand_default_block_markbad;
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002703 if (!this->scan_bbt)
2704 this->scan_bbt = onenand_default_bbt;
2705
Kyungmin Park79e90462007-09-10 17:13:49 +09002706 if (onenand_probe(mtd))
2707 return -ENXIO;
2708
2709 /* Set Sync. Burst Read after probing */
2710 if (this->mmcontrol) {
2711 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2712 this->read_bufferram = onenand_sync_read_bufferram;
2713 }
2714
Kyungmin Park5d7a01c2008-08-19 08:42:53 +09002715 /* Allocate buffers, if necessary */
2716 if (!this->page_buf) {
2717 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2718 if (!this->page_buf) {
2719 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2720 return -ENOMEM;
2721 }
2722 this->options |= ONENAND_PAGEBUF_ALLOC;
2723 }
2724 if (!this->oob_buf) {
2725 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2726 if (!this->oob_buf) {
2727 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2728 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2729 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2730 kfree(this->page_buf);
2731 }
2732 return -ENOMEM;
2733 }
2734 this->options |= ONENAND_OOBBUF_ALLOC;
2735 }
2736
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002737 this->state = FL_READY;
2738
2739 /*
2740 * Allow subpage writes up to oobsize.
2741 */
2742 switch (mtd->oobsize) {
Amul Kumar Sahaed886a22009-11-06 17:15:31 +05302743 case 128:
2744 this->ecclayout = &onenand_oob_128;
2745 mtd->subpage_sft = 0;
2746 break;
2747
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002748 case 64:
2749 this->ecclayout = &onenand_oob_64;
2750 mtd->subpage_sft = 2;
2751 break;
2752
2753 case 32:
2754 this->ecclayout = &onenand_oob_32;
2755 mtd->subpage_sft = 1;
2756 break;
2757
2758 default:
2759 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2760 mtd->oobsize);
2761 mtd->subpage_sft = 0;
2762 /* To prevent kernel oops */
2763 this->ecclayout = &onenand_oob_32;
2764 break;
2765 }
2766
2767 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2768
2769 /*
2770 * The number of bytes available for a client to place data into
2771 * the out of band area
2772 */
2773 this->ecclayout->oobavail = 0;
Prabhakar Kushwaha4d2ba172013-10-04 13:47:58 +05302774
2775 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
Stefan Roesedb7ce8c2008-12-02 11:06:47 +01002776 this->ecclayout->oobfree[i].length; i++)
2777 this->ecclayout->oobavail +=
2778 this->ecclayout->oobfree[i].length;
2779 mtd->oobavail = this->ecclayout->oobavail;
2780
2781 mtd->ecclayout = this->ecclayout;
2782
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002783 /* Unlock whole block */
2784 onenand_unlock_all(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002785
Kyungmin Parkdc7a79c2008-11-04 09:24:07 +09002786 return this->scan_bbt(mtd);
Kyungmin Park79e90462007-09-10 17:13:49 +09002787}
2788
2789/**
2790 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2791 * @param mtd MTD device structure
2792 */
2793void onenand_release(struct mtd_info *mtd)
2794{
2795}