blob: e082d632fb742e1719d68918a4be47a10c4ef1c1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002/*
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303 * Copyright © 2005-2009 Samsung Electronics
4 * Copyright © 2007 Nokia Corporation
5 *
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01006 * Kyungmin Park <kyungmin.park@samsung.com>
7 *
Adrian Hunter81280d52007-02-15 09:47:29 +09008 * Credits:
9 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
10 * auto-placement support, read-while load support, various fixes
Adrian Hunter81280d52007-02-15 09:47:29 +090011 *
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070012 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
13 * Flex-OneNAND support
Amul Kumar Saha3cf60252009-10-21 17:00:05 +053014 * Amul Kumar Saha <amul.saha at samsung.com>
15 * OTP support
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010016 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
Amul Sahac90173f2009-06-16 11:24:01 +053020#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Andrew Morton015953d2005-11-08 21:34:28 -080022#include <linux/sched.h>
Richard Purdie6c77fd642008-02-06 10:18:22 +000023#include <linux/delay.h>
Kyungmin Park2c221202006-11-16 11:23:48 +090024#include <linux/interrupt.h>
Andrew Morton015953d2005-11-08 21:34:28 -080025#include <linux/jiffies.h>
Kyungmin Parkcd5f6342005-07-11 11:41:53 +010026#include <linux/mtd/mtd.h>
27#include <linux/mtd/onenand.h>
28#include <linux/mtd/partitions.h>
29
30#include <asm/io.h>
31
Mika Korhonen72073022009-10-23 07:50:43 +020032/*
33 * Multiblock erase if number of blocks to erase is 2 or more.
34 * Maximum number of blocks for simultaneous erase is 64.
35 */
36#define MB_ERASE_MIN_BLK_COUNT 2
37#define MB_ERASE_MAX_BLK_COUNT 64
38
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070039/* Default Flex-OneNAND boundary and lock respectively */
40static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
41
Amul Sahac90173f2009-06-16 11:24:01 +053042module_param_array(flex_bdry, int, NULL, 0400);
43MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
44 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
45 "DIE_BDRY: SLC boundary of the die"
46 "LOCK: Locking information for SLC boundary"
47 " : 0->Set boundary in unlocked status"
48 " : 1->Set boundary in locked status");
49
Amul Kumar Saha3cf60252009-10-21 17:00:05 +053050/* Default OneNAND/Flex-OneNAND OTP options*/
51static int otp;
52
53module_param(otp, int, 0400);
54MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
55 "Syntax : otp=LOCK_TYPE"
56 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
57 " : 0 -> Default (No Blocks Locked)"
58 " : 1 -> OTP Block lock"
59 " : 2 -> 1st Block lock"
60 " : 3 -> BOTH OTP Block and 1st Block lock");
61
Roman Tereshonkov99b17c02011-04-11 12:52:01 +030062/*
63 * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
64 * For now, we expose only 64 out of 80 ecc bytes
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070065 */
Boris Brezillona4116792015-12-07 22:46:45 +010066static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section,
67 struct mtd_oob_region *oobregion)
68{
69 if (section > 7)
70 return -ERANGE;
71
72 oobregion->offset = (section * 16) + 6;
73 oobregion->length = 10;
74
75 return 0;
76}
77
78static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section,
79 struct mtd_oob_region *oobregion)
80{
81 if (section > 7)
82 return -ERANGE;
83
84 oobregion->offset = (section * 16) + 2;
85 oobregion->length = 4;
86
87 return 0;
88}
89
90static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = {
91 .ecc = flexonenand_ooblayout_ecc,
92 .free = flexonenand_ooblayout_free,
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070093};
94
Roman Tereshonkov99b17c02011-04-11 12:52:01 +030095/*
96 * onenand_oob_128 - oob info for OneNAND with 4KB page
97 *
98 * Based on specification:
99 * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
100 *
Roman Tereshonkov99b17c02011-04-11 12:52:01 +0300101 */
Boris Brezillona4116792015-12-07 22:46:45 +0100102static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section,
103 struct mtd_oob_region *oobregion)
104{
105 if (section > 7)
106 return -ERANGE;
107
108 oobregion->offset = (section * 16) + 7;
109 oobregion->length = 9;
110
111 return 0;
112}
113
114static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section,
115 struct mtd_oob_region *oobregion)
116{
117 if (section >= 8)
118 return -ERANGE;
119
120 /*
121 * free bytes are using the spare area fields marked as
122 * "Managed by internal ECC logic for Logical Sector Number area"
123 */
124 oobregion->offset = (section * 16) + 2;
125 oobregion->length = 3;
126
127 return 0;
128}
129
130static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = {
131 .ecc = onenand_ooblayout_128_ecc,
132 .free = onenand_ooblayout_128_free,
Roman Tereshonkov99b17c02011-04-11 12:52:01 +0300133};
134
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100135/**
Boris Brezillona4116792015-12-07 22:46:45 +0100136 * onenand_oob_32_64 - oob info for large (2KB) page
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100137 */
Boris Brezillona4116792015-12-07 22:46:45 +0100138static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section,
139 struct mtd_oob_region *oobregion)
140{
141 if (section > 3)
142 return -ERANGE;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100143
Boris Brezillona4116792015-12-07 22:46:45 +0100144 oobregion->offset = (section * 16) + 8;
145 oobregion->length = 5;
146
147 return 0;
148}
149
150static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section,
151 struct mtd_oob_region *oobregion)
152{
153 int sections = (mtd->oobsize / 32) * 2;
154
155 if (section >= sections)
156 return -ERANGE;
157
158 if (section & 1) {
159 oobregion->offset = ((section - 1) * 16) + 14;
160 oobregion->length = 2;
161 } else {
162 oobregion->offset = (section * 16) + 2;
163 oobregion->length = 3;
164 }
165
166 return 0;
167}
168
169static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = {
170 .ecc = onenand_ooblayout_32_64_ecc,
171 .free = onenand_ooblayout_32_64_free,
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100172};
173
174static const unsigned char ffchars[] = {
175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
177 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
178 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
179 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
184 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
185 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
186 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100191};
192
193/**
194 * onenand_readw - [OneNAND Interface] Read OneNAND register
195 * @param addr address to read
196 *
197 * Read OneNAND register
198 */
199static unsigned short onenand_readw(void __iomem *addr)
200{
201 return readw(addr);
202}
203
204/**
205 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
206 * @param value value to write
207 * @param addr address to write
208 *
209 * Write OneNAND register with value
210 */
211static void onenand_writew(unsigned short value, void __iomem *addr)
212{
213 writew(value, addr);
214}
215
216/**
217 * onenand_block_address - [DEFAULT] Get block address
Kyungmin Park83a36832005-09-29 04:53:16 +0100218 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100219 * @param block the block
220 * @return translated block address if DDP, otherwise same
221 *
222 * Setup Start Address 1 Register (F100h)
223 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100224static int onenand_block_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100225{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900226 /* Device Flash Core select, NAND Flash Block Address */
227 if (block & this->density_mask)
228 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100229
230 return block;
231}
232
233/**
234 * onenand_bufferram_address - [DEFAULT] Get bufferram address
Kyungmin Park83a36832005-09-29 04:53:16 +0100235 * @param this onenand chip data structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100236 * @param block the block
237 * @return set DBS value if DDP, otherwise 0
238 *
239 * Setup Start Address 2 Register (F101h) for DDP
240 */
Kyungmin Park83a36832005-09-29 04:53:16 +0100241static int onenand_bufferram_address(struct onenand_chip *this, int block)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100242{
Kyungmin Park738d61f2007-01-15 17:09:14 +0900243 /* Device BufferRAM Select */
244 if (block & this->density_mask)
245 return ONENAND_DDP_CHIP1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100246
Kyungmin Park738d61f2007-01-15 17:09:14 +0900247 return ONENAND_DDP_CHIP0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100248}
249
250/**
251 * onenand_page_address - [DEFAULT] Get page address
252 * @param page the page address
253 * @param sector the sector address
254 * @return combined page and sector address
255 *
256 * Setup Start Address 8 Register (F107h)
257 */
258static int onenand_page_address(int page, int sector)
259{
260 /* Flash Page Address, Flash Sector Address */
261 int fpa, fsa;
262
263 fpa = page & ONENAND_FPA_MASK;
264 fsa = sector & ONENAND_FSA_MASK;
265
266 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
267}
268
269/**
270 * onenand_buffer_address - [DEFAULT] Get buffer address
271 * @param dataram1 DataRAM index
272 * @param sectors the sector address
273 * @param count the number of sectors
274 * @return the start buffer value
275 *
276 * Setup Start Buffer Register (F200h)
277 */
278static int onenand_buffer_address(int dataram1, int sectors, int count)
279{
280 int bsa, bsc;
281
282 /* BufferRAM Sector Address */
283 bsa = sectors & ONENAND_BSA_MASK;
284
285 if (dataram1)
286 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
287 else
288 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
289
290 /* BufferRAM Sector Count */
291 bsc = count & ONENAND_BSC_MASK;
292
293 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
294}
295
296/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700297 * flexonenand_block- For given address return block number
298 * @param this - OneNAND device structure
299 * @param addr - Address for which block number is needed
300 */
301static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
302{
303 unsigned boundary, blk, die = 0;
304
305 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
306 die = 1;
307 addr -= this->diesize[0];
308 }
309
310 boundary = this->boundary[die];
311
312 blk = addr >> (this->erase_shift - 1);
313 if (blk > boundary)
314 blk = (blk + boundary + 1) >> 1;
315
316 blk += die ? this->density_mask : 0;
317 return blk;
318}
319
320inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
321{
322 if (!FLEXONENAND(this))
323 return addr >> this->erase_shift;
324 return flexonenand_block(this, addr);
325}
326
327/**
328 * flexonenand_addr - Return address of the block
329 * @this: OneNAND device structure
330 * @block: Block number on Flex-OneNAND
331 *
332 * Return address of the block
333 */
334static loff_t flexonenand_addr(struct onenand_chip *this, int block)
335{
336 loff_t ofs = 0;
337 int die = 0, boundary;
338
339 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
340 block -= this->density_mask;
341 die = 1;
342 ofs = this->diesize[0];
343 }
344
345 boundary = this->boundary[die];
346 ofs += (loff_t)block << (this->erase_shift - 1);
347 if (block > (boundary + 1))
348 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
349 return ofs;
350}
351
352loff_t onenand_addr(struct onenand_chip *this, int block)
353{
354 if (!FLEXONENAND(this))
355 return (loff_t)block << this->erase_shift;
356 return flexonenand_addr(this, block);
357}
358EXPORT_SYMBOL(onenand_addr);
359
360/**
Kyungmin Parke71f04f2007-12-11 11:23:45 +0900361 * onenand_get_density - [DEFAULT] Get OneNAND density
362 * @param dev_id OneNAND device ID
363 *
364 * Get OneNAND density from device ID
365 */
366static inline int onenand_get_density(int dev_id)
367{
368 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
369 return (density & ONENAND_DEVICE_DENSITY_MASK);
370}
371
372/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700373 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
374 * @param mtd MTD device structure
375 * @param addr address whose erase region needs to be identified
376 */
377int flexonenand_region(struct mtd_info *mtd, loff_t addr)
378{
379 int i;
380
381 for (i = 0; i < mtd->numeraseregions; i++)
382 if (addr < mtd->eraseregions[i].offset)
383 break;
384 return i - 1;
385}
386EXPORT_SYMBOL(flexonenand_region);
387
388/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100389 * onenand_command - [DEFAULT] Send command to OneNAND device
390 * @param mtd MTD device structure
391 * @param cmd the command to be sent
392 * @param addr offset to read from or write to
393 * @param len number of bytes to read or write
394 *
395 * Send command to OneNAND device. This function is used for middle/large page
396 * devices (1KB/2KB Bytes per page)
397 */
398static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
399{
400 struct onenand_chip *this = mtd->priv;
Kyungmin Parkb21b72c2007-12-11 11:13:18 +0900401 int value, block, page;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100402
403 /* Address translation */
404 switch (cmd) {
405 case ONENAND_CMD_UNLOCK:
406 case ONENAND_CMD_LOCK:
407 case ONENAND_CMD_LOCK_TIGHT:
Kyungmin Park28b79ff2006-09-26 09:45:28 +0000408 case ONENAND_CMD_UNLOCK_ALL:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100409 block = -1;
410 page = -1;
411 break;
412
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700413 case FLEXONENAND_CMD_PI_ACCESS:
414 /* addr contains die index */
415 block = addr * this->density_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100416 page = -1;
417 break;
418
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700419 case ONENAND_CMD_ERASE:
Mika Korhonen72073022009-10-23 07:50:43 +0200420 case ONENAND_CMD_MULTIBLOCK_ERASE:
421 case ONENAND_CMD_ERASE_VERIFY:
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700422 case ONENAND_CMD_BUFFERRAM:
423 case ONENAND_CMD_OTP_ACCESS:
424 block = onenand_block(this, addr);
425 page = -1;
426 break;
427
428 case FLEXONENAND_CMD_READ_PI:
429 cmd = ONENAND_CMD_READ;
430 block = addr * this->density_mask;
431 page = 0;
432 break;
433
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100434 default:
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700435 block = onenand_block(this, addr);
Rohit Hassan Sathyanarayan42b0aab2010-07-23 12:29:25 +0530436 if (FLEXONENAND(this))
437 page = (int) (addr - onenand_addr(this, block))>>\
438 this->page_shift;
439 else
440 page = (int) (addr >> this->page_shift);
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900441 if (ONENAND_IS_2PLANE(this)) {
442 /* Make the even block number */
443 block &= ~1;
444 /* Is it the odd plane? */
445 if (addr & this->writesize)
446 block++;
447 page >>= 1;
448 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100449 page &= this->page_mask;
450 break;
451 }
452
453 /* NOTE: The setting order of the registers is very important! */
454 if (cmd == ONENAND_CMD_BUFFERRAM) {
455 /* Select DataRAM for DDP */
Kyungmin Park83a36832005-09-29 04:53:16 +0100456 value = onenand_bufferram_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100457 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
458
Kyungmin Park8a8f6322010-12-02 09:24:16 +0900459 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900460 /* It is always BufferRAM0 */
461 ONENAND_SET_BUFFERRAM0(this);
462 else
463 /* Switch to the next data buffer */
464 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100465
466 return 0;
467 }
468
469 if (block != -1) {
470 /* Write 'DFS, FBA' of Flash */
Kyungmin Park83a36832005-09-29 04:53:16 +0100471 value = onenand_block_address(this, block);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100472 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
Kyungmin Park3cecf692006-05-12 17:02:51 +0300473
Kyungmin Parkb21b72c2007-12-11 11:13:18 +0900474 /* Select DataRAM for DDP */
475 value = onenand_bufferram_address(this, block);
476 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100477 }
478
479 if (page != -1) {
Kyungmin Park60d84f92006-12-22 16:21:54 +0900480 /* Now we use page size operation */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700481 int sectors = 0, count = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100482 int dataram;
483
484 switch (cmd) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700485 case FLEXONENAND_CMD_RECOVER_LSB:
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100486 case ONENAND_CMD_READ:
487 case ONENAND_CMD_READOOB:
Kyungmin Park8a8f6322010-12-02 09:24:16 +0900488 if (ONENAND_IS_4KB_PAGE(this))
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700489 /* It is always BufferRAM0 */
490 dataram = ONENAND_SET_BUFFERRAM0(this);
491 else
492 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100493 break;
494
495 default:
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900496 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
497 cmd = ONENAND_CMD_2X_PROG;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100498 dataram = ONENAND_CURRENT_BUFFERRAM(this);
499 break;
500 }
501
502 /* Write 'FPA, FSA' of Flash */
503 value = onenand_page_address(page, sectors);
504 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
505
506 /* Write 'BSA, BSC' of DataRAM */
507 value = onenand_buffer_address(dataram, sectors, count);
508 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100509 }
510
511 /* Interrupt clear */
512 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
513
514 /* Write command */
515 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
516
517 return 0;
518}
519
520/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700521 * onenand_read_ecc - return ecc status
522 * @param this onenand chip structure
523 */
524static inline int onenand_read_ecc(struct onenand_chip *this)
525{
526 int ecc, i, result = 0;
527
Kyungmin Park6a88c472010-04-28 17:46:45 +0200528 if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700529 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
530
531 for (i = 0; i < 4; i++) {
Kyungmin Park6a88c472010-04-28 17:46:45 +0200532 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700533 if (likely(!ecc))
534 continue;
535 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
536 return ONENAND_ECC_2BIT_ALL;
537 else
538 result = ONENAND_ECC_1BIT_ALL;
539 }
540
541 return result;
542}
543
544/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100545 * onenand_wait - [DEFAULT] wait until the command is done
546 * @param mtd MTD device structure
547 * @param state state to select the max. timeout value
548 *
549 * Wait for command done. This applies to all OneNAND command
550 * Read can take up to 30us, erase up to 2ms and program up to 350us
551 * according to general OneNAND specs
552 */
553static int onenand_wait(struct mtd_info *mtd, int state)
554{
555 struct onenand_chip * this = mtd->priv;
556 unsigned long timeout;
557 unsigned int flags = ONENAND_INT_MASTER;
558 unsigned int interrupt = 0;
Kyungmin Park2fd32d42006-12-29 11:51:40 +0900559 unsigned int ctrl;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100560
561 /* The 20 msec is enough */
562 timeout = jiffies + msecs_to_jiffies(20);
563 while (time_before(jiffies, timeout)) {
564 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
565
566 if (interrupt & flags)
567 break;
568
Mika Korhonen72073022009-10-23 07:50:43 +0200569 if (state != FL_READING && state != FL_PREPARING_ERASE)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100570 cond_resched();
571 }
572 /* To get correct interrupt status in timeout case */
573 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
574
575 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
576
Kyungmin Park83973b82008-05-29 14:52:40 +0900577 /*
578 * In the Spec. it checks the controller status first
579 * However if you get the correct information in case of
580 * power off recovery (POR) test, it should read ECC status first
581 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100582 if (interrupt & ONENAND_INT_READ) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700583 int ecc = onenand_read_ecc(this);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900584 if (ecc) {
Kyungmin Parkb3c9f8b2007-01-05 19:16:04 +0900585 if (ecc & ONENAND_ECC_2BIT_ALL) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530586 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
587 __func__, ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900588 mtd->ecc_stats.failed++;
Adrian Hunter30a7eb22007-10-12 10:19:38 +0300589 return -EBADMSG;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300590 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530591 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
592 __func__, ecc);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900593 mtd->ecc_stats.corrected++;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +0300594 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100595 }
Adrian Hunter9d032802007-01-10 07:51:26 +0200596 } else if (state == FL_READING) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530597 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
598 __func__, ctrl, interrupt);
Adrian Hunter9d032802007-01-10 07:51:26 +0200599 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100600 }
601
Mika Korhonen72073022009-10-23 07:50:43 +0200602 if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
603 printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
604 __func__, ctrl, interrupt);
605 return -EIO;
606 }
607
608 if (!(interrupt & ONENAND_INT_MASTER)) {
609 printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
610 __func__, ctrl, interrupt);
611 return -EIO;
612 }
613
Kyungmin Park83973b82008-05-29 14:52:40 +0900614 /* If there's controller error, it's a real error */
615 if (ctrl & ONENAND_CTRL_ERROR) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530616 printk(KERN_ERR "%s: controller error = 0x%04x\n",
617 __func__, ctrl);
Kyungmin Park83973b82008-05-29 14:52:40 +0900618 if (ctrl & ONENAND_CTRL_LOCK)
Amul Kumar Saha297758f2009-10-02 16:59:11 +0530619 printk(KERN_ERR "%s: it's locked error.\n", __func__);
Kyungmin Park83973b82008-05-29 14:52:40 +0900620 return -EIO;
621 }
622
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100623 return 0;
624}
625
Kyungmin Park2c221202006-11-16 11:23:48 +0900626/*
627 * onenand_interrupt - [DEFAULT] onenand interrupt handler
628 * @param irq onenand interrupt number
629 * @param dev_id interrupt data
630 *
631 * complete the work
632 */
633static irqreturn_t onenand_interrupt(int irq, void *data)
634{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400635 struct onenand_chip *this = data;
Kyungmin Park2c221202006-11-16 11:23:48 +0900636
637 /* To handle shared interrupt */
638 if (!this->complete.done)
639 complete(&this->complete);
640
641 return IRQ_HANDLED;
642}
643
644/*
645 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
646 * @param mtd MTD device structure
647 * @param state state to select the max. timeout value
648 *
649 * Wait for command done.
650 */
651static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
652{
653 struct onenand_chip *this = mtd->priv;
654
Kyungmin Park2c221202006-11-16 11:23:48 +0900655 wait_for_completion(&this->complete);
656
657 return onenand_wait(mtd, state);
658}
659
660/*
661 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
662 * @param mtd MTD device structure
663 * @param state state to select the max. timeout value
664 *
665 * Try interrupt based wait (It is used one-time)
666 */
667static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
668{
669 struct onenand_chip *this = mtd->priv;
670 unsigned long remain, timeout;
671
672 /* We use interrupt wait first */
673 this->wait = onenand_interrupt_wait;
674
Kyungmin Park2c221202006-11-16 11:23:48 +0900675 timeout = msecs_to_jiffies(100);
676 remain = wait_for_completion_timeout(&this->complete, timeout);
677 if (!remain) {
678 printk(KERN_INFO "OneNAND: There's no interrupt. "
679 "We use the normal wait\n");
680
681 /* Release the irq */
682 free_irq(this->irq, this);
David Woodhousec9ac5972006-11-30 08:17:38 +0000683
Kyungmin Park2c221202006-11-16 11:23:48 +0900684 this->wait = onenand_wait;
685 }
686
687 return onenand_wait(mtd, state);
688}
689
690/*
691 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
692 * @param mtd MTD device structure
693 *
694 * There's two method to wait onenand work
695 * 1. polling - read interrupt status register
696 * 2. interrupt - use the kernel interrupt method
697 */
698static void onenand_setup_wait(struct mtd_info *mtd)
699{
700 struct onenand_chip *this = mtd->priv;
701 int syscfg;
702
703 init_completion(&this->complete);
704
705 if (this->irq <= 0) {
706 this->wait = onenand_wait;
707 return;
708 }
709
710 if (request_irq(this->irq, &onenand_interrupt,
711 IRQF_SHARED, "onenand", this)) {
712 /* If we can't get irq, use the normal wait */
713 this->wait = onenand_wait;
714 return;
715 }
716
717 /* Enable interrupt */
718 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
719 syscfg |= ONENAND_SYS_CFG1_IOBE;
720 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
721
722 this->wait = onenand_try_interrupt_wait;
723}
724
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100725/**
726 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
727 * @param mtd MTD data structure
728 * @param area BufferRAM area
729 * @return offset given area
730 *
731 * Return BufferRAM offset given area
732 */
733static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
734{
735 struct onenand_chip *this = mtd->priv;
736
737 if (ONENAND_CURRENT_BUFFERRAM(this)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900738 /* Note: the 'this->writesize' is a real page size */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100739 if (area == ONENAND_DATARAM)
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900740 return this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100741 if (area == ONENAND_SPARERAM)
742 return mtd->oobsize;
743 }
744
745 return 0;
746}
747
748/**
749 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
750 * @param mtd MTD data structure
751 * @param area BufferRAM area
752 * @param buffer the databuffer to put/get data
753 * @param offset offset to read from or write to
754 * @param count number of bytes to read/write
755 *
756 * Read the BufferRAM area
757 */
758static int onenand_read_bufferram(struct mtd_info *mtd, int area,
759 unsigned char *buffer, int offset, size_t count)
760{
761 struct onenand_chip *this = mtd->priv;
762 void __iomem *bufferram;
763
764 bufferram = this->base + area;
765
766 bufferram += onenand_bufferram_offset(mtd, area);
767
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300768 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
769 unsigned short word;
770
771 /* Align with word(16-bit) size */
772 count--;
773
774 /* Read word and save byte */
775 word = this->read_word(bufferram + offset + count);
776 buffer[count] = (word & 0xff);
777 }
778
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100779 memcpy(buffer, bufferram + offset, count);
780
781 return 0;
782}
783
784/**
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100785 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
786 * @param mtd MTD data structure
787 * @param area BufferRAM area
788 * @param buffer the databuffer to put/get data
789 * @param offset offset to read from or write to
790 * @param count number of bytes to read/write
791 *
792 * Read the BufferRAM area with Sync. Burst Mode
793 */
794static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
795 unsigned char *buffer, int offset, size_t count)
796{
797 struct onenand_chip *this = mtd->priv;
798 void __iomem *bufferram;
799
800 bufferram = this->base + area;
801
802 bufferram += onenand_bufferram_offset(mtd, area);
803
804 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
805
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300806 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
807 unsigned short word;
808
809 /* Align with word(16-bit) size */
810 count--;
811
812 /* Read word and save byte */
813 word = this->read_word(bufferram + offset + count);
814 buffer[count] = (word & 0xff);
815 }
816
Kyungmin Park52b0eea2005-09-03 07:07:19 +0100817 memcpy(buffer, bufferram + offset, count);
818
819 this->mmcontrol(mtd, 0);
820
821 return 0;
822}
823
824/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100825 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
826 * @param mtd MTD data structure
827 * @param area BufferRAM area
828 * @param buffer the databuffer to put/get data
829 * @param offset offset to read from or write to
830 * @param count number of bytes to read/write
831 *
832 * Write the BufferRAM area
833 */
834static int onenand_write_bufferram(struct mtd_info *mtd, int area,
835 const unsigned char *buffer, int offset, size_t count)
836{
837 struct onenand_chip *this = mtd->priv;
838 void __iomem *bufferram;
839
840 bufferram = this->base + area;
841
842 bufferram += onenand_bufferram_offset(mtd, area);
843
Kyungmin Park9c01f87d2006-05-12 17:02:31 +0300844 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
845 unsigned short word;
846 int byte_offset;
847
848 /* Align with word(16-bit) size */
849 count--;
850
851 /* Calculate byte access offset */
852 byte_offset = offset + count;
853
854 /* Read word and save byte */
855 word = this->read_word(bufferram + byte_offset);
856 word = (word & ~0xff) | buffer[count];
857 this->write_word(word, bufferram + byte_offset);
858 }
859
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100860 memcpy(bufferram + offset, buffer, count);
861
862 return 0;
863}
864
865/**
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900866 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
867 * @param mtd MTD data structure
868 * @param addr address to check
869 * @return blockpage address
870 *
871 * Get blockpage address at 2x program mode
872 */
873static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
874{
875 struct onenand_chip *this = mtd->priv;
876 int blockpage, block, page;
877
878 /* Calculate the even block number */
879 block = (int) (addr >> this->erase_shift) & ~1;
880 /* Is it the odd plane? */
881 if (addr & this->writesize)
882 block++;
883 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
884 blockpage = (block << 7) | page;
885
886 return blockpage;
887}
888
889/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100890 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
891 * @param mtd MTD data structure
892 * @param addr address to check
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000893 * @return 1 if there are valid data, otherwise 0
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100894 *
895 * Check bufferram if there is data we required
896 */
897static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
898{
899 struct onenand_chip *this = mtd->priv;
Adrian Huntercde36b32007-02-08 10:28:08 +0200900 int blockpage, found = 0;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900901 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000902
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900903 if (ONENAND_IS_2PLANE(this))
904 blockpage = onenand_get_2x_blockpage(mtd, addr);
905 else
906 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100907
908 /* Is there valid data? */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900909 i = ONENAND_CURRENT_BUFFERRAM(this);
910 if (this->bufferram[i].blockpage == blockpage)
Adrian Huntercde36b32007-02-08 10:28:08 +0200911 found = 1;
912 else {
913 /* Check another BufferRAM */
914 i = ONENAND_NEXT_BUFFERRAM(this);
915 if (this->bufferram[i].blockpage == blockpage) {
916 ONENAND_SET_NEXT_BUFFERRAM(this);
917 found = 1;
918 }
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900919 }
920
Adrian Huntercde36b32007-02-08 10:28:08 +0200921 if (found && ONENAND_IS_DDP(this)) {
922 /* Select DataRAM for DDP */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700923 int block = onenand_block(this, addr);
Adrian Huntercde36b32007-02-08 10:28:08 +0200924 int value = onenand_bufferram_address(this, block);
925 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
926 }
927
928 return found;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100929}
930
931/**
932 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
933 * @param mtd MTD data structure
934 * @param addr address to update
935 * @param valid valid flag
936 *
937 * Update BufferRAM information
938 */
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900939static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100940 int valid)
941{
942 struct onenand_chip *this = mtd->priv;
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900943 int blockpage;
944 unsigned int i;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +0000945
Kyungmin Parkee9745f2007-06-30 13:57:49 +0900946 if (ONENAND_IS_2PLANE(this))
947 blockpage = onenand_get_2x_blockpage(mtd, addr);
948 else
949 blockpage = (int) (addr >> this->page_shift);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100950
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900951 /* Invalidate another BufferRAM */
952 i = ONENAND_NEXT_BUFFERRAM(this);
Kyungmin Park5b4246f2007-02-02 09:39:21 +0900953 if (this->bufferram[i].blockpage == blockpage)
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900954 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100955
956 /* Update BufferRAM */
957 i = ONENAND_CURRENT_BUFFERRAM(this);
Kyungmin Parkabf3c0f2007-02-02 09:29:36 +0900958 if (valid)
959 this->bufferram[i].blockpage = blockpage;
960 else
961 this->bufferram[i].blockpage = -1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100962}
963
964/**
Adrian Hunter480b9df2007-02-07 13:55:19 +0200965 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
966 * @param mtd MTD data structure
967 * @param addr start address to invalidate
968 * @param len length to invalidate
969 *
970 * Invalidate BufferRAM information
971 */
972static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
973 unsigned int len)
974{
975 struct onenand_chip *this = mtd->priv;
976 int i;
977 loff_t end_addr = addr + len;
978
979 /* Invalidate BufferRAM */
980 for (i = 0; i < MAX_BUFFERRAM; i++) {
981 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
982 if (buf_addr >= addr && buf_addr < end_addr)
983 this->bufferram[i].blockpage = -1;
984 }
985}
986
987/**
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100988 * onenand_get_device - [GENERIC] Get chip for selected access
989 * @param mtd MTD device structure
990 * @param new_state the state which is requested
991 *
992 * Get the device and lock it for exclusive access
993 */
Kyungmin Parka41371e2005-09-29 03:55:31 +0100994static int onenand_get_device(struct mtd_info *mtd, int new_state)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +0100995{
996 struct onenand_chip *this = mtd->priv;
997 DECLARE_WAITQUEUE(wait, current);
998
999 /*
1000 * Grab the lock and see if the device is available
1001 */
1002 while (1) {
1003 spin_lock(&this->chip_lock);
1004 if (this->state == FL_READY) {
1005 this->state = new_state;
1006 spin_unlock(&this->chip_lock);
Adrian Huntercf24dc82010-02-19 15:39:52 +01001007 if (new_state != FL_PM_SUSPENDED && this->enable)
1008 this->enable(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001009 break;
1010 }
Kyungmin Parka41371e2005-09-29 03:55:31 +01001011 if (new_state == FL_PM_SUSPENDED) {
1012 spin_unlock(&this->chip_lock);
1013 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
1014 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001015 set_current_state(TASK_UNINTERRUPTIBLE);
1016 add_wait_queue(&this->wq, &wait);
1017 spin_unlock(&this->chip_lock);
1018 schedule();
1019 remove_wait_queue(&this->wq, &wait);
1020 }
Kyungmin Parka41371e2005-09-29 03:55:31 +01001021
1022 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001023}
1024
1025/**
1026 * onenand_release_device - [GENERIC] release chip
1027 * @param mtd MTD device structure
1028 *
1029 * Deselect, release chip lock and wake up anyone waiting on the device
1030 */
1031static void onenand_release_device(struct mtd_info *mtd)
1032{
1033 struct onenand_chip *this = mtd->priv;
1034
Adrian Huntercf24dc82010-02-19 15:39:52 +01001035 if (this->state != FL_PM_SUSPENDED && this->disable)
1036 this->disable(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001037 /* Release the chip */
1038 spin_lock(&this->chip_lock);
1039 this->state = FL_READY;
1040 wake_up(&this->wq);
1041 spin_unlock(&this->chip_lock);
1042}
1043
1044/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001045 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001046 * @param mtd MTD device structure
1047 * @param buf destination address
1048 * @param column oob offset to read from
1049 * @param thislen oob length to read
1050 */
1051static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
1052 int thislen)
1053{
1054 struct onenand_chip *this = mtd->priv;
Boris Brezillond30aae62016-02-03 20:12:31 +01001055 int ret;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001056
Boris Brezillond30aae62016-02-03 20:12:31 +01001057 this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
1058 mtd->oobsize);
1059 ret = mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
1060 column, thislen);
1061 if (ret)
1062 return ret;
1063
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001064 return 0;
1065}
1066
1067/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001068 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1069 * @param mtd MTD device structure
1070 * @param addr address to recover
1071 * @param status return value from onenand_wait / onenand_bbt_wait
1072 *
1073 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1074 * lower page address and MSB page has higher page address in paired pages.
1075 * If power off occurs during MSB page program, the paired LSB page data can
1076 * become corrupt. LSB page recovery read is a way to read LSB page though page
1077 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1078 * read after power up, issue LSB page recovery read.
1079 */
1080static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1081{
1082 struct onenand_chip *this = mtd->priv;
1083 int i;
1084
1085 /* Recovery is only for Flex-OneNAND */
1086 if (!FLEXONENAND(this))
1087 return status;
1088
1089 /* check if we failed due to uncorrectable error */
Brian Norrisd57f40542011-09-20 18:34:25 -07001090 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001091 return status;
1092
1093 /* check if address lies in MLC region */
1094 i = flexonenand_region(mtd, addr);
1095 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1096 return status;
1097
1098 /* We are attempting to reread, so decrement stats.failed
1099 * which was incremented by onenand_wait due to read failure
1100 */
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301101 printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1102 __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001103 mtd->ecc_stats.failed--;
1104
1105 /* Issue the LSB page recovery command */
1106 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1107 return this->wait(mtd, FL_READING);
1108}
1109
1110/**
1111 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1112 * @param mtd MTD device structure
1113 * @param from offset to read from
1114 * @param ops: oob operation description structure
1115 *
1116 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1117 * So, read-while-load is not present.
1118 */
1119static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1120 struct mtd_oob_ops *ops)
1121{
1122 struct onenand_chip *this = mtd->priv;
1123 struct mtd_ecc_stats stats;
1124 size_t len = ops->len;
1125 size_t ooblen = ops->ooblen;
1126 u_char *buf = ops->datbuf;
1127 u_char *oobbuf = ops->oobbuf;
1128 int read = 0, column, thislen;
1129 int oobread = 0, oobcolumn, thisooblen, oobsize;
1130 int ret = 0;
1131 int writesize = this->writesize;
1132
Brian Norris0a32a102011-07-19 10:06:10 -07001133 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1134 (int)len);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001135
Boris BREZILLON29f10582016-03-07 10:46:52 +01001136 oobsize = mtd_oobavail(mtd, ops);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001137 oobcolumn = from & (mtd->oobsize - 1);
1138
1139 /* Do not allow reads past end of device */
1140 if (from + len > mtd->size) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301141 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1142 __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001143 ops->retlen = 0;
1144 ops->oobretlen = 0;
1145 return -EINVAL;
1146 }
1147
1148 stats = mtd->ecc_stats;
1149
1150 while (read < len) {
1151 cond_resched();
1152
1153 thislen = min_t(int, writesize, len - read);
1154
1155 column = from & (writesize - 1);
1156 if (column + thislen > writesize)
1157 thislen = writesize - column;
1158
1159 if (!onenand_check_bufferram(mtd, from)) {
1160 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1161
1162 ret = this->wait(mtd, FL_READING);
1163 if (unlikely(ret))
1164 ret = onenand_recover_lsb(mtd, from, ret);
1165 onenand_update_bufferram(mtd, from, !ret);
Brian Norrisd57f40542011-09-20 18:34:25 -07001166 if (mtd_is_eccerr(ret))
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001167 ret = 0;
Adrian Hunterb0850582011-02-08 12:02:38 +02001168 if (ret)
1169 break;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001170 }
1171
1172 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1173 if (oobbuf) {
1174 thisooblen = oobsize - oobcolumn;
1175 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1176
Brian Norris0612b9d2011-08-30 18:45:40 -07001177 if (ops->mode == MTD_OPS_AUTO_OOB)
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001178 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1179 else
1180 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1181 oobread += thisooblen;
1182 oobbuf += thisooblen;
1183 oobcolumn = 0;
1184 }
1185
1186 read += thislen;
1187 if (read == len)
1188 break;
1189
1190 from += thislen;
1191 buf += thislen;
1192 }
1193
1194 /*
1195 * Return success, if no ECC failures, else -EBADMSG
1196 * fs driver will take care of that, because
1197 * retlen == desired len and result == -EBADMSG
1198 */
1199 ops->retlen = read;
1200 ops->oobretlen = oobread;
1201
1202 if (ret)
1203 return ret;
1204
1205 if (mtd->ecc_stats.failed - stats.failed)
1206 return -EBADMSG;
1207
Mike Dunnedbc45402012-04-25 12:06:11 -07001208 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1209 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001210}
1211
1212/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001213 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001214 * @param mtd MTD device structure
1215 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001216 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001217 *
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001218 * OneNAND read main and/or out-of-band data
1219 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001220static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001221 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001222{
1223 struct onenand_chip *this = mtd->priv;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001224 struct mtd_ecc_stats stats;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001225 size_t len = ops->len;
1226 size_t ooblen = ops->ooblen;
1227 u_char *buf = ops->datbuf;
1228 u_char *oobbuf = ops->oobbuf;
1229 int read = 0, column, thislen;
1230 int oobread = 0, oobcolumn, thisooblen, oobsize;
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001231 int ret = 0, boundary = 0;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001232 int writesize = this->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001233
Brian Norris0a32a102011-07-19 10:06:10 -07001234 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1235 (int)len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001236
Boris BREZILLON29f10582016-03-07 10:46:52 +01001237 oobsize = mtd_oobavail(mtd, ops);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001238 oobcolumn = from & (mtd->oobsize - 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001239
1240 /* Do not allow reads past end of device */
1241 if ((from + len) > mtd->size) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301242 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1243 __func__);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001244 ops->retlen = 0;
1245 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001246 return -EINVAL;
1247 }
1248
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001249 stats = mtd->ecc_stats;
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001250
Adrian Huntera8de85d2007-01-04 09:51:26 +02001251 /* Read-while-load method */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001252
Adrian Huntera8de85d2007-01-04 09:51:26 +02001253 /* Do first load to bufferRAM */
1254 if (read < len) {
1255 if (!onenand_check_bufferram(mtd, from)) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001256 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001257 ret = this->wait(mtd, FL_READING);
1258 onenand_update_bufferram(mtd, from, !ret);
Brian Norrisd57f40542011-09-20 18:34:25 -07001259 if (mtd_is_eccerr(ret))
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001260 ret = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001261 }
1262 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001263
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001264 thislen = min_t(int, writesize, len - read);
1265 column = from & (writesize - 1);
1266 if (column + thislen > writesize)
1267 thislen = writesize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001268
Adrian Huntera8de85d2007-01-04 09:51:26 +02001269 while (!ret) {
1270 /* If there is more to load then start next load */
1271 from += thislen;
1272 if (read + thislen < len) {
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001273 this->command(mtd, ONENAND_CMD_READ, from, writesize);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001274 /*
1275 * Chip boundary handling in DDP
1276 * Now we issued chip 1 read and pointed chip 1
Mika Korhonen492e1502009-06-09 21:52:35 +03001277 * bufferram so we have to point chip 0 bufferram.
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001278 */
Kyungmin Park738d61f2007-01-15 17:09:14 +09001279 if (ONENAND_IS_DDP(this) &&
1280 unlikely(from == (this->chipsize >> 1))) {
1281 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001282 boundary = 1;
1283 } else
1284 boundary = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001285 ONENAND_SET_PREV_BUFFERRAM(this);
1286 }
1287 /* While load is going, read from last bufferRAM */
1288 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001289
1290 /* Read oob area if needed */
1291 if (oobbuf) {
1292 thisooblen = oobsize - oobcolumn;
1293 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1294
Brian Norris0612b9d2011-08-30 18:45:40 -07001295 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001296 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1297 else
1298 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1299 oobread += thisooblen;
1300 oobbuf += thisooblen;
1301 oobcolumn = 0;
1302 }
1303
Adrian Huntera8de85d2007-01-04 09:51:26 +02001304 /* See if we are done */
1305 read += thislen;
1306 if (read == len)
1307 break;
1308 /* Set up for next read from bufferRAM */
Adrian Hunter0fc2cce2007-01-09 17:55:21 +02001309 if (unlikely(boundary))
Kyungmin Park738d61f2007-01-15 17:09:14 +09001310 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001311 ONENAND_SET_NEXT_BUFFERRAM(this);
1312 buf += thislen;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001313 thislen = min_t(int, writesize, len - read);
Adrian Huntera8de85d2007-01-04 09:51:26 +02001314 column = 0;
1315 cond_resched();
1316 /* Now wait for load */
1317 ret = this->wait(mtd, FL_READING);
1318 onenand_update_bufferram(mtd, from, !ret);
Brian Norrisd57f40542011-09-20 18:34:25 -07001319 if (mtd_is_eccerr(ret))
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001320 ret = 0;
Adrian Huntera8de85d2007-01-04 09:51:26 +02001321 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001322
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001323 /*
1324 * Return success, if no ECC failures, else -EBADMSG
1325 * fs driver will take care of that, because
1326 * retlen == desired len and result == -EBADMSG
1327 */
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001328 ops->retlen = read;
1329 ops->oobretlen = oobread;
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +09001330
Adrian Huntera8de85d2007-01-04 09:51:26 +02001331 if (ret)
1332 return ret;
1333
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001334 if (mtd->ecc_stats.failed - stats.failed)
1335 return -EBADMSG;
1336
Mike Dunnedbc45402012-04-25 12:06:11 -07001337 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1338 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001339}
1340
1341/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001342 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001343 * @param mtd MTD device structure
1344 * @param from offset to read from
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001345 * @param ops: oob operation description structure
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001346 *
1347 * OneNAND read out-of-band data from the spare area
1348 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001349static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
Kyungmin Park12f77c92007-08-30 09:36:05 +09001350 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001351{
1352 struct onenand_chip *this = mtd->priv;
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001353 struct mtd_ecc_stats stats;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001354 int read = 0, thislen, column, oobsize;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001355 size_t len = ops->ooblen;
Brian Norris905c6bc2011-08-30 18:45:39 -07001356 unsigned int mode = ops->mode;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001357 u_char *buf = ops->oobbuf;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001358 int ret = 0, readcmd;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001359
Kyungmin Park12f77c92007-08-30 09:36:05 +09001360 from += ops->ooboffs;
1361
Brian Norris0a32a102011-07-19 10:06:10 -07001362 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1363 (int)len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001364
1365 /* Initialize return length value */
Kyungmin Park12f77c92007-08-30 09:36:05 +09001366 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001367
Brian Norris0612b9d2011-08-30 18:45:40 -07001368 if (mode == MTD_OPS_AUTO_OOB)
Boris BREZILLONf5b8aa72016-03-07 10:46:51 +01001369 oobsize = mtd->oobavail;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001370 else
1371 oobsize = mtd->oobsize;
1372
1373 column = from & (mtd->oobsize - 1);
1374
1375 if (unlikely(column >= oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301376 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1377 __func__);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001378 return -EINVAL;
1379 }
1380
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001381 stats = mtd->ecc_stats;
1382
Kyungmin Park8a8f6322010-12-02 09:24:16 +09001383 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001384
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001385 while (read < len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09001386 cond_resched();
1387
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001388 thislen = oobsize - column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001389 thislen = min_t(int, thislen, len);
1390
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001391 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001392
1393 onenand_update_bufferram(mtd, from, 0);
1394
1395 ret = this->wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001396 if (unlikely(ret))
1397 ret = onenand_recover_lsb(mtd, from, ret);
1398
Brian Norrisd57f40542011-09-20 18:34:25 -07001399 if (ret && !mtd_is_eccerr(ret)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301400 printk(KERN_ERR "%s: read failed = 0x%x\n",
1401 __func__, ret);
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001402 break;
1403 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001404
Brian Norris0612b9d2011-08-30 18:45:40 -07001405 if (mode == MTD_OPS_AUTO_OOB)
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001406 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1407 else
1408 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001409
1410 read += thislen;
1411
1412 if (read == len)
1413 break;
1414
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001415 buf += thislen;
1416
1417 /* Read more? */
1418 if (read < len) {
1419 /* Page size */
Joern Engel28318772006-05-22 23:18:05 +02001420 from += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001421 column = 0;
1422 }
1423 }
1424
Kyungmin Park12f77c92007-08-30 09:36:05 +09001425 ops->oobretlen = read;
Adrian Hunter5f4d47d2007-11-06 09:17:25 +02001426
1427 if (ret)
1428 return ret;
1429
1430 if (mtd->ecc_stats.failed - stats.failed)
1431 return -EBADMSG;
1432
1433 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001434}
1435
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001436/**
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001437 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09001438 * @param mtd: MTD device structure
1439 * @param from: offset to read from
1440 * @param ops: oob operation description structure
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001441
1442 * Read main and/or out-of-band
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001443 */
1444static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1445 struct mtd_oob_ops *ops)
1446{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001447 struct onenand_chip *this = mtd->priv;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001448 int ret;
1449
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001450 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001451 case MTD_OPS_PLACE_OOB:
1452 case MTD_OPS_AUTO_OOB:
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001453 break;
Brian Norris0612b9d2011-08-30 18:45:40 -07001454 case MTD_OPS_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09001455 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001456 default:
1457 return -EINVAL;
1458 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001459
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001460 onenand_get_device(mtd, FL_READING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001461 if (ops->datbuf)
Kyungmin Park8a8f6322010-12-02 09:24:16 +09001462 ret = ONENAND_IS_4KB_PAGE(this) ?
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001463 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1464 onenand_read_ops_nolock(mtd, from, ops);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001465 else
1466 ret = onenand_read_oob_nolock(mtd, from, ops);
1467 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001468
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001469 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001470}
1471
Kyungmin Park211ac752007-02-07 12:15:01 +09001472/**
1473 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1474 * @param mtd MTD device structure
1475 * @param state state to select the max. timeout value
1476 *
1477 * Wait for command done.
1478 */
1479static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1480{
1481 struct onenand_chip *this = mtd->priv;
1482 unsigned long timeout;
Adrian Huntere0c1a922010-12-10 12:04:20 +02001483 unsigned int interrupt, ctrl, ecc, addr1, addr8;
Kyungmin Park211ac752007-02-07 12:15:01 +09001484
1485 /* The 20 msec is enough */
1486 timeout = jiffies + msecs_to_jiffies(20);
1487 while (time_before(jiffies, timeout)) {
1488 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1489 if (interrupt & ONENAND_INT_MASTER)
1490 break;
1491 }
1492 /* To get correct interrupt status in timeout case */
1493 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1494 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
Adrian Huntere0c1a922010-12-10 12:04:20 +02001495 addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
1496 addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
Kyungmin Park211ac752007-02-07 12:15:01 +09001497
Kyungmin Park211ac752007-02-07 12:15:01 +09001498 if (interrupt & ONENAND_INT_READ) {
Adrian Huntere0c1a922010-12-10 12:04:20 +02001499 ecc = onenand_read_ecc(this);
Kyungmin Park83973b82008-05-29 14:52:40 +09001500 if (ecc & ONENAND_ECC_2BIT_ALL) {
Adrian Huntere0c1a922010-12-10 12:04:20 +02001501 printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
1502 "intr 0x%04x addr1 %#x addr8 %#x\n",
1503 __func__, ecc, ctrl, interrupt, addr1, addr8);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001504 return ONENAND_BBT_READ_ECC_ERROR;
Kyungmin Park83973b82008-05-29 14:52:40 +09001505 }
Kyungmin Park211ac752007-02-07 12:15:01 +09001506 } else {
Adrian Huntere0c1a922010-12-10 12:04:20 +02001507 printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
1508 "intr 0x%04x addr1 %#x addr8 %#x\n",
1509 __func__, ctrl, interrupt, addr1, addr8);
Kyungmin Park211ac752007-02-07 12:15:01 +09001510 return ONENAND_BBT_READ_FATAL_ERROR;
1511 }
1512
Kyungmin Park83973b82008-05-29 14:52:40 +09001513 /* Initial bad block case: 0x2400 or 0x0400 */
1514 if (ctrl & ONENAND_CTRL_ERROR) {
Adrian Huntere0c1a922010-12-10 12:04:20 +02001515 printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1516 "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
Kyungmin Park83973b82008-05-29 14:52:40 +09001517 return ONENAND_BBT_READ_ERROR;
1518 }
1519
Kyungmin Park211ac752007-02-07 12:15:01 +09001520 return 0;
1521}
1522
1523/**
1524 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1525 * @param mtd MTD device structure
1526 * @param from offset to read from
Kyungmin Parke3da8062007-02-15 09:36:39 +09001527 * @param ops oob operation description structure
Kyungmin Park211ac752007-02-07 12:15:01 +09001528 *
1529 * OneNAND read out-of-band data from the spare area for bbt scan
1530 */
1531int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1532 struct mtd_oob_ops *ops)
1533{
1534 struct onenand_chip *this = mtd->priv;
1535 int read = 0, thislen, column;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001536 int ret = 0, readcmd;
Kyungmin Park211ac752007-02-07 12:15:01 +09001537 size_t len = ops->ooblen;
1538 u_char *buf = ops->oobbuf;
1539
Brian Norris0a32a102011-07-19 10:06:10 -07001540 pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
1541 len);
Kyungmin Park211ac752007-02-07 12:15:01 +09001542
1543 /* Initialize return value */
1544 ops->oobretlen = 0;
1545
1546 /* Do not allow reads past end of device */
1547 if (unlikely((from + len) > mtd->size)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301548 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1549 __func__);
Kyungmin Park211ac752007-02-07 12:15:01 +09001550 return ONENAND_BBT_READ_FATAL_ERROR;
1551 }
1552
1553 /* Grab the lock and see if the device is available */
1554 onenand_get_device(mtd, FL_READING);
1555
1556 column = from & (mtd->oobsize - 1);
1557
Kyungmin Park8a8f6322010-12-02 09:24:16 +09001558 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001559
Kyungmin Park211ac752007-02-07 12:15:01 +09001560 while (read < len) {
1561 cond_resched();
1562
1563 thislen = mtd->oobsize - column;
1564 thislen = min_t(int, thislen, len);
1565
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001566 this->command(mtd, readcmd, from, mtd->oobsize);
Kyungmin Park211ac752007-02-07 12:15:01 +09001567
1568 onenand_update_bufferram(mtd, from, 0);
1569
Kyungmin Park31bb9992009-05-12 13:46:57 -07001570 ret = this->bbt_wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001571 if (unlikely(ret))
1572 ret = onenand_recover_lsb(mtd, from, ret);
1573
Kyungmin Park211ac752007-02-07 12:15:01 +09001574 if (ret)
1575 break;
1576
1577 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1578 read += thislen;
1579 if (read == len)
1580 break;
1581
1582 buf += thislen;
1583
1584 /* Read more? */
1585 if (read < len) {
1586 /* Update Page size */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001587 from += this->writesize;
Kyungmin Park211ac752007-02-07 12:15:01 +09001588 column = 0;
1589 }
1590 }
1591
1592 /* Deselect and wake up anyone waiting on the device */
1593 onenand_release_device(mtd);
1594
1595 ops->oobretlen = read;
1596 return ret;
1597}
1598
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001599#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1600/**
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001601 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1602 * @param mtd MTD device structure
1603 * @param buf the databuffer to verify
1604 * @param to offset to read from
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001605 */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001606static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001607{
1608 struct onenand_chip *this = mtd->priv;
Kyungmin Park69d79182007-12-14 14:47:21 +09001609 u_char *oob_buf = this->oob_buf;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001610 int status, i, readcmd;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001611
Kyungmin Park8a8f6322010-12-02 09:24:16 +09001612 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001613
1614 this->command(mtd, readcmd, to, mtd->oobsize);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001615 onenand_update_bufferram(mtd, to, 0);
1616 status = this->wait(mtd, FL_READING);
1617 if (status)
1618 return status;
1619
Kyungmin Park69d79182007-12-14 14:47:21 +09001620 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
Kyungmin Park91014e92007-02-12 10:34:39 +09001621 for (i = 0; i < mtd->oobsize; i++)
Kyungmin Park69d79182007-12-14 14:47:21 +09001622 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001623 return -EBADMSG;
1624
1625 return 0;
1626}
1627
1628/**
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001629 * onenand_verify - [GENERIC] verify the chip contents after a write
1630 * @param mtd MTD device structure
1631 * @param buf the databuffer to verify
1632 * @param addr offset to read from
1633 * @param len number of bytes to read and compare
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001634 */
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001635static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001636{
1637 struct onenand_chip *this = mtd->priv;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001638 int ret = 0;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001639 int thislen, column;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001640
Roman Tereshonkove6da8562011-02-08 12:02:42 +02001641 column = addr & (this->writesize - 1);
1642
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001643 while (len != 0) {
Roman Tereshonkove6da8562011-02-08 12:02:42 +02001644 thislen = min_t(int, this->writesize - column, len);
Kyungmin Park60d84f92006-12-22 16:21:54 +09001645
Kyungmin Parkee9745f2007-06-30 13:57:49 +09001646 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001647
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001648 onenand_update_bufferram(mtd, addr, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001649
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001650 ret = this->wait(mtd, FL_READING);
1651 if (ret)
1652 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001653
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001654 onenand_update_bufferram(mtd, addr, 1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001655
Kyungmin Park3328dc32010-04-28 17:46:47 +02001656 this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001657
Roman Tereshonkove6da8562011-02-08 12:02:42 +02001658 if (memcmp(buf, this->verify_buf + column, thislen))
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001659 return -EBADMSG;
1660
1661 len -= thislen;
1662 buf += thislen;
1663 addr += thislen;
Roman Tereshonkove6da8562011-02-08 12:02:42 +02001664 column = 0;
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001665 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00001666
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001667 return 0;
1668}
1669#else
Adrian Hunter8b29c0b2007-01-25 14:06:33 +09001670#define onenand_verify(...) (0)
Kyungmin Park8e6ec692006-05-12 17:02:41 +03001671#define onenand_verify_oob(...) (0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001672#endif
1673
Kyungmin Park60d84f92006-12-22 16:21:54 +09001674#define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001675
Richard Purdie6c77fd642008-02-06 10:18:22 +00001676static void onenand_panic_wait(struct mtd_info *mtd)
1677{
1678 struct onenand_chip *this = mtd->priv;
1679 unsigned int interrupt;
1680 int i;
1681
1682 for (i = 0; i < 2000; i++) {
1683 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1684 if (interrupt & ONENAND_INT_MASTER)
1685 break;
1686 udelay(10);
1687 }
1688}
1689
1690/**
1691 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1692 * @param mtd MTD device structure
1693 * @param to offset to write to
1694 * @param len number of bytes to write
1695 * @param retlen pointer to variable to store the number of written bytes
1696 * @param buf the data to write
1697 *
1698 * Write with ECC
1699 */
1700static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1701 size_t *retlen, const u_char *buf)
1702{
1703 struct onenand_chip *this = mtd->priv;
1704 int column, subpage;
1705 int written = 0;
Richard Purdie6c77fd642008-02-06 10:18:22 +00001706
1707 if (this->state == FL_PM_SUSPENDED)
1708 return -EBUSY;
1709
1710 /* Wait for any existing operation to clear */
1711 onenand_panic_wait(mtd);
1712
Brian Norris0a32a102011-07-19 10:06:10 -07001713 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1714 (int)len);
Richard Purdie6c77fd642008-02-06 10:18:22 +00001715
Richard Purdie6c77fd642008-02-06 10:18:22 +00001716 /* Reject writes, which are not page aligned */
Roel Kluinb73d7e432008-02-16 18:14:35 +01001717 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301718 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1719 __func__);
Richard Purdie6c77fd642008-02-06 10:18:22 +00001720 return -EINVAL;
1721 }
1722
1723 column = to & (mtd->writesize - 1);
1724
1725 /* Loop until all data write */
1726 while (written < len) {
1727 int thislen = min_t(int, mtd->writesize - column, len - written);
1728 u_char *wbuf = (u_char *) buf;
1729
1730 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1731
1732 /* Partial page write */
1733 subpage = thislen < mtd->writesize;
1734 if (subpage) {
1735 memset(this->page_buf, 0xff, mtd->writesize);
1736 memcpy(this->page_buf + column, buf, thislen);
1737 wbuf = this->page_buf;
1738 }
1739
1740 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1741 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1742
1743 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1744
1745 onenand_panic_wait(mtd);
1746
1747 /* In partial page write we don't update bufferram */
Brian Norris7f2a7ce2015-02-28 02:02:25 -08001748 onenand_update_bufferram(mtd, to, !subpage);
Richard Purdie6c77fd642008-02-06 10:18:22 +00001749 if (ONENAND_IS_2PLANE(this)) {
1750 ONENAND_SET_BUFFERRAM1(this);
Brian Norris7f2a7ce2015-02-28 02:02:25 -08001751 onenand_update_bufferram(mtd, to + this->writesize, !subpage);
Richard Purdie6c77fd642008-02-06 10:18:22 +00001752 }
1753
1754 written += thislen;
1755
1756 if (written == len)
1757 break;
1758
1759 column = 0;
1760 to += thislen;
1761 buf += thislen;
1762 }
1763
1764 *retlen = written;
Brian Norris7f2a7ce2015-02-28 02:02:25 -08001765 return 0;
Richard Purdie6c77fd642008-02-06 10:18:22 +00001766}
1767
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001768/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001769 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001770 * @param mtd MTD device structure
1771 * @param oob_buf oob buffer
1772 * @param buf source address
1773 * @param column oob offset to write to
1774 * @param thislen oob length to write
1775 */
1776static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1777 const u_char *buf, int column, int thislen)
1778{
Boris Brezillond30aae62016-02-03 20:12:31 +01001779 return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001780}
1781
1782/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001783 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001784 * @param mtd MTD device structure
1785 * @param to offset to write to
1786 * @param ops oob operation description structure
1787 *
1788 * Write main and/or oob with ECC
1789 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001790static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001791 struct mtd_oob_ops *ops)
1792{
1793 struct onenand_chip *this = mtd->priv;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001794 int written = 0, column, thislen = 0, subpage = 0;
1795 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001796 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1797 size_t len = ops->len;
1798 size_t ooblen = ops->ooblen;
1799 const u_char *buf = ops->datbuf;
1800 const u_char *oob = ops->oobbuf;
1801 u_char *oobbuf;
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02001802 int ret = 0, cmd;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001803
Brian Norris0a32a102011-07-19 10:06:10 -07001804 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1805 (int)len);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001806
1807 /* Initialize retlen, in case of early exit */
1808 ops->retlen = 0;
1809 ops->oobretlen = 0;
1810
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001811 /* Reject writes, which are not page aligned */
Roel Kluinb73d7e432008-02-16 18:14:35 +01001812 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301813 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1814 __func__);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001815 return -EINVAL;
1816 }
1817
Kyungmin Park9ce96902008-11-17 17:54:28 +09001818 /* Check zero length */
1819 if (!len)
1820 return 0;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001821 oobsize = mtd_oobavail(mtd, ops);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001822 oobcolumn = to & (mtd->oobsize - 1);
1823
1824 column = to & (mtd->writesize - 1);
1825
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001826 /* Loop until all data write */
Kyungmin Park9ce96902008-11-17 17:54:28 +09001827 while (1) {
1828 if (written < len) {
1829 u_char *wbuf = (u_char *) buf;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001830
Kyungmin Park9ce96902008-11-17 17:54:28 +09001831 thislen = min_t(int, mtd->writesize - column, len - written);
1832 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001833
Kyungmin Park9ce96902008-11-17 17:54:28 +09001834 cond_resched();
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001835
Kyungmin Park9ce96902008-11-17 17:54:28 +09001836 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001837
Kyungmin Park9ce96902008-11-17 17:54:28 +09001838 /* Partial page write */
1839 subpage = thislen < mtd->writesize;
1840 if (subpage) {
1841 memset(this->page_buf, 0xff, mtd->writesize);
1842 memcpy(this->page_buf + column, buf, thislen);
1843 wbuf = this->page_buf;
1844 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001845
Kyungmin Park9ce96902008-11-17 17:54:28 +09001846 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001847
Kyungmin Park9ce96902008-11-17 17:54:28 +09001848 if (oob) {
1849 oobbuf = this->oob_buf;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001850
Kyungmin Park9ce96902008-11-17 17:54:28 +09001851 /* We send data to spare ram with oobsize
1852 * to prevent byte access */
1853 memset(oobbuf, 0xff, mtd->oobsize);
Brian Norris0612b9d2011-08-30 18:45:40 -07001854 if (ops->mode == MTD_OPS_AUTO_OOB)
Kyungmin Park9ce96902008-11-17 17:54:28 +09001855 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1856 else
1857 memcpy(oobbuf + oobcolumn, oob, thisooblen);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001858
Kyungmin Park9ce96902008-11-17 17:54:28 +09001859 oobwritten += thisooblen;
1860 oob += thisooblen;
1861 oobcolumn = 0;
1862 } else
1863 oobbuf = (u_char *) ffchars;
1864
1865 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001866 } else
Kyungmin Park9ce96902008-11-17 17:54:28 +09001867 ONENAND_SET_NEXT_BUFFERRAM(this);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001868
Kyungmin Park9ce96902008-11-17 17:54:28 +09001869 /*
Mika Korhonen492e1502009-06-09 21:52:35 +03001870 * 2 PLANE, MLC, and Flex-OneNAND do not support
1871 * write-while-program feature.
Kyungmin Park9ce96902008-11-17 17:54:28 +09001872 */
Kyungmin Park6a88c472010-04-28 17:46:45 +02001873 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
Kyungmin Park9ce96902008-11-17 17:54:28 +09001874 ONENAND_SET_PREV_BUFFERRAM(this);
1875
1876 ret = this->wait(mtd, FL_WRITING);
1877
1878 /* In partial page write we don't update bufferram */
1879 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1880 if (ret) {
1881 written -= prevlen;
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301882 printk(KERN_ERR "%s: write failed %d\n",
1883 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001884 break;
1885 }
1886
1887 if (written == len) {
1888 /* Only check verify write turn on */
1889 ret = onenand_verify(mtd, buf - len, to - len, len);
1890 if (ret)
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301891 printk(KERN_ERR "%s: verify failed %d\n",
1892 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001893 break;
1894 }
1895
1896 ONENAND_SET_NEXT_BUFFERRAM(this);
1897 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001898
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02001899 this->ongoing = 0;
1900 cmd = ONENAND_CMD_PROG;
1901
1902 /* Exclude 1st OTP and OTP blocks for cache program feature */
1903 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1904 likely(onenand_block(this, to) != 0) &&
1905 ONENAND_IS_4KB_PAGE(this) &&
1906 ((written + thislen) < len)) {
1907 cmd = ONENAND_CMD_2X_CACHE_PROG;
1908 this->ongoing = 1;
1909 }
1910
1911 this->command(mtd, cmd, to, mtd->writesize);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001912
Kyungmin Park9ce96902008-11-17 17:54:28 +09001913 /*
1914 * 2 PLANE, MLC, and Flex-OneNAND wait here
1915 */
Kyungmin Park6a88c472010-04-28 17:46:45 +02001916 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
Kyungmin Park9ce96902008-11-17 17:54:28 +09001917 ret = this->wait(mtd, FL_WRITING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001918
Kyungmin Park9ce96902008-11-17 17:54:28 +09001919 /* In partial page write we don't update bufferram */
1920 onenand_update_bufferram(mtd, to, !ret && !subpage);
1921 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301922 printk(KERN_ERR "%s: write failed %d\n",
1923 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001924 break;
1925 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001926
Kyungmin Park9ce96902008-11-17 17:54:28 +09001927 /* Only check verify write turn on */
1928 ret = onenand_verify(mtd, buf, to, thislen);
1929 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05301930 printk(KERN_ERR "%s: verify failed %d\n",
1931 __func__, ret);
Kyungmin Park9ce96902008-11-17 17:54:28 +09001932 break;
1933 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001934
Kyungmin Park9ce96902008-11-17 17:54:28 +09001935 written += thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001936
Kyungmin Park9ce96902008-11-17 17:54:28 +09001937 if (written == len)
1938 break;
1939
1940 } else
1941 written += thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001942
1943 column = 0;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001944 prev_subpage = subpage;
1945 prev = to;
1946 prevlen = thislen;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001947 to += thislen;
1948 buf += thislen;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001949 first = 0;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001950 }
1951
Kyungmin Park9ce96902008-11-17 17:54:28 +09001952 /* In error case, clear all bufferrams */
1953 if (written != len)
1954 onenand_invalidate_bufferram(mtd, 0, -1);
1955
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001956 ops->retlen = written;
Kyungmin Park9ce96902008-11-17 17:54:28 +09001957 ops->oobretlen = oobwritten;
Kyungmin Parkd15057b2007-09-06 10:06:12 +09001958
1959 return ret;
1960}
1961
1962
1963/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001964 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001965 * @param mtd MTD device structure
1966 * @param to offset to write to
1967 * @param len number of bytes to write
1968 * @param retlen pointer to variable to store the number of written bytes
1969 * @param buf the data to write
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001970 * @param mode operation mode
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001971 *
1972 * OneNAND write out-of-band
1973 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03001974static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1975 struct mtd_oob_ops *ops)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001976{
1977 struct onenand_chip *this = mtd->priv;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001978 int column, ret = 0, oobsize;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07001979 int written = 0, oobcmd;
Kyungmin Park91014e92007-02-12 10:34:39 +09001980 u_char *oobbuf;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001981 size_t len = ops->ooblen;
1982 const u_char *buf = ops->oobbuf;
Brian Norris905c6bc2011-08-30 18:45:39 -07001983 unsigned int mode = ops->mode;
Kyungmin Park12f77c92007-08-30 09:36:05 +09001984
1985 to += ops->ooboffs;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001986
Brian Norris0a32a102011-07-19 10:06:10 -07001987 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1988 (int)len);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001989
1990 /* Initialize retlen, in case of early exit */
Kyungmin Park12f77c92007-08-30 09:36:05 +09001991 ops->oobretlen = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01001992
Brian Norris0612b9d2011-08-30 18:45:40 -07001993 if (mode == MTD_OPS_AUTO_OOB)
Boris BREZILLONf5b8aa72016-03-07 10:46:51 +01001994 oobsize = mtd->oobavail;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02001995 else
1996 oobsize = mtd->oobsize;
1997
1998 column = to & (mtd->oobsize - 1);
1999
2000 if (unlikely(column >= oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302001 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2002 __func__);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002003 return -EINVAL;
2004 }
2005
Adrian Hunter52e42002007-02-06 09:15:39 +09002006 /* For compatibility with NAND: Do not allow write past end of page */
Kyungmin Park91014e92007-02-12 10:34:39 +09002007 if (unlikely(column + len > oobsize)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302008 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2009 __func__);
Adrian Hunter52e42002007-02-06 09:15:39 +09002010 return -EINVAL;
2011 }
2012
Kyungmin Park470bc842007-03-09 10:08:11 +09002013 oobbuf = this->oob_buf;
Kyungmin Park91014e92007-02-12 10:34:39 +09002014
Kyungmin Park8a8f6322010-12-02 09:24:16 +09002015 oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002016
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002017 /* Loop until all data write */
2018 while (written < len) {
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002019 int thislen = min_t(int, oobsize, len - written);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002020
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09002021 cond_resched();
2022
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002023 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2024
Kyungmin Park34c10602006-05-12 17:02:46 +03002025 /* We send data to spare ram with oobsize
2026 * to prevent byte access */
Kyungmin Park91014e92007-02-12 10:34:39 +09002027 memset(oobbuf, 0xff, mtd->oobsize);
Brian Norris0612b9d2011-08-30 18:45:40 -07002028 if (mode == MTD_OPS_AUTO_OOB)
Kyungmin Park91014e92007-02-12 10:34:39 +09002029 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002030 else
Kyungmin Park91014e92007-02-12 10:34:39 +09002031 memcpy(oobbuf + column, buf, thislen);
2032 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002033
Kyungmin Park8a8f6322010-12-02 09:24:16 +09002034 if (ONENAND_IS_4KB_PAGE(this)) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002035 /* Set main area of DataRAM to 0xff*/
2036 memset(this->page_buf, 0xff, mtd->writesize);
2037 this->write_bufferram(mtd, ONENAND_DATARAM,
2038 this->page_buf, 0, mtd->writesize);
2039 }
2040
2041 this->command(mtd, oobcmd, to, mtd->oobsize);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002042
2043 onenand_update_bufferram(mtd, to, 0);
Kyungmin Parkee9745f2007-06-30 13:57:49 +09002044 if (ONENAND_IS_2PLANE(this)) {
2045 ONENAND_SET_BUFFERRAM1(this);
2046 onenand_update_bufferram(mtd, to + this->writesize, 0);
2047 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002048
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002049 ret = this->wait(mtd, FL_WRITING);
2050 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302051 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09002052 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002053 }
2054
Kyungmin Park91014e92007-02-12 10:34:39 +09002055 ret = onenand_verify_oob(mtd, oobbuf, to);
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002056 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302057 printk(KERN_ERR "%s: verify failed %d\n",
2058 __func__, ret);
Kyungmin Park5b4246f2007-02-02 09:39:21 +09002059 break;
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002060 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002061
2062 written += thislen;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002063 if (written == len)
2064 break;
2065
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002066 to += mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002067 buf += thislen;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002068 column = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002069 }
2070
Kyungmin Park12f77c92007-08-30 09:36:05 +09002071 ops->oobretlen = written;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002072
Kyungmin Park8e6ec692006-05-12 17:02:41 +03002073 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002074}
2075
2076/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002077 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Kyungmin Parke3da8062007-02-15 09:36:39 +09002078 * @param mtd: MTD device structure
2079 * @param to: offset to write
2080 * @param ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002081 */
2082static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2083 struct mtd_oob_ops *ops)
2084{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002085 int ret;
2086
Kyungmin Park4f4fad22007-02-02 09:22:21 +09002087 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002088 case MTD_OPS_PLACE_OOB:
2089 case MTD_OPS_AUTO_OOB:
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002090 break;
Brian Norris0612b9d2011-08-30 18:45:40 -07002091 case MTD_OPS_RAW:
Kyungmin Park4f4fad22007-02-02 09:22:21 +09002092 /* Not implemented yet */
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02002093 default:
2094 return -EINVAL;
2095 }
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002096
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002097 onenand_get_device(mtd, FL_WRITING);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002098 if (ops->datbuf)
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002099 ret = onenand_write_ops_nolock(mtd, to, ops);
2100 else
2101 ret = onenand_write_oob_nolock(mtd, to, ops);
2102 onenand_release_device(mtd);
Kyungmin Parkd15057b2007-09-06 10:06:12 +09002103
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002104 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002105}
2106
2107/**
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002108 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002109 * @param mtd MTD device structure
2110 * @param ofs offset from device start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002111 * @param allowbbt 1, if its allowed to access the bbt area
2112 *
2113 * Check, if the block is bad. Either by reading the bad block table or
2114 * calling of the scan function.
2115 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002116static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002117{
2118 struct onenand_chip *this = mtd->priv;
2119 struct bbm_info *bbm = this->bbm;
2120
2121 /* Return info from the table */
2122 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2123}
2124
Mika Korhonen72073022009-10-23 07:50:43 +02002125
2126static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
2127 struct erase_info *instr)
2128{
2129 struct onenand_chip *this = mtd->priv;
2130 loff_t addr = instr->addr;
2131 int len = instr->len;
2132 unsigned int block_size = (1 << this->erase_shift);
2133 int ret = 0;
2134
2135 while (len) {
2136 this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
2137 ret = this->wait(mtd, FL_VERIFYING_ERASE);
2138 if (ret) {
2139 printk(KERN_ERR "%s: Failed verify, block %d\n",
2140 __func__, onenand_block(this, addr));
Mika Korhonen72073022009-10-23 07:50:43 +02002141 instr->fail_addr = addr;
2142 return -1;
2143 }
2144 len -= block_size;
2145 addr += block_size;
2146 }
2147 return 0;
2148}
2149
2150/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002151 * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
Mika Korhonen72073022009-10-23 07:50:43 +02002152 * @param mtd MTD device structure
2153 * @param instr erase instruction
2154 * @param region erase region
2155 *
2156 * Erase one or more blocks up to 64 block at a time
2157 */
2158static int onenand_multiblock_erase(struct mtd_info *mtd,
2159 struct erase_info *instr,
2160 unsigned int block_size)
2161{
2162 struct onenand_chip *this = mtd->priv;
2163 loff_t addr = instr->addr;
2164 int len = instr->len;
2165 int eb_count = 0;
2166 int ret = 0;
2167 int bdry_block = 0;
2168
Mika Korhonen72073022009-10-23 07:50:43 +02002169 if (ONENAND_IS_DDP(this)) {
2170 loff_t bdry_addr = this->chipsize >> 1;
2171 if (addr < bdry_addr && (addr + len) > bdry_addr)
2172 bdry_block = bdry_addr >> this->erase_shift;
2173 }
2174
2175 /* Pre-check bbs */
2176 while (len) {
2177 /* Check if we have a bad block, we do not erase bad blocks */
2178 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2179 printk(KERN_WARNING "%s: attempt to erase a bad block "
2180 "at addr 0x%012llx\n",
2181 __func__, (unsigned long long) addr);
Mika Korhonen72073022009-10-23 07:50:43 +02002182 return -EIO;
2183 }
2184 len -= block_size;
2185 addr += block_size;
2186 }
2187
2188 len = instr->len;
2189 addr = instr->addr;
2190
2191 /* loop over 64 eb batches */
2192 while (len) {
2193 struct erase_info verify_instr = *instr;
2194 int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
2195
2196 verify_instr.addr = addr;
2197 verify_instr.len = 0;
2198
2199 /* do not cross chip boundary */
2200 if (bdry_block) {
2201 int this_block = (addr >> this->erase_shift);
2202
2203 if (this_block < bdry_block) {
2204 max_eb_count = min(max_eb_count,
2205 (bdry_block - this_block));
2206 }
2207 }
2208
2209 eb_count = 0;
2210
2211 while (len > block_size && eb_count < (max_eb_count - 1)) {
2212 this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
2213 addr, block_size);
2214 onenand_invalidate_bufferram(mtd, addr, block_size);
2215
2216 ret = this->wait(mtd, FL_PREPARING_ERASE);
2217 if (ret) {
2218 printk(KERN_ERR "%s: Failed multiblock erase, "
2219 "block %d\n", __func__,
2220 onenand_block(this, addr));
Mika Korhonen72073022009-10-23 07:50:43 +02002221 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2222 return -EIO;
2223 }
2224
2225 len -= block_size;
2226 addr += block_size;
2227 eb_count++;
2228 }
2229
2230 /* last block of 64-eb series */
2231 cond_resched();
2232 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2233 onenand_invalidate_bufferram(mtd, addr, block_size);
2234
2235 ret = this->wait(mtd, FL_ERASING);
2236 /* Check if it is write protected */
2237 if (ret) {
2238 printk(KERN_ERR "%s: Failed erase, block %d\n",
2239 __func__, onenand_block(this, addr));
Mika Korhonen72073022009-10-23 07:50:43 +02002240 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2241 return -EIO;
2242 }
2243
2244 len -= block_size;
2245 addr += block_size;
2246 eb_count++;
2247
2248 /* verify */
2249 verify_instr.len = eb_count * block_size;
2250 if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
Mika Korhonen72073022009-10-23 07:50:43 +02002251 instr->fail_addr = verify_instr.fail_addr;
2252 return -EIO;
2253 }
2254
2255 }
2256 return 0;
2257}
2258
2259
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002260/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002261 * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002262 * @param mtd MTD device structure
2263 * @param instr erase instruction
Mika Korhonen73885ae2009-10-23 07:50:42 +02002264 * @param region erase region
2265 * @param block_size erase block size
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002266 *
Mika Korhonen73885ae2009-10-23 07:50:42 +02002267 * Erase one or more blocks one block at a time
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002268 */
Mika Korhonen73885ae2009-10-23 07:50:42 +02002269static int onenand_block_by_block_erase(struct mtd_info *mtd,
2270 struct erase_info *instr,
2271 struct mtd_erase_region_info *region,
2272 unsigned int block_size)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002273{
2274 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002275 loff_t addr = instr->addr;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002276 int len = instr->len;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002277 loff_t region_end = 0;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002278 int ret = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002279
Mika Korhonen73885ae2009-10-23 07:50:42 +02002280 if (region) {
2281 /* region is set for Flex-OneNAND */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002282 region_end = region->offset + region->erasesize * region->numblocks;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002283 }
2284
Mika Korhonen73885ae2009-10-23 07:50:42 +02002285 /* Loop through the blocks */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002286 while (len) {
Artem Bityutskiy61a7e192006-12-26 16:41:24 +09002287 cond_resched();
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002288
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002289 /* Check if we have a bad block, we do not erase bad blocks */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002290 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302291 printk(KERN_WARNING "%s: attempt to erase a bad block "
2292 "at addr 0x%012llx\n",
2293 __func__, (unsigned long long) addr);
Mika Korhonen73885ae2009-10-23 07:50:42 +02002294 return -EIO;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002295 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002296
2297 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2298
Adrian Hunter480b9df2007-02-07 13:55:19 +02002299 onenand_invalidate_bufferram(mtd, addr, block_size);
2300
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002301 ret = this->wait(mtd, FL_ERASING);
2302 /* Check, if it is write protected */
2303 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302304 printk(KERN_ERR "%s: Failed erase, block %d\n",
2305 __func__, onenand_block(this, addr));
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002306 instr->fail_addr = addr;
Mika Korhonen73885ae2009-10-23 07:50:42 +02002307 return -EIO;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002308 }
2309
2310 len -= block_size;
2311 addr += block_size;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002312
john.maxin@nokia.comeff3bba2011-05-06 09:17:21 +00002313 if (region && addr == region_end) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002314 if (!len)
2315 break;
2316 region++;
2317
2318 block_size = region->erasesize;
2319 region_end = region->offset + region->erasesize * region->numblocks;
2320
2321 if (len & (block_size - 1)) {
2322 /* FIXME: This should be handled at MTD partitioning level. */
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302323 printk(KERN_ERR "%s: Unaligned address\n",
2324 __func__);
Mika Korhonen73885ae2009-10-23 07:50:42 +02002325 return -EIO;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002326 }
2327 }
Mika Korhonen73885ae2009-10-23 07:50:42 +02002328 }
2329 return 0;
2330}
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002331
Mika Korhonen73885ae2009-10-23 07:50:42 +02002332/**
2333 * onenand_erase - [MTD Interface] erase block(s)
2334 * @param mtd MTD device structure
2335 * @param instr erase instruction
2336 *
2337 * Erase one or more blocks
2338 */
2339static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2340{
2341 struct onenand_chip *this = mtd->priv;
2342 unsigned int block_size;
2343 loff_t addr = instr->addr;
2344 loff_t len = instr->len;
2345 int ret = 0;
2346 struct mtd_erase_region_info *region = NULL;
2347 loff_t region_offset = 0;
2348
Brian Norris289c0522011-07-19 10:06:09 -07002349 pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
Brian Norris0a32a102011-07-19 10:06:10 -07002350 (unsigned long long)instr->addr,
2351 (unsigned long long)instr->len);
Mika Korhonen73885ae2009-10-23 07:50:42 +02002352
Mika Korhonen73885ae2009-10-23 07:50:42 +02002353 if (FLEXONENAND(this)) {
2354 /* Find the eraseregion of this address */
2355 int i = flexonenand_region(mtd, addr);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002356
Mika Korhonen73885ae2009-10-23 07:50:42 +02002357 region = &mtd->eraseregions[i];
2358 block_size = region->erasesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002359
Mika Korhonen73885ae2009-10-23 07:50:42 +02002360 /* Start address within region must align on block boundary.
2361 * Erase region's start offset is always block start address.
2362 */
2363 region_offset = region->offset;
2364 } else
2365 block_size = 1 << this->erase_shift;
2366
2367 /* Start address must align on block boundary */
2368 if (unlikely((addr - region_offset) & (block_size - 1))) {
2369 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2370 return -EINVAL;
2371 }
2372
2373 /* Length must align on block boundary */
2374 if (unlikely(len & (block_size - 1))) {
2375 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2376 return -EINVAL;
2377 }
2378
Mika Korhonen73885ae2009-10-23 07:50:42 +02002379 /* Grab the lock and see if the device is available */
2380 onenand_get_device(mtd, FL_ERASING);
2381
Kyungmin Parkd983c542010-12-06 09:05:18 +09002382 if (ONENAND_IS_4KB_PAGE(this) || region ||
2383 instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
Mika Korhonen72073022009-10-23 07:50:43 +02002384 /* region is set for Flex-OneNAND (no mb erase) */
2385 ret = onenand_block_by_block_erase(mtd, instr,
2386 region, block_size);
2387 } else {
2388 ret = onenand_multiblock_erase(mtd, instr, block_size);
2389 }
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002390
2391 /* Deselect and wake up anyone waiting on the device */
2392 onenand_release_device(mtd);
2393
2394 return ret;
2395}
2396
2397/**
2398 * onenand_sync - [MTD Interface] sync
2399 * @param mtd MTD device structure
2400 *
2401 * Sync is actually a wait for chip ready function
2402 */
2403static void onenand_sync(struct mtd_info *mtd)
2404{
Brian Norris289c0522011-07-19 10:06:09 -07002405 pr_debug("%s: called\n", __func__);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002406
2407 /* Grab the lock and see if the device is available */
2408 onenand_get_device(mtd, FL_SYNCING);
2409
2410 /* Release it and go back */
2411 onenand_release_device(mtd);
2412}
2413
2414/**
2415 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2416 * @param mtd MTD device structure
2417 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002418 *
2419 * Check whether the block is bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002420 */
2421static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2422{
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002423 int ret;
2424
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002425 onenand_get_device(mtd, FL_READING);
2426 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2427 onenand_release_device(mtd);
2428 return ret;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002429}
2430
2431/**
2432 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2433 * @param mtd MTD device structure
2434 * @param ofs offset from device start
2435 *
2436 * This is the default implementation, which can be overridden by
2437 * a hardware specific driver.
2438 */
2439static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2440{
2441 struct onenand_chip *this = mtd->priv;
2442 struct bbm_info *bbm = this->bbm;
2443 u_char buf[2] = {0, 0};
Kyungmin Park12f77c92007-08-30 09:36:05 +09002444 struct mtd_oob_ops ops = {
Brian Norris0612b9d2011-08-30 18:45:40 -07002445 .mode = MTD_OPS_PLACE_OOB,
Kyungmin Park12f77c92007-08-30 09:36:05 +09002446 .ooblen = 2,
2447 .oobbuf = buf,
2448 .ooboffs = 0,
2449 };
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002450 int block;
2451
2452 /* Get block number */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002453 block = onenand_block(this, ofs);
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002454 if (bbm->bbt)
2455 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2456
Mika Korhonen492e1502009-06-09 21:52:35 +03002457 /* We write two bytes, so we don't have to mess with 16-bit access */
Frieder Schrempfbfd15c92019-04-17 12:36:35 +00002458 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002459 /* FIXME : What to do when marking SLC block in partition
2460 * with MLC erasesize? For now, it is not advisable to
2461 * create partitions containing both SLC and MLC regions.
2462 */
2463 return onenand_write_oob_nolock(mtd, ofs, &ops);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002464}
2465
2466/**
2467 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2468 * @param mtd MTD device structure
2469 * @param ofs offset relative to mtd start
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002470 *
2471 * Mark the block as bad
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002472 */
2473static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2474{
Aaro Koskinen5e64c292016-02-20 22:27:48 +02002475 struct onenand_chip *this = mtd->priv;
Kyungmin Parkcdc00132005-09-03 07:15:48 +01002476 int ret;
2477
2478 ret = onenand_block_isbad(mtd, ofs);
2479 if (ret) {
2480 /* If it was bad already, return success and do nothing */
2481 if (ret > 0)
2482 return 0;
2483 return ret;
2484 }
2485
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002486 onenand_get_device(mtd, FL_WRITING);
Aaro Koskinen5e64c292016-02-20 22:27:48 +02002487 ret = this->block_markbad(mtd, ofs);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002488 onenand_release_device(mtd);
2489 return ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002490}
2491
2492/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09002493 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002494 * @param mtd MTD device structure
2495 * @param ofs offset relative to mtd start
Kyungmin Park08f782b2006-11-16 11:29:39 +09002496 * @param len number of bytes to lock or unlock
Kyungmin Parke3da8062007-02-15 09:36:39 +09002497 * @param cmd lock or unlock command
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002498 *
Kyungmin Park08f782b2006-11-16 11:29:39 +09002499 * Lock or unlock one or more blocks
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002500 */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002501static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002502{
2503 struct onenand_chip *this = mtd->priv;
2504 int start, end, block, value, status;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002505 int wp_status_mask;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002506
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002507 start = onenand_block(this, ofs);
2508 end = onenand_block(this, ofs + len) - 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002509
Kyungmin Park08f782b2006-11-16 11:29:39 +09002510 if (cmd == ONENAND_CMD_LOCK)
2511 wp_status_mask = ONENAND_WP_LS;
2512 else
2513 wp_status_mask = ONENAND_WP_US;
2514
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002515 /* Continuous lock scheme */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002516 if (this->options & ONENAND_HAS_CONT_LOCK) {
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002517 /* Set start block address */
2518 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2519 /* Set end block address */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002520 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002521 /* Write lock command */
2522 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002523
2524 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002525 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002526
2527 /* Sanity check */
2528 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2529 & ONENAND_CTRL_ONGO)
2530 continue;
2531
2532 /* Check lock status */
2533 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002534 if (!(status & wp_status_mask))
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302535 printk(KERN_ERR "%s: wp status = 0x%x\n",
2536 __func__, status);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002537
2538 return 0;
2539 }
2540
2541 /* Block lock scheme */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002542 for (block = start; block < end + 1; block++) {
Kyungmin Park20ba89a2005-12-16 11:17:29 +09002543 /* Set block address */
2544 value = onenand_block_address(this, block);
2545 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2546 /* Select DataRAM for DDP */
2547 value = onenand_bufferram_address(this, block);
2548 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002549 /* Set start block address */
2550 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002551 /* Write lock command */
2552 this->command(mtd, cmd, 0, 0);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002553
2554 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002555 this->wait(mtd, FL_LOCKING);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002556
2557 /* Sanity check */
2558 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2559 & ONENAND_CTRL_ONGO)
2560 continue;
2561
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002562 /* Check lock status */
2563 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park08f782b2006-11-16 11:29:39 +09002564 if (!(status & wp_status_mask))
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302565 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2566 __func__, block, status);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002567 }
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00002568
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01002569 return 0;
2570}
2571
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002572/**
Kyungmin Park08f782b2006-11-16 11:29:39 +09002573 * onenand_lock - [MTD Interface] Lock block(s)
2574 * @param mtd MTD device structure
2575 * @param ofs offset relative to mtd start
2576 * @param len number of bytes to unlock
2577 *
2578 * Lock one or more blocks
2579 */
Adrian Hunter69423d92008-12-10 13:37:21 +00002580static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Park08f782b2006-11-16 11:29:39 +09002581{
Adrian Hunter34627f02007-10-12 10:19:26 +03002582 int ret;
2583
2584 onenand_get_device(mtd, FL_LOCKING);
2585 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2586 onenand_release_device(mtd);
2587 return ret;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002588}
2589
Kyungmin Park08f782b2006-11-16 11:29:39 +09002590/**
2591 * onenand_unlock - [MTD Interface] Unlock block(s)
2592 * @param mtd MTD device structure
2593 * @param ofs offset relative to mtd start
2594 * @param len number of bytes to unlock
2595 *
2596 * Unlock one or more blocks
2597 */
Adrian Hunter69423d92008-12-10 13:37:21 +00002598static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Park08f782b2006-11-16 11:29:39 +09002599{
Adrian Hunter34627f02007-10-12 10:19:26 +03002600 int ret;
2601
2602 onenand_get_device(mtd, FL_LOCKING);
2603 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2604 onenand_release_device(mtd);
2605 return ret;
Kyungmin Park08f782b2006-11-16 11:29:39 +09002606}
2607
2608/**
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002609 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2610 * @param this onenand chip data structure
2611 *
2612 * Check lock status
2613 */
Kyungmin Park66a10502008-02-13 15:55:38 +09002614static int onenand_check_lock_status(struct onenand_chip *this)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002615{
2616 unsigned int value, block, status;
2617 unsigned int end;
2618
2619 end = this->chipsize >> this->erase_shift;
2620 for (block = 0; block < end; block++) {
2621 /* Set block address */
2622 value = onenand_block_address(this, block);
2623 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2624 /* Select DataRAM for DDP */
2625 value = onenand_bufferram_address(this, block);
2626 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2627 /* Set start block address */
2628 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2629
2630 /* Check lock status */
2631 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
Kyungmin Park66a10502008-02-13 15:55:38 +09002632 if (!(status & ONENAND_WP_US)) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05302633 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2634 __func__, block, status);
Kyungmin Park66a10502008-02-13 15:55:38 +09002635 return 0;
2636 }
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002637 }
Kyungmin Park66a10502008-02-13 15:55:38 +09002638
2639 return 1;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002640}
2641
2642/**
2643 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2644 * @param mtd MTD device structure
2645 *
2646 * Unlock all blocks
2647 */
Kyungmin Park66a10502008-02-13 15:55:38 +09002648static void onenand_unlock_all(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002649{
2650 struct onenand_chip *this = mtd->priv;
Kyungmin Park66a10502008-02-13 15:55:38 +09002651 loff_t ofs = 0;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002652 loff_t len = mtd->size;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002653
2654 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
Kyungmin Park10b7a2b2007-01-12 05:45:34 +09002655 /* Set start block address */
2656 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002657 /* Write unlock command */
2658 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2659
2660 /* There's no return value */
Kyungmin Park08f782b2006-11-16 11:29:39 +09002661 this->wait(mtd, FL_LOCKING);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002662
2663 /* Sanity check */
2664 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2665 & ONENAND_CTRL_ONGO)
2666 continue;
2667
Kyungmin Park31bb9992009-05-12 13:46:57 -07002668 /* Don't check lock status */
2669 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2670 return;
2671
Kyungmin Park66a10502008-02-13 15:55:38 +09002672 /* Check lock status */
2673 if (onenand_check_lock_status(this))
2674 return;
2675
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002676 /* Workaround for all block unlock in DDP */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002677 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
Kyungmin Park66a10502008-02-13 15:55:38 +09002678 /* All blocks on another chip */
2679 ofs = this->chipsize >> 1;
2680 len = this->chipsize >> 1;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002681 }
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002682 }
2683
Kyungmin Park66a10502008-02-13 15:55:38 +09002684 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00002685}
2686
Kyungmin Park493c6462006-05-12 17:03:07 +03002687#ifdef CONFIG_MTD_ONENAND_OTP
2688
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302689/**
2690 * onenand_otp_command - Send OTP specific command to OneNAND device
2691 * @param mtd MTD device structure
2692 * @param cmd the command to be sent
2693 * @param addr offset to read from or write to
2694 * @param len number of bytes to read or write
2695 */
2696static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2697 size_t len)
2698{
2699 struct onenand_chip *this = mtd->priv;
2700 int value, block, page;
2701
2702 /* Address translation */
2703 switch (cmd) {
2704 case ONENAND_CMD_OTP_ACCESS:
2705 block = (int) (addr >> this->erase_shift);
2706 page = -1;
2707 break;
2708
2709 default:
2710 block = (int) (addr >> this->erase_shift);
2711 page = (int) (addr >> this->page_shift);
2712
2713 if (ONENAND_IS_2PLANE(this)) {
2714 /* Make the even block number */
2715 block &= ~1;
2716 /* Is it the odd plane? */
2717 if (addr & this->writesize)
2718 block++;
2719 page >>= 1;
2720 }
2721 page &= this->page_mask;
2722 break;
2723 }
2724
2725 if (block != -1) {
2726 /* Write 'DFS, FBA' of Flash */
2727 value = onenand_block_address(this, block);
2728 this->write_word(value, this->base +
2729 ONENAND_REG_START_ADDRESS1);
2730 }
2731
2732 if (page != -1) {
2733 /* Now we use page size operation */
2734 int sectors = 4, count = 4;
2735 int dataram;
2736
2737 switch (cmd) {
2738 default:
2739 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2740 cmd = ONENAND_CMD_2X_PROG;
2741 dataram = ONENAND_CURRENT_BUFFERRAM(this);
2742 break;
2743 }
2744
2745 /* Write 'FPA, FSA' of Flash */
2746 value = onenand_page_address(page, sectors);
2747 this->write_word(value, this->base +
2748 ONENAND_REG_START_ADDRESS8);
2749
2750 /* Write 'BSA, BSC' of DataRAM */
2751 value = onenand_buffer_address(dataram, sectors, count);
2752 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2753 }
2754
2755 /* Interrupt clear */
2756 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2757
2758 /* Write command */
2759 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2760
2761 return 0;
2762}
2763
2764/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002765 * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302766 * @param mtd MTD device structure
2767 * @param to offset to write to
2768 * @param len number of bytes to write
2769 * @param retlen pointer to variable to store the number of written bytes
2770 * @param buf the data to write
2771 *
2772 * OneNAND write out-of-band only for OTP
2773 */
2774static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2775 struct mtd_oob_ops *ops)
2776{
2777 struct onenand_chip *this = mtd->priv;
2778 int column, ret = 0, oobsize;
2779 int written = 0;
2780 u_char *oobbuf;
2781 size_t len = ops->ooblen;
2782 const u_char *buf = ops->oobbuf;
2783 int block, value, status;
2784
2785 to += ops->ooboffs;
2786
2787 /* Initialize retlen, in case of early exit */
2788 ops->oobretlen = 0;
2789
2790 oobsize = mtd->oobsize;
2791
2792 column = to & (mtd->oobsize - 1);
2793
2794 oobbuf = this->oob_buf;
2795
2796 /* Loop until all data write */
2797 while (written < len) {
2798 int thislen = min_t(int, oobsize, len - written);
2799
2800 cond_resched();
2801
2802 block = (int) (to >> this->erase_shift);
2803 /*
2804 * Write 'DFS, FBA' of Flash
2805 * Add: F100h DQ=DFS, FBA
2806 */
2807
2808 value = onenand_block_address(this, block);
2809 this->write_word(value, this->base +
2810 ONENAND_REG_START_ADDRESS1);
2811
2812 /*
2813 * Select DataRAM for DDP
2814 * Add: F101h DQ=DBS
2815 */
2816
2817 value = onenand_bufferram_address(this, block);
2818 this->write_word(value, this->base +
2819 ONENAND_REG_START_ADDRESS2);
2820 ONENAND_SET_NEXT_BUFFERRAM(this);
2821
2822 /*
2823 * Enter OTP access mode
2824 */
2825 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2826 this->wait(mtd, FL_OTPING);
2827
2828 /* We send data to spare ram with oobsize
2829 * to prevent byte access */
2830 memcpy(oobbuf + column, buf, thislen);
2831
2832 /*
2833 * Write Data into DataRAM
2834 * Add: 8th Word
2835 * in sector0/spare/page0
2836 * DQ=XXFCh
2837 */
2838 this->write_bufferram(mtd, ONENAND_SPARERAM,
2839 oobbuf, 0, mtd->oobsize);
2840
2841 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2842 onenand_update_bufferram(mtd, to, 0);
2843 if (ONENAND_IS_2PLANE(this)) {
2844 ONENAND_SET_BUFFERRAM1(this);
2845 onenand_update_bufferram(mtd, to + this->writesize, 0);
2846 }
2847
2848 ret = this->wait(mtd, FL_WRITING);
2849 if (ret) {
2850 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2851 break;
2852 }
2853
2854 /* Exit OTP access mode */
2855 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2856 this->wait(mtd, FL_RESETING);
2857
2858 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2859 status &= 0x60;
2860
2861 if (status == 0x60) {
2862 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2863 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2864 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2865 } else if (status == 0x20) {
2866 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2867 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2868 printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
2869 } else if (status == 0x40) {
2870 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2871 printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
2872 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2873 } else {
2874 printk(KERN_DEBUG "Reboot to check\n");
2875 }
2876
2877 written += thislen;
2878 if (written == len)
2879 break;
2880
2881 to += mtd->writesize;
2882 buf += thislen;
2883 column = 0;
2884 }
2885
2886 ops->oobretlen = written;
2887
2888 return ret;
2889}
2890
Mika Korhonen492e1502009-06-09 21:52:35 +03002891/* Internal OTP operation */
Kyungmin Park493c6462006-05-12 17:03:07 +03002892typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2893 size_t *retlen, u_char *buf);
2894
2895/**
2896 * do_otp_read - [DEFAULT] Read OTP block area
2897 * @param mtd MTD device structure
2898 * @param from The offset to read
2899 * @param len number of bytes to read
2900 * @param retlen pointer to variable to store the number of readbytes
2901 * @param buf the databuffer to put/get data
2902 *
2903 * Read OTP block area.
2904 */
2905static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2906 size_t *retlen, u_char *buf)
2907{
2908 struct onenand_chip *this = mtd->priv;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002909 struct mtd_oob_ops ops = {
2910 .len = len,
2911 .ooblen = 0,
2912 .datbuf = buf,
2913 .oobbuf = NULL,
2914 };
Kyungmin Park493c6462006-05-12 17:03:07 +03002915 int ret;
2916
2917 /* Enter OTP access mode */
2918 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2919 this->wait(mtd, FL_OTPING);
2920
Kyungmin Park8a8f6322010-12-02 09:24:16 +09002921 ret = ONENAND_IS_4KB_PAGE(this) ?
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002922 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2923 onenand_read_ops_nolock(mtd, from, &ops);
Kyungmin Park493c6462006-05-12 17:03:07 +03002924
2925 /* Exit OTP access mode */
2926 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2927 this->wait(mtd, FL_RESETING);
2928
2929 return ret;
2930}
2931
2932/**
2933 * do_otp_write - [DEFAULT] Write OTP block area
2934 * @param mtd MTD device structure
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002935 * @param to The offset to write
Kyungmin Park493c6462006-05-12 17:03:07 +03002936 * @param len number of bytes to write
2937 * @param retlen pointer to variable to store the number of write bytes
2938 * @param buf the databuffer to put/get data
2939 *
2940 * Write OTP block area.
2941 */
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002942static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
Kyungmin Park493c6462006-05-12 17:03:07 +03002943 size_t *retlen, u_char *buf)
2944{
2945 struct onenand_chip *this = mtd->priv;
2946 unsigned char *pbuf = buf;
2947 int ret;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002948 struct mtd_oob_ops ops;
Kyungmin Park493c6462006-05-12 17:03:07 +03002949
2950 /* Force buffer page aligned */
Joern Engel28318772006-05-22 23:18:05 +02002951 if (len < mtd->writesize) {
Kyungmin Park493c6462006-05-12 17:03:07 +03002952 memcpy(this->page_buf, buf, len);
Joern Engel28318772006-05-22 23:18:05 +02002953 memset(this->page_buf + len, 0xff, mtd->writesize - len);
Kyungmin Park493c6462006-05-12 17:03:07 +03002954 pbuf = this->page_buf;
Joern Engel28318772006-05-22 23:18:05 +02002955 len = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03002956 }
2957
2958 /* Enter OTP access mode */
2959 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2960 this->wait(mtd, FL_OTPING);
2961
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002962 ops.len = len;
2963 ops.ooblen = 0;
Kyungmin Park14370852007-10-10 13:48:14 +09002964 ops.datbuf = pbuf;
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03002965 ops.oobbuf = NULL;
2966 ret = onenand_write_ops_nolock(mtd, to, &ops);
2967 *retlen = ops.retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03002968
2969 /* Exit OTP access mode */
2970 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2971 this->wait(mtd, FL_RESETING);
2972
2973 return ret;
2974}
2975
2976/**
2977 * do_otp_lock - [DEFAULT] Lock OTP block area
2978 * @param mtd MTD device structure
2979 * @param from The offset to lock
2980 * @param len number of bytes to lock
2981 * @param retlen pointer to variable to store the number of lock bytes
2982 * @param buf the databuffer to put/get data
2983 *
2984 * Lock OTP block area.
2985 */
2986static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2987 size_t *retlen, u_char *buf)
2988{
2989 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002990 struct mtd_oob_ops ops;
Kyungmin Park493c6462006-05-12 17:03:07 +03002991 int ret;
2992
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002993 if (FLEXONENAND(this)) {
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05302994
2995 /* Enter OTP access mode */
2996 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2997 this->wait(mtd, FL_OTPING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07002998 /*
2999 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3000 * main area of page 49.
3001 */
3002 ops.len = mtd->writesize;
3003 ops.ooblen = 0;
3004 ops.datbuf = buf;
3005 ops.oobbuf = NULL;
3006 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
3007 *retlen = ops.retlen;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303008
3009 /* Exit OTP access mode */
3010 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3011 this->wait(mtd, FL_RESETING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003012 } else {
Brian Norris0612b9d2011-08-30 18:45:40 -07003013 ops.mode = MTD_OPS_PLACE_OOB;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003014 ops.ooblen = len;
3015 ops.oobbuf = buf;
3016 ops.ooboffs = 0;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303017 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003018 *retlen = ops.oobretlen;
3019 }
Kyungmin Park493c6462006-05-12 17:03:07 +03003020
Kyungmin Park493c6462006-05-12 17:03:07 +03003021 return ret;
3022}
3023
3024/**
3025 * onenand_otp_walk - [DEFAULT] Handle OTP operation
3026 * @param mtd MTD device structure
3027 * @param from The offset to read/write
3028 * @param len number of bytes to read/write
3029 * @param retlen pointer to variable to store the number of read bytes
3030 * @param buf the databuffer to put/get data
3031 * @param action do given action
3032 * @param mode specify user and factory
3033 *
3034 * Handle OTP operation.
3035 */
3036static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
3037 size_t *retlen, u_char *buf,
3038 otp_op_t action, int mode)
3039{
3040 struct onenand_chip *this = mtd->priv;
3041 int otp_pages;
3042 int density;
3043 int ret = 0;
3044
3045 *retlen = 0;
3046
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003047 density = onenand_get_density(this->device_id);
Kyungmin Park493c6462006-05-12 17:03:07 +03003048 if (density < ONENAND_DEVICE_DENSITY_512Mb)
3049 otp_pages = 20;
3050 else
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303051 otp_pages = 50;
Kyungmin Park493c6462006-05-12 17:03:07 +03003052
3053 if (mode == MTD_OTP_FACTORY) {
Joern Engel28318772006-05-22 23:18:05 +02003054 from += mtd->writesize * otp_pages;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303055 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
Kyungmin Park493c6462006-05-12 17:03:07 +03003056 }
3057
3058 /* Check User/Factory boundary */
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303059 if (mode == MTD_OTP_USER) {
Roel Kluin0a032a42009-12-16 01:37:17 +01003060 if (mtd->writesize * otp_pages < from + len)
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303061 return 0;
3062 } else {
Roel Kluin0a032a42009-12-16 01:37:17 +01003063 if (mtd->writesize * otp_pages < len)
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303064 return 0;
3065 }
Kyungmin Park493c6462006-05-12 17:03:07 +03003066
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003067 onenand_get_device(mtd, FL_OTPING);
Kyungmin Park493c6462006-05-12 17:03:07 +03003068 while (len > 0 && otp_pages > 0) {
3069 if (!action) { /* OTP Info functions */
3070 struct otp_info *otpinfo;
3071
3072 len -= sizeof(struct otp_info);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003073 if (len <= 0) {
3074 ret = -ENOSPC;
3075 break;
3076 }
Kyungmin Park493c6462006-05-12 17:03:07 +03003077
3078 otpinfo = (struct otp_info *) buf;
3079 otpinfo->start = from;
Joern Engel28318772006-05-22 23:18:05 +02003080 otpinfo->length = mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03003081 otpinfo->locked = 0;
3082
Joern Engel28318772006-05-22 23:18:05 +02003083 from += mtd->writesize;
Kyungmin Park493c6462006-05-12 17:03:07 +03003084 buf += sizeof(struct otp_info);
3085 *retlen += sizeof(struct otp_info);
3086 } else {
3087 size_t tmp_retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03003088
3089 ret = action(mtd, from, len, &tmp_retlen, buf);
Dan Carpenterc3cb77f2016-04-13 09:42:34 +03003090 if (ret)
3091 break;
Kyungmin Park493c6462006-05-12 17:03:07 +03003092
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303093 buf += tmp_retlen;
3094 len -= tmp_retlen;
3095 *retlen += tmp_retlen;
Kyungmin Park493c6462006-05-12 17:03:07 +03003096
Kyungmin Park493c6462006-05-12 17:03:07 +03003097 }
3098 otp_pages--;
3099 }
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003100 onenand_release_device(mtd);
Kyungmin Park493c6462006-05-12 17:03:07 +03003101
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003102 return ret;
Kyungmin Park493c6462006-05-12 17:03:07 +03003103}
3104
3105/**
3106 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3107 * @param mtd MTD device structure
Kyungmin Park493c6462006-05-12 17:03:07 +03003108 * @param len number of bytes to read
Christian Riesch4b78fc42014-01-28 09:29:44 +01003109 * @param retlen pointer to variable to store the number of read bytes
3110 * @param buf the databuffer to put/get data
Kyungmin Park493c6462006-05-12 17:03:07 +03003111 *
3112 * Read factory OTP info.
3113 */
Christian Riesch4b78fc42014-01-28 09:29:44 +01003114static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
3115 size_t *retlen, struct otp_info *buf)
Kyungmin Park493c6462006-05-12 17:03:07 +03003116{
Christian Riesch4b78fc42014-01-28 09:29:44 +01003117 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3118 MTD_OTP_FACTORY);
Kyungmin Park493c6462006-05-12 17:03:07 +03003119}
3120
3121/**
3122 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3123 * @param mtd MTD device structure
3124 * @param from The offset to read
3125 * @param len number of bytes to read
3126 * @param retlen pointer to variable to store the number of read bytes
3127 * @param buf the databuffer to put/get data
3128 *
3129 * Read factory OTP area.
3130 */
3131static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3132 size_t len, size_t *retlen, u_char *buf)
3133{
3134 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3135}
3136
3137/**
3138 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3139 * @param mtd MTD device structure
Christian Riesch4b78fc42014-01-28 09:29:44 +01003140 * @param retlen pointer to variable to store the number of read bytes
Kyungmin Park493c6462006-05-12 17:03:07 +03003141 * @param len number of bytes to read
Christian Riesch4b78fc42014-01-28 09:29:44 +01003142 * @param buf the databuffer to put/get data
Kyungmin Park493c6462006-05-12 17:03:07 +03003143 *
3144 * Read user OTP info.
3145 */
Christian Riesch4b78fc42014-01-28 09:29:44 +01003146static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len,
3147 size_t *retlen, struct otp_info *buf)
Kyungmin Park493c6462006-05-12 17:03:07 +03003148{
Christian Riesch4b78fc42014-01-28 09:29:44 +01003149 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3150 MTD_OTP_USER);
Kyungmin Park493c6462006-05-12 17:03:07 +03003151}
3152
3153/**
3154 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3155 * @param mtd MTD device structure
3156 * @param from The offset to read
3157 * @param len number of bytes to read
3158 * @param retlen pointer to variable to store the number of read bytes
3159 * @param buf the databuffer to put/get data
3160 *
3161 * Read user OTP area.
3162 */
3163static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3164 size_t len, size_t *retlen, u_char *buf)
3165{
3166 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3167}
3168
3169/**
3170 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3171 * @param mtd MTD device structure
3172 * @param from The offset to write
3173 * @param len number of bytes to write
3174 * @param retlen pointer to variable to store the number of write bytes
3175 * @param buf the databuffer to put/get data
3176 *
3177 * Write user OTP area.
3178 */
3179static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3180 size_t len, size_t *retlen, u_char *buf)
3181{
3182 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3183}
3184
3185/**
3186 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3187 * @param mtd MTD device structure
3188 * @param from The offset to lock
3189 * @param len number of bytes to unlock
3190 *
3191 * Write lock mark on spare area in page 0 in OTP block
3192 */
3193static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3194 size_t len)
3195{
Kyungmin Park69d79182007-12-14 14:47:21 +09003196 struct onenand_chip *this = mtd->priv;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003197 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
Kyungmin Park493c6462006-05-12 17:03:07 +03003198 size_t retlen;
3199 int ret;
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303200 unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
Kyungmin Park493c6462006-05-12 17:03:07 +03003201
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003202 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3203 : mtd->oobsize);
Kyungmin Park493c6462006-05-12 17:03:07 +03003204 /*
Kyungmin Park493c6462006-05-12 17:03:07 +03003205 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3206 * We write 16 bytes spare area instead of 2 bytes.
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003207 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3208 * main area of page 49.
Kyungmin Park493c6462006-05-12 17:03:07 +03003209 */
Kyungmin Park493c6462006-05-12 17:03:07 +03003210
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003211 from = 0;
3212 len = FLEXONENAND(this) ? mtd->writesize : 16;
3213
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303214 /*
3215 * Note: OTP lock operation
3216 * OTP block : 0xXXFC XX 1111 1100
3217 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3218 * Both : 0xXXF0 (If chip support) XX 1111 0000
3219 */
3220 if (FLEXONENAND(this))
3221 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3222
3223 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3224 if (otp == 1)
3225 buf[otp_lock_offset] = 0xFC;
3226 else if (otp == 2)
3227 buf[otp_lock_offset] = 0xF3;
3228 else if (otp == 3)
3229 buf[otp_lock_offset] = 0xF0;
3230 else if (otp != 0)
3231 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3232
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003233 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
Kyungmin Park493c6462006-05-12 17:03:07 +03003234
3235 return ret ? : retlen;
3236}
Amul Kumar Saha3cf60252009-10-21 17:00:05 +05303237
Kyungmin Park493c6462006-05-12 17:03:07 +03003238#endif /* CONFIG_MTD_ONENAND_OTP */
3239
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003240/**
Kyungmin Park75384b02007-01-18 11:10:57 +09003241 * onenand_check_features - Check and set OneNAND features
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003242 * @param mtd MTD data structure
3243 *
Kyungmin Park75384b02007-01-18 11:10:57 +09003244 * Check and set OneNAND features
3245 * - lock scheme
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003246 * - two plane
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003247 */
Kyungmin Park75384b02007-01-18 11:10:57 +09003248static void onenand_check_features(struct mtd_info *mtd)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003249{
3250 struct onenand_chip *this = mtd->priv;
Roman Tereshonkovedb44b92010-10-11 14:47:32 +03003251 unsigned int density, process, numbufs;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003252
3253 /* Lock scheme depends on density and process */
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003254 density = onenand_get_density(this->device_id);
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003255 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
Roman Tereshonkovedb44b92010-10-11 14:47:32 +03003256 numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003257
3258 /* Lock scheme */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003259 switch (density) {
Jonathan Bakker855eff22019-04-26 17:06:34 +02003260 case ONENAND_DEVICE_DENSITY_8Gb:
3261 this->options |= ONENAND_HAS_NOP_1;
Gustavo A. R. Silvacc407042019-06-04 08:58:02 -05003262 /* fall through */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003263 case ONENAND_DEVICE_DENSITY_4Gb:
Kyungmin Park6a88c472010-04-28 17:46:45 +02003264 if (ONENAND_IS_DDP(this))
3265 this->options |= ONENAND_HAS_2PLANE;
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02003266 else if (numbufs == 1) {
Kyungmin Park6a88c472010-04-28 17:46:45 +02003267 this->options |= ONENAND_HAS_4KB_PAGE;
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02003268 this->options |= ONENAND_HAS_CACHE_PROGRAM;
Kyungmin Parke1c10242011-06-22 14:16:49 +09003269 /*
3270 * There are two different 4KiB pagesize chips
3271 * and no way to detect it by H/W config values.
3272 *
3273 * To detect the correct NOP for each chips,
3274 * It should check the version ID as workaround.
3275 *
3276 * Now it has as following
3277 * KFM4G16Q4M has NOP 4 with version ID 0x0131
3278 * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
3279 */
3280 if ((this->version_id & 0xf) == 0xe)
3281 this->options |= ONENAND_HAS_NOP_1;
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02003282 }
Gustavo A. R. Silvadb010772019-05-23 14:16:06 -05003283 this->options |= ONENAND_HAS_UNLOCK_ALL;
3284 break;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003285
3286 case ONENAND_DEVICE_DENSITY_2Gb:
Mika Korhonen492e1502009-06-09 21:52:35 +03003287 /* 2Gb DDP does not have 2 plane */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003288 if (!ONENAND_IS_DDP(this))
3289 this->options |= ONENAND_HAS_2PLANE;
3290 this->options |= ONENAND_HAS_UNLOCK_ALL;
Gustavo A. R. Silvadb010772019-05-23 14:16:06 -05003291 break;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003292
3293 case ONENAND_DEVICE_DENSITY_1Gb:
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003294 /* A-Die has all block unlock */
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003295 if (process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003296 this->options |= ONENAND_HAS_UNLOCK_ALL;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003297 break;
3298
3299 default:
3300 /* Some OneNAND has continuous lock scheme */
3301 if (!process)
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003302 this->options |= ONENAND_HAS_CONT_LOCK;
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003303 break;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003304 }
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003305
Kyungmin Park8a8f6322010-12-02 09:24:16 +09003306 /* The MLC has 4KiB pagesize. */
3307 if (ONENAND_IS_MLC(this))
3308 this->options |= ONENAND_HAS_4KB_PAGE;
3309
3310 if (ONENAND_IS_4KB_PAGE(this))
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003311 this->options &= ~ONENAND_HAS_2PLANE;
3312
3313 if (FLEXONENAND(this)) {
3314 this->options &= ~ONENAND_HAS_CONT_LOCK;
3315 this->options |= ONENAND_HAS_UNLOCK_ALL;
3316 }
3317
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003318 if (this->options & ONENAND_HAS_CONT_LOCK)
3319 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3320 if (this->options & ONENAND_HAS_UNLOCK_ALL)
3321 printk(KERN_DEBUG "Chip support all block unlock\n");
3322 if (this->options & ONENAND_HAS_2PLANE)
3323 printk(KERN_DEBUG "Chip has 2 plane\n");
Kyungmin Park6a88c472010-04-28 17:46:45 +02003324 if (this->options & ONENAND_HAS_4KB_PAGE)
3325 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
Roman Tereshonkovac80dac2010-11-03 12:55:21 +02003326 if (this->options & ONENAND_HAS_CACHE_PROGRAM)
3327 printk(KERN_DEBUG "Chip has cache program feature\n");
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003328}
3329
3330/**
Kyungmin Parke3da8062007-02-15 09:36:39 +09003331 * onenand_print_device_info - Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003332 * @param device device ID
Kyungmin Parke3da8062007-02-15 09:36:39 +09003333 * @param version version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003334 *
Kyungmin Parke3da8062007-02-15 09:36:39 +09003335 * Print device & version ID
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003336 */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003337static void onenand_print_device_info(int device, int version)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003338{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003339 int vcc, demuxed, ddp, density, flexonenand;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003340
3341 vcc = device & ONENAND_DEVICE_VCC_MASK;
3342 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3343 ddp = device & ONENAND_DEVICE_IS_DDP;
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003344 density = onenand_get_density(device);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003345 flexonenand = device & DEVICE_IS_FLEXONENAND;
3346 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3347 demuxed ? "" : "Muxed ",
3348 flexonenand ? "Flex-" : "",
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003349 ddp ? "(DDP)" : "",
3350 (16 << density),
3351 vcc ? "2.65/3.3" : "1.8",
3352 device);
Artem Bityutskiy49dc08e2007-09-21 19:35:21 +03003353 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003354}
3355
3356static const struct onenand_manufacturers onenand_manuf_ids[] = {
3357 {ONENAND_MFR_SAMSUNG, "Samsung"},
Adrian Hunteree8f3762009-05-05 11:04:19 +03003358 {ONENAND_MFR_NUMONYX, "Numonyx"},
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003359};
3360
3361/**
3362 * onenand_check_maf - Check manufacturer ID
3363 * @param manuf manufacturer ID
3364 *
3365 * Check manufacturer ID
3366 */
3367static int onenand_check_maf(int manuf)
3368{
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003369 int size = ARRAY_SIZE(onenand_manuf_ids);
3370 char *name;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003371 int i;
3372
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003373 for (i = 0; i < size; i++)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003374 if (manuf == onenand_manuf_ids[i].id)
3375 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003376
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003377 if (i < size)
3378 name = onenand_manuf_ids[i].name;
3379 else
3380 name = "Unknown";
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003381
Kyungmin Park37b1cc32005-12-16 11:17:29 +09003382 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3383
3384 return (i == size);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003385}
3386
3387/**
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003388* flexonenand_get_boundary - Reads the SLC boundary
3389* @param onenand_info - onenand info structure
3390**/
3391static int flexonenand_get_boundary(struct mtd_info *mtd)
3392{
3393 struct onenand_chip *this = mtd->priv;
3394 unsigned die, bdry;
Brian Norris6b7368c2013-08-27 18:01:19 -07003395 int syscfg, locked;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003396
3397 /* Disable ECC */
3398 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3399 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3400
3401 for (die = 0; die < this->dies; die++) {
3402 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3403 this->wait(mtd, FL_SYNCING);
3404
3405 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Brian Norris6b7368c2013-08-27 18:01:19 -07003406 this->wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003407
3408 bdry = this->read_word(this->base + ONENAND_DATARAM);
3409 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3410 locked = 0;
3411 else
3412 locked = 1;
3413 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3414
3415 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
Brian Norris6b7368c2013-08-27 18:01:19 -07003416 this->wait(mtd, FL_RESETING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003417
3418 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3419 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3420 }
3421
3422 /* Enable ECC */
3423 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3424 return 0;
3425}
3426
3427/**
3428 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3429 * boundary[], diesize[], mtd->size, mtd->erasesize
3430 * @param mtd - MTD device structure
3431 */
3432static void flexonenand_get_size(struct mtd_info *mtd)
3433{
3434 struct onenand_chip *this = mtd->priv;
3435 int die, i, eraseshift, density;
3436 int blksperdie, maxbdry;
3437 loff_t ofs;
3438
3439 density = onenand_get_density(this->device_id);
3440 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3441 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3442 maxbdry = blksperdie - 1;
3443 eraseshift = this->erase_shift - 1;
3444
3445 mtd->numeraseregions = this->dies << 1;
3446
3447 /* This fills up the device boundary */
3448 flexonenand_get_boundary(mtd);
3449 die = ofs = 0;
3450 i = -1;
3451 for (; die < this->dies; die++) {
3452 if (!die || this->boundary[die-1] != maxbdry) {
3453 i++;
3454 mtd->eraseregions[i].offset = ofs;
3455 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3456 mtd->eraseregions[i].numblocks =
3457 this->boundary[die] + 1;
3458 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3459 eraseshift++;
3460 } else {
3461 mtd->numeraseregions -= 1;
3462 mtd->eraseregions[i].numblocks +=
3463 this->boundary[die] + 1;
3464 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3465 }
3466 if (this->boundary[die] != maxbdry) {
3467 i++;
3468 mtd->eraseregions[i].offset = ofs;
3469 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3470 mtd->eraseregions[i].numblocks = maxbdry ^
3471 this->boundary[die];
3472 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3473 eraseshift--;
3474 } else
3475 mtd->numeraseregions -= 1;
3476 }
3477
3478 /* Expose MLC erase size except when all blocks are SLC */
3479 mtd->erasesize = 1 << this->erase_shift;
3480 if (mtd->numeraseregions == 1)
3481 mtd->erasesize >>= 1;
3482
3483 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3484 for (i = 0; i < mtd->numeraseregions; i++)
3485 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3486 " numblocks: %04u]\n",
3487 (unsigned int) mtd->eraseregions[i].offset,
3488 mtd->eraseregions[i].erasesize,
3489 mtd->eraseregions[i].numblocks);
3490
3491 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3492 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3493 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3494 << (this->erase_shift - 1);
3495 mtd->size += this->diesize[die];
3496 }
3497}
3498
3499/**
3500 * flexonenand_check_blocks_erased - Check if blocks are erased
3501 * @param mtd_info - mtd info structure
3502 * @param start - first erase block to check
3503 * @param end - last erase block to check
3504 *
3505 * Converting an unerased block from MLC to SLC
3506 * causes byte values to change. Since both data and its ECC
3507 * have changed, reads on the block give uncorrectable error.
3508 * This might lead to the block being detected as bad.
3509 *
3510 * Avoid this by ensuring that the block to be converted is
3511 * erased.
3512 */
3513static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3514{
3515 struct onenand_chip *this = mtd->priv;
3516 int i, ret;
3517 int block;
3518 struct mtd_oob_ops ops = {
Brian Norris0612b9d2011-08-30 18:45:40 -07003519 .mode = MTD_OPS_PLACE_OOB,
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003520 .ooboffs = 0,
3521 .ooblen = mtd->oobsize,
3522 .datbuf = NULL,
3523 .oobbuf = this->oob_buf,
3524 };
3525 loff_t addr;
3526
3527 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3528
3529 for (block = start; block <= end; block++) {
3530 addr = flexonenand_addr(this, block);
3531 if (onenand_block_isbad_nolock(mtd, addr, 0))
3532 continue;
3533
3534 /*
3535 * Since main area write results in ECC write to spare,
3536 * it is sufficient to check only ECC bytes for change.
3537 */
3538 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3539 if (ret)
3540 return ret;
3541
3542 for (i = 0; i < mtd->oobsize; i++)
3543 if (this->oob_buf[i] != 0xff)
3544 break;
3545
3546 if (i != mtd->oobsize) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303547 printk(KERN_WARNING "%s: Block %d not erased.\n",
3548 __func__, block);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003549 return 1;
3550 }
3551 }
3552
3553 return 0;
3554}
3555
3556/**
3557 * flexonenand_set_boundary - Writes the SLC boundary
3558 * @param mtd - mtd info structure
3559 */
Sachin Kamat01319502012-09-22 11:42:31 +05303560static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003561 int boundary, int lock)
3562{
3563 struct onenand_chip *this = mtd->priv;
3564 int ret, density, blksperdie, old, new, thisboundary;
3565 loff_t addr;
3566
3567 /* Change only once for SDP Flex-OneNAND */
3568 if (die && (!ONENAND_IS_DDP(this)))
3569 return 0;
3570
3571 /* boundary value of -1 indicates no required change */
3572 if (boundary < 0 || boundary == this->boundary[die])
3573 return 0;
3574
3575 density = onenand_get_density(this->device_id);
3576 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3577 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3578
3579 if (boundary >= blksperdie) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303580 printk(KERN_ERR "%s: Invalid boundary value. "
3581 "Boundary not changed.\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003582 return -EINVAL;
3583 }
3584
3585 /* Check if converting blocks are erased */
3586 old = this->boundary[die] + (die * this->density_mask);
3587 new = boundary + (die * this->density_mask);
3588 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3589 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303590 printk(KERN_ERR "%s: Please erase blocks "
3591 "before boundary change\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003592 return ret;
3593 }
3594
3595 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3596 this->wait(mtd, FL_SYNCING);
3597
3598 /* Check is boundary is locked */
3599 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
Brian Norris6b7368c2013-08-27 18:01:19 -07003600 this->wait(mtd, FL_READING);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003601
3602 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3603 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303604 printk(KERN_ERR "%s: boundary locked\n", __func__);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003605 ret = 1;
3606 goto out;
3607 }
3608
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303609 printk(KERN_INFO "Changing die %d boundary: %d%s\n",
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003610 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3611
3612 addr = die ? this->diesize[0] : 0;
3613
3614 boundary &= FLEXONENAND_PI_MASK;
3615 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3616
3617 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3618 ret = this->wait(mtd, FL_ERASING);
3619 if (ret) {
Mika Korhonenf369c7e2009-10-23 07:50:44 +02003620 printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
3621 __func__, die);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003622 goto out;
3623 }
3624
3625 this->write_word(boundary, this->base + ONENAND_DATARAM);
3626 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3627 ret = this->wait(mtd, FL_WRITING);
3628 if (ret) {
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303629 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3630 __func__, die);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003631 goto out;
3632 }
3633
3634 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3635 ret = this->wait(mtd, FL_WRITING);
3636out:
3637 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3638 this->wait(mtd, FL_RESETING);
3639 if (!ret)
3640 /* Recalculate device size on boundary change*/
3641 flexonenand_get_size(mtd);
3642
3643 return ret;
3644}
3645
3646/**
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003647 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003648 * @param mtd MTD device structure
3649 *
3650 * OneNAND detection method:
Michael Opdenacker59c51592007-05-09 08:57:56 +02003651 * Compare the values from command with ones from register
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003652 */
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003653static int onenand_chip_probe(struct mtd_info *mtd)
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003654{
3655 struct onenand_chip *this = mtd->priv;
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003656 int bram_maf_id, bram_dev_id, maf_id, dev_id;
Kyungmin Park47e777e2006-09-25 23:53:28 +00003657 int syscfg;
3658
3659 /* Save system configuration 1 */
3660 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3661 /* Clear Sync. Burst Read mode to read BootRAM */
Adrian Hunteree8f3762009-05-05 11:04:19 +03003662 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003663
3664 /* Send the command for reading device ID from BootRAM */
3665 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3666
3667 /* Read manufacturer and device IDs from BootRAM */
3668 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3669 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3670
Kyungmin Park47e777e2006-09-25 23:53:28 +00003671 /* Reset OneNAND to read default register values */
3672 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3673 /* Wait reset */
3674 this->wait(mtd, FL_RESETING);
3675
3676 /* Restore system configuration 1 */
3677 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3678
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003679 /* Check manufacturer ID */
3680 if (onenand_check_maf(bram_maf_id))
3681 return -ENXIO;
3682
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003683 /* Read manufacturer and device IDs from Register */
3684 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3685 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3686
3687 /* Check OneNAND device */
3688 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3689 return -ENXIO;
3690
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003691 return 0;
3692}
3693
3694/**
3695 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3696 * @param mtd MTD device structure
3697 */
3698static int onenand_probe(struct mtd_info *mtd)
3699{
3700 struct onenand_chip *this = mtd->priv;
Brian Norris6b7368c2013-08-27 18:01:19 -07003701 int dev_id, ver_id;
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003702 int density;
3703 int ret;
3704
3705 ret = this->chip_probe(mtd);
3706 if (ret)
3707 return ret;
3708
Brian Norris6b7368c2013-08-27 18:01:19 -07003709 /* Device and version IDs from Register */
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003710 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3711 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3712 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3713
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003714 /* Flash device information */
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003715 onenand_print_device_info(dev_id, ver_id);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003716 this->device_id = dev_id;
Kyungmin Park28b79ff2006-09-26 09:45:28 +00003717 this->version_id = ver_id;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003718
Kyungmin Parkc37cb562010-04-28 17:46:48 +02003719 /* Check OneNAND features */
3720 onenand_check_features(mtd);
3721
Kyungmin Parke71f04f2007-12-11 11:23:45 +09003722 density = onenand_get_density(dev_id);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003723 if (FLEXONENAND(this)) {
3724 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3725 /* Maximum possible erase regions */
3726 mtd->numeraseregions = this->dies << 1;
Kees Cook6396bb22018-06-12 14:03:40 -07003727 mtd->eraseregions =
3728 kcalloc(this->dies << 1,
3729 sizeof(struct mtd_erase_region_info),
3730 GFP_KERNEL);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003731 if (!mtd->eraseregions)
3732 return -ENOMEM;
3733 }
3734
3735 /*
3736 * For Flex-OneNAND, chipsize represents maximum possible device size.
3737 * mtd->size represents the actual device size.
3738 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003739 this->chipsize = (16 << density) << 20;
3740
3741 /* OneNAND page size & block size */
3742 /* The data buffer size is equal to page size */
Joern Engel28318772006-05-22 23:18:05 +02003743 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003744 /* We use the full BufferRAM */
Kyungmin Park8a8f6322010-12-02 09:24:16 +09003745 if (ONENAND_IS_4KB_PAGE(this))
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003746 mtd->writesize <<= 1;
3747
Joern Engel28318772006-05-22 23:18:05 +02003748 mtd->oobsize = mtd->writesize >> 5;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09003749 /* Pages per a block are always 64 in OneNAND */
Joern Engel28318772006-05-22 23:18:05 +02003750 mtd->erasesize = mtd->writesize << 6;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003751 /*
3752 * Flex-OneNAND SLC area has 64 pages per block.
3753 * Flex-OneNAND MLC area has 128 pages per block.
3754 * Expose MLC erase size to find erase_shift and page_mask.
3755 */
3756 if (FLEXONENAND(this))
3757 mtd->erasesize <<= 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003758
3759 this->erase_shift = ffs(mtd->erasesize) - 1;
Joern Engel28318772006-05-22 23:18:05 +02003760 this->page_shift = ffs(mtd->writesize) - 1;
Kyungmin Park9bfbc9b2007-01-31 14:25:21 +09003761 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003762 /* Set density mask. it is used for DDP */
3763 if (ONENAND_IS_DDP(this))
3764 this->density_mask = this->chipsize >> (this->erase_shift + 1);
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003765 /* It's real page size */
3766 this->writesize = mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003767
Mika Korhonen492e1502009-06-09 21:52:35 +03003768 /* REVISIT: Multichip handling */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003769
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003770 if (FLEXONENAND(this))
3771 flexonenand_get_size(mtd);
3772 else
3773 mtd->size = this->chipsize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003774
Kyungmin Parkee9745f2007-06-30 13:57:49 +09003775 /*
3776 * We emulate the 4KiB page and 256KiB erase block size
3777 * But oobsize is still 64 bytes.
3778 * It is only valid if you turn on 2X program support,
3779 * Otherwise it will be ignored by compiler.
3780 */
3781 if (ONENAND_IS_2PLANE(this)) {
3782 mtd->writesize <<= 1;
3783 mtd->erasesize <<= 1;
3784 }
3785
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003786 return 0;
3787}
3788
Kyungmin Parka41371e2005-09-29 03:55:31 +01003789/**
3790 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3791 * @param mtd MTD device structure
3792 */
3793static int onenand_suspend(struct mtd_info *mtd)
3794{
3795 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3796}
3797
3798/**
3799 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3800 * @param mtd MTD device structure
3801 */
3802static void onenand_resume(struct mtd_info *mtd)
3803{
3804 struct onenand_chip *this = mtd->priv;
3805
3806 if (this->state == FL_PM_SUSPENDED)
3807 onenand_release_device(mtd);
3808 else
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303809 printk(KERN_ERR "%s: resume() called for the chip which is not "
3810 "in suspended state\n", __func__);
Kyungmin Parka41371e2005-09-29 03:55:31 +01003811}
3812
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003813/**
3814 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3815 * @param mtd MTD device structure
3816 * @param maxchips Number of chips to scan for
3817 *
3818 * This fills out all the not initialized function pointers
3819 * with the defaults.
3820 * The flash ID is read and the mtd/chip structures are
3821 * filled with the appropriate values.
3822 */
3823int onenand_scan(struct mtd_info *mtd, int maxchips)
3824{
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003825 int i, ret;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003826 struct onenand_chip *this = mtd->priv;
3827
3828 if (!this->read_word)
3829 this->read_word = onenand_readw;
3830 if (!this->write_word)
3831 this->write_word = onenand_writew;
3832
3833 if (!this->command)
3834 this->command = onenand_command;
3835 if (!this->wait)
Kyungmin Park2c221202006-11-16 11:23:48 +09003836 onenand_setup_wait(mtd);
Kyungmin Park31bb9992009-05-12 13:46:57 -07003837 if (!this->bbt_wait)
3838 this->bbt_wait = onenand_bbt_wait;
3839 if (!this->unlock_all)
3840 this->unlock_all = onenand_unlock_all;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003841
Kyungmin Parkad0d3632010-05-28 11:03:11 +09003842 if (!this->chip_probe)
3843 this->chip_probe = onenand_chip_probe;
3844
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003845 if (!this->read_bufferram)
3846 this->read_bufferram = onenand_read_bufferram;
3847 if (!this->write_bufferram)
3848 this->write_bufferram = onenand_write_bufferram;
3849
Kyungmin Parkcdc00132005-09-03 07:15:48 +01003850 if (!this->block_markbad)
3851 this->block_markbad = onenand_default_block_markbad;
3852 if (!this->scan_bbt)
3853 this->scan_bbt = onenand_default_bbt;
3854
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003855 if (onenand_probe(mtd))
3856 return -ENXIO;
3857
Kyungmin Park52b0eea2005-09-03 07:07:19 +01003858 /* Set Sync. Burst Read after probing */
3859 if (this->mmcontrol) {
3860 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3861 this->read_bufferram = onenand_sync_read_bufferram;
3862 }
3863
Kyungmin Park532a37c2005-12-16 11:17:29 +09003864 /* Allocate buffers, if necessary */
3865 if (!this->page_buf) {
Kyungmin Park470bc842007-03-09 10:08:11 +09003866 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
Jingoo Hane4eec192014-02-06 15:15:38 +09003867 if (!this->page_buf)
Kyungmin Park532a37c2005-12-16 11:17:29 +09003868 return -ENOMEM;
Kyungmin Park4a8ce0b2010-04-28 17:46:46 +02003869#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3870 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3871 if (!this->verify_buf) {
3872 kfree(this->page_buf);
3873 return -ENOMEM;
3874 }
3875#endif
Kyungmin Park532a37c2005-12-16 11:17:29 +09003876 this->options |= ONENAND_PAGEBUF_ALLOC;
3877 }
Kyungmin Park470bc842007-03-09 10:08:11 +09003878 if (!this->oob_buf) {
3879 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3880 if (!this->oob_buf) {
Kyungmin Park470bc842007-03-09 10:08:11 +09003881 if (this->options & ONENAND_PAGEBUF_ALLOC) {
3882 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3883 kfree(this->page_buf);
3884 }
3885 return -ENOMEM;
3886 }
3887 this->options |= ONENAND_OOBBUF_ALLOC;
3888 }
Kyungmin Park532a37c2005-12-16 11:17:29 +09003889
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003890 this->state = FL_READY;
3891 init_waitqueue_head(&this->wq);
3892 spin_lock_init(&this->chip_lock);
3893
Kyungmin Park60d84f92006-12-22 16:21:54 +09003894 /*
3895 * Allow subpage writes up to oobsize.
3896 */
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003897 switch (mtd->oobsize) {
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003898 case 128:
Roman Tereshonkov99b17c02011-04-11 12:52:01 +03003899 if (FLEXONENAND(this)) {
Boris Brezillona4116792015-12-07 22:46:45 +01003900 mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops);
Roman Tereshonkov99b17c02011-04-11 12:52:01 +03003901 mtd->subpage_sft = 0;
3902 } else {
Boris Brezillona4116792015-12-07 22:46:45 +01003903 mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops);
Roman Tereshonkov99b17c02011-04-11 12:52:01 +03003904 mtd->subpage_sft = 2;
3905 }
Kyungmin Parke1c10242011-06-22 14:16:49 +09003906 if (ONENAND_IS_NOP_1(this))
3907 mtd->subpage_sft = 0;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003908 break;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003909 case 64:
Boris Brezillona4116792015-12-07 22:46:45 +01003910 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
Kyungmin Park60d84f92006-12-22 16:21:54 +09003911 mtd->subpage_sft = 2;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003912 break;
3913
3914 case 32:
Boris Brezillona4116792015-12-07 22:46:45 +01003915 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
Kyungmin Park60d84f92006-12-22 16:21:54 +09003916 mtd->subpage_sft = 1;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003917 break;
3918
3919 default:
Amul Kumar Saha297758f2009-10-02 16:59:11 +05303920 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
3921 __func__, mtd->oobsize);
Kyungmin Park60d84f92006-12-22 16:21:54 +09003922 mtd->subpage_sft = 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003923 /* To prevent kernel oops */
Boris Brezillona4116792015-12-07 22:46:45 +01003924 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003925 break;
3926 }
3927
Kyungmin Park60d84f92006-12-22 16:21:54 +09003928 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02003929
3930 /*
3931 * The number of bytes available for a client to place data into
3932 * the out of band area
3933 */
Boris Brezillond30aae62016-02-03 20:12:31 +01003934 ret = mtd_ooblayout_count_freebytes(mtd);
3935 if (ret < 0)
3936 ret = 0;
3937
3938 mtd->oobavail = ret;
Adrian Huntera5e7c7b2007-01-31 17:19:28 +02003939
Mike Dunn6a918ba2012-03-11 14:21:11 -07003940 mtd->ecc_strength = 1;
Thomas Gleixnerd5c5e782005-11-07 11:15:51 +00003941
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003942 /* Fill in remaining MTD driver data */
Rohit Hassan Sathyanarayanc7626802010-09-27 16:02:10 +05303943 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02003944 mtd->flags = MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003945 mtd->_erase = onenand_erase;
3946 mtd->_point = NULL;
3947 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003948 mtd->_read_oob = onenand_read_oob;
3949 mtd->_write_oob = onenand_write_oob;
3950 mtd->_panic_write = onenand_panic_write;
Kyungmin Park493c6462006-05-12 17:03:07 +03003951#ifdef CONFIG_MTD_ONENAND_OTP
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003952 mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
3953 mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
3954 mtd->_get_user_prot_info = onenand_get_user_prot_info;
3955 mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
3956 mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
3957 mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
Kyungmin Park493c6462006-05-12 17:03:07 +03003958#endif
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003959 mtd->_sync = onenand_sync;
3960 mtd->_lock = onenand_lock;
3961 mtd->_unlock = onenand_unlock;
3962 mtd->_suspend = onenand_suspend;
3963 mtd->_resume = onenand_resume;
3964 mtd->_block_isbad = onenand_block_isbad;
3965 mtd->_block_markbad = onenand_block_markbad;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003966 mtd->owner = THIS_MODULE;
Anatolij Gustschin25dcd292010-12-16 23:42:17 +01003967 mtd->writebufsize = mtd->writesize;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003968
3969 /* Unlock whole block */
Roman Tereshonkovb3dcfd352011-02-17 13:44:41 +02003970 if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
3971 this->unlock_all(mtd);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003972
Frieder Schrempfbfd15c92019-04-17 12:36:35 +00003973 /* Set the bad block marker position */
3974 this->badblockpos = ONENAND_BADBLOCK_POS;
3975
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07003976 ret = this->scan_bbt(mtd);
3977 if ((!FLEXONENAND(this)) || ret)
3978 return ret;
3979
3980 /* Change Flex-OneNAND boundaries if required */
3981 for (i = 0; i < MAX_DIES; i++)
3982 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3983 flex_bdry[(2 * i) + 1]);
3984
3985 return 0;
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003986}
3987
3988/**
3989 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3990 * @param mtd MTD device structure
3991 */
3992void onenand_release(struct mtd_info *mtd)
3993{
Kyungmin Park532a37c2005-12-16 11:17:29 +09003994 struct onenand_chip *this = mtd->priv;
3995
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01003996 /* Deregister partitions */
Jamie Iles711a6322011-05-23 10:22:56 +01003997 mtd_device_unregister(mtd);
Kyungmin Park532a37c2005-12-16 11:17:29 +09003998
3999 /* Free bad block table memory, if allocated */
Adrian Hunterf00b0042007-01-22 17:01:01 +09004000 if (this->bbm) {
4001 struct bbm_info *bbm = this->bbm;
4002 kfree(bbm->bbt);
Kyungmin Park532a37c2005-12-16 11:17:29 +09004003 kfree(this->bbm);
Adrian Hunterf00b0042007-01-22 17:01:01 +09004004 }
Kyungmin Park470bc842007-03-09 10:08:11 +09004005 /* Buffers allocated by onenand_scan */
Kyungmin Park4a8ce0b2010-04-28 17:46:46 +02004006 if (this->options & ONENAND_PAGEBUF_ALLOC) {
Kyungmin Park532a37c2005-12-16 11:17:29 +09004007 kfree(this->page_buf);
Kyungmin Park4a8ce0b2010-04-28 17:46:46 +02004008#ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4009 kfree(this->verify_buf);
4010#endif
4011 }
Kyungmin Park470bc842007-03-09 10:08:11 +09004012 if (this->options & ONENAND_OOBBUF_ALLOC)
4013 kfree(this->oob_buf);
Rohit Hagargundgi5988af22009-05-12 13:46:57 -07004014 kfree(mtd->eraseregions);
Kyungmin Parkcd5f6342005-07-11 11:41:53 +01004015}
4016
4017EXPORT_SYMBOL_GPL(onenand_scan);
4018EXPORT_SYMBOL_GPL(onenand_release);
4019
4020MODULE_LICENSE("GPL");
4021MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4022MODULE_DESCRIPTION("Generic OneNAND flash driver code");