Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/mtd/nand.c | |
3 | * | |
4 | * Overview: | |
5 | * This is the generic MTD driver for NAND flash devices. It should be | |
6 | * capable of working with almost all NAND chips currently available. | |
61b03bd7 | 7 | * |
1da177e4 | 8 | * Additional technical information is available on |
8b2b403c | 9 | * http://www.linux-mtd.infradead.org/doc/nand.html |
61b03bd7 | 10 | * |
1da177e4 | 11 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
ace4dfee | 12 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
1da177e4 | 13 | * |
ace4dfee | 14 | * Credits: |
61b03bd7 TG |
15 | * David Woodhouse for adding multichip support |
16 | * | |
1da177e4 LT |
17 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
18 | * rework for 2K page size chips | |
19 | * | |
ace4dfee | 20 | * TODO: |
1da177e4 LT |
21 | * Enable cached programming for 2k page size chips |
22 | * Check, if mtd->ecctype should be set to MTD_ECC_HW | |
7854d3f7 | 23 | * if we have HW ECC support. |
c0b8ba7b | 24 | * BBT table is not serialized, has to be fixed |
1da177e4 | 25 | * |
1da177e4 LT |
26 | * This program is free software; you can redistribute it and/or modify |
27 | * it under the terms of the GNU General Public License version 2 as | |
28 | * published by the Free Software Foundation. | |
29 | * | |
30 | */ | |
31 | ||
20171642 EG |
32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
33 | ||
552d9205 | 34 | #include <linux/module.h> |
1da177e4 LT |
35 | #include <linux/delay.h> |
36 | #include <linux/errno.h> | |
7aa65bfd | 37 | #include <linux/err.h> |
1da177e4 LT |
38 | #include <linux/sched.h> |
39 | #include <linux/slab.h> | |
40 | #include <linux/types.h> | |
41 | #include <linux/mtd/mtd.h> | |
42 | #include <linux/mtd/nand.h> | |
43 | #include <linux/mtd/nand_ecc.h> | |
193bd400 | 44 | #include <linux/mtd/nand_bch.h> |
1da177e4 LT |
45 | #include <linux/interrupt.h> |
46 | #include <linux/bitops.h> | |
8fe833c1 | 47 | #include <linux/leds.h> |
7351d3a5 | 48 | #include <linux/io.h> |
1da177e4 | 49 | #include <linux/mtd/partitions.h> |
1da177e4 LT |
50 | |
51 | /* Define default oob placement schemes for large and small page devices */ | |
5bd34c09 | 52 | static struct nand_ecclayout nand_oob_8 = { |
1da177e4 LT |
53 | .eccbytes = 3, |
54 | .eccpos = {0, 1, 2}, | |
5bd34c09 TG |
55 | .oobfree = { |
56 | {.offset = 3, | |
57 | .length = 2}, | |
58 | {.offset = 6, | |
f8ac0414 | 59 | .length = 2} } |
1da177e4 LT |
60 | }; |
61 | ||
5bd34c09 | 62 | static struct nand_ecclayout nand_oob_16 = { |
1da177e4 LT |
63 | .eccbytes = 6, |
64 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
5bd34c09 TG |
65 | .oobfree = { |
66 | {.offset = 8, | |
f8ac0414 | 67 | . length = 8} } |
1da177e4 LT |
68 | }; |
69 | ||
5bd34c09 | 70 | static struct nand_ecclayout nand_oob_64 = { |
1da177e4 LT |
71 | .eccbytes = 24, |
72 | .eccpos = { | |
e0c7d767 DW |
73 | 40, 41, 42, 43, 44, 45, 46, 47, |
74 | 48, 49, 50, 51, 52, 53, 54, 55, | |
75 | 56, 57, 58, 59, 60, 61, 62, 63}, | |
5bd34c09 TG |
76 | .oobfree = { |
77 | {.offset = 2, | |
f8ac0414 | 78 | .length = 38} } |
1da177e4 LT |
79 | }; |
80 | ||
81ec5364 TG |
81 | static struct nand_ecclayout nand_oob_128 = { |
82 | .eccbytes = 48, | |
83 | .eccpos = { | |
84 | 80, 81, 82, 83, 84, 85, 86, 87, | |
85 | 88, 89, 90, 91, 92, 93, 94, 95, | |
86 | 96, 97, 98, 99, 100, 101, 102, 103, | |
87 | 104, 105, 106, 107, 108, 109, 110, 111, | |
88 | 112, 113, 114, 115, 116, 117, 118, 119, | |
89 | 120, 121, 122, 123, 124, 125, 126, 127}, | |
90 | .oobfree = { | |
91 | {.offset = 2, | |
f8ac0414 | 92 | .length = 78} } |
81ec5364 TG |
93 | }; |
94 | ||
6a8214aa | 95 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
1da177e4 | 96 | |
8593fbc6 TG |
97 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
98 | struct mtd_oob_ops *ops); | |
99 | ||
d470a97c | 100 | /* |
8e87d782 | 101 | * For devices which display every fart in the system on a separate LED. Is |
d470a97c TG |
102 | * compiled away when LED support is disabled. |
103 | */ | |
104 | DEFINE_LED_TRIGGER(nand_led_trigger); | |
105 | ||
6fe5a6ac VS |
106 | static int check_offs_len(struct mtd_info *mtd, |
107 | loff_t ofs, uint64_t len) | |
108 | { | |
109 | struct nand_chip *chip = mtd->priv; | |
110 | int ret = 0; | |
111 | ||
112 | /* Start address must align on block boundary */ | |
daae74ca | 113 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
289c0522 | 114 | pr_debug("%s: unaligned address\n", __func__); |
6fe5a6ac VS |
115 | ret = -EINVAL; |
116 | } | |
117 | ||
118 | /* Length must align on block boundary */ | |
daae74ca | 119 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
289c0522 | 120 | pr_debug("%s: length not block aligned\n", __func__); |
6fe5a6ac VS |
121 | ret = -EINVAL; |
122 | } | |
123 | ||
6fe5a6ac VS |
124 | return ret; |
125 | } | |
126 | ||
1da177e4 LT |
127 | /** |
128 | * nand_release_device - [GENERIC] release chip | |
8b6e50c9 | 129 | * @mtd: MTD device structure |
61b03bd7 | 130 | * |
b0bb6903 | 131 | * Release chip lock and wake up anyone waiting on the device. |
1da177e4 | 132 | */ |
e0c7d767 | 133 | static void nand_release_device(struct mtd_info *mtd) |
1da177e4 | 134 | { |
ace4dfee | 135 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 136 | |
a36ed299 | 137 | /* Release the controller and the chip */ |
ace4dfee TG |
138 | spin_lock(&chip->controller->lock); |
139 | chip->controller->active = NULL; | |
140 | chip->state = FL_READY; | |
141 | wake_up(&chip->controller->wq); | |
142 | spin_unlock(&chip->controller->lock); | |
1da177e4 LT |
143 | } |
144 | ||
145 | /** | |
146 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
8b6e50c9 | 147 | * @mtd: MTD device structure |
1da177e4 | 148 | * |
7854d3f7 | 149 | * Default read function for 8bit buswidth |
1da177e4 | 150 | */ |
58dd8f2b | 151 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
1da177e4 | 152 | { |
ace4dfee TG |
153 | struct nand_chip *chip = mtd->priv; |
154 | return readb(chip->IO_ADDR_R); | |
1da177e4 LT |
155 | } |
156 | ||
1da177e4 | 157 | /** |
064a7694 | 158 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
7854d3f7 | 159 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
8b6e50c9 | 160 | * @mtd: MTD device structure |
1da177e4 | 161 | * |
7854d3f7 BN |
162 | * Default read function for 16bit buswidth with endianness conversion. |
163 | * | |
1da177e4 | 164 | */ |
58dd8f2b | 165 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
1da177e4 | 166 | { |
ace4dfee TG |
167 | struct nand_chip *chip = mtd->priv; |
168 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); | |
1da177e4 LT |
169 | } |
170 | ||
1da177e4 LT |
171 | /** |
172 | * nand_read_word - [DEFAULT] read one word from the chip | |
8b6e50c9 | 173 | * @mtd: MTD device structure |
1da177e4 | 174 | * |
7854d3f7 | 175 | * Default read function for 16bit buswidth without endianness conversion. |
1da177e4 LT |
176 | */ |
177 | static u16 nand_read_word(struct mtd_info *mtd) | |
178 | { | |
ace4dfee TG |
179 | struct nand_chip *chip = mtd->priv; |
180 | return readw(chip->IO_ADDR_R); | |
1da177e4 LT |
181 | } |
182 | ||
1da177e4 LT |
183 | /** |
184 | * nand_select_chip - [DEFAULT] control CE line | |
8b6e50c9 BN |
185 | * @mtd: MTD device structure |
186 | * @chipnr: chipnumber to select, -1 for deselect | |
1da177e4 LT |
187 | * |
188 | * Default select function for 1 chip devices. | |
189 | */ | |
ace4dfee | 190 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
1da177e4 | 191 | { |
ace4dfee TG |
192 | struct nand_chip *chip = mtd->priv; |
193 | ||
194 | switch (chipnr) { | |
1da177e4 | 195 | case -1: |
ace4dfee | 196 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
1da177e4 LT |
197 | break; |
198 | case 0: | |
1da177e4 LT |
199 | break; |
200 | ||
201 | default: | |
202 | BUG(); | |
203 | } | |
204 | } | |
205 | ||
05f78359 UKK |
206 | /** |
207 | * nand_write_byte - [DEFAULT] write single byte to chip | |
208 | * @mtd: MTD device structure | |
209 | * @byte: value to write | |
210 | * | |
211 | * Default function to write a byte to I/O[7:0] | |
212 | */ | |
213 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) | |
214 | { | |
215 | struct nand_chip *chip = mtd->priv; | |
216 | ||
217 | chip->write_buf(mtd, &byte, 1); | |
218 | } | |
219 | ||
220 | /** | |
221 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 | |
222 | * @mtd: MTD device structure | |
223 | * @byte: value to write | |
224 | * | |
225 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. | |
226 | */ | |
227 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) | |
228 | { | |
229 | struct nand_chip *chip = mtd->priv; | |
230 | uint16_t word = byte; | |
231 | ||
232 | /* | |
233 | * It's not entirely clear what should happen to I/O[15:8] when writing | |
234 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: | |
235 | * | |
236 | * When the host supports a 16-bit bus width, only data is | |
237 | * transferred at the 16-bit width. All address and command line | |
238 | * transfers shall use only the lower 8-bits of the data bus. During | |
239 | * command transfers, the host may place any value on the upper | |
240 | * 8-bits of the data bus. During address transfers, the host shall | |
241 | * set the upper 8-bits of the data bus to 00h. | |
242 | * | |
243 | * One user of the write_byte callback is nand_onfi_set_features. The | |
244 | * four parameters are specified to be written to I/O[7:0], but this is | |
245 | * neither an address nor a command transfer. Let's assume a 0 on the | |
246 | * upper I/O lines is OK. | |
247 | */ | |
248 | chip->write_buf(mtd, (uint8_t *)&word, 2); | |
249 | } | |
250 | ||
1da177e4 LT |
251 | /** |
252 | * nand_write_buf - [DEFAULT] write buffer to chip | |
8b6e50c9 BN |
253 | * @mtd: MTD device structure |
254 | * @buf: data buffer | |
255 | * @len: number of bytes to write | |
1da177e4 | 256 | * |
7854d3f7 | 257 | * Default write function for 8bit buswidth. |
1da177e4 | 258 | */ |
58dd8f2b | 259 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 | 260 | { |
ace4dfee | 261 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 262 | |
76413839 | 263 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
1da177e4 LT |
264 | } |
265 | ||
266 | /** | |
61b03bd7 | 267 | * nand_read_buf - [DEFAULT] read chip data into buffer |
8b6e50c9 BN |
268 | * @mtd: MTD device structure |
269 | * @buf: buffer to store date | |
270 | * @len: number of bytes to read | |
1da177e4 | 271 | * |
7854d3f7 | 272 | * Default read function for 8bit buswidth. |
1da177e4 | 273 | */ |
58dd8f2b | 274 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 | 275 | { |
ace4dfee | 276 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 277 | |
76413839 | 278 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
1da177e4 LT |
279 | } |
280 | ||
1da177e4 LT |
281 | /** |
282 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
8b6e50c9 BN |
283 | * @mtd: MTD device structure |
284 | * @buf: data buffer | |
285 | * @len: number of bytes to write | |
1da177e4 | 286 | * |
7854d3f7 | 287 | * Default write function for 16bit buswidth. |
1da177e4 | 288 | */ |
58dd8f2b | 289 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
1da177e4 | 290 | { |
ace4dfee | 291 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 292 | u16 *p = (u16 *) buf; |
61b03bd7 | 293 | |
76413839 | 294 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
1da177e4 LT |
295 | } |
296 | ||
297 | /** | |
61b03bd7 | 298 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
8b6e50c9 BN |
299 | * @mtd: MTD device structure |
300 | * @buf: buffer to store date | |
301 | * @len: number of bytes to read | |
1da177e4 | 302 | * |
7854d3f7 | 303 | * Default read function for 16bit buswidth. |
1da177e4 | 304 | */ |
58dd8f2b | 305 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
1da177e4 | 306 | { |
ace4dfee | 307 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 308 | u16 *p = (u16 *) buf; |
1da177e4 | 309 | |
76413839 | 310 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
1da177e4 LT |
311 | } |
312 | ||
1da177e4 LT |
313 | /** |
314 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
8b6e50c9 BN |
315 | * @mtd: MTD device structure |
316 | * @ofs: offset from device start | |
317 | * @getchip: 0, if the chip is already selected | |
1da177e4 | 318 | * |
61b03bd7 | 319 | * Check, if the block is bad. |
1da177e4 LT |
320 | */ |
321 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
322 | { | |
cdbec050 | 323 | int page, chipnr, res = 0, i = 0; |
ace4dfee | 324 | struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
325 | u16 bad; |
326 | ||
5fb1549d | 327 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
b60b08b0 KC |
328 | ofs += mtd->erasesize - mtd->writesize; |
329 | ||
1a12f46a TK |
330 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
331 | ||
1da177e4 | 332 | if (getchip) { |
ace4dfee | 333 | chipnr = (int)(ofs >> chip->chip_shift); |
1da177e4 | 334 | |
6a8214aa | 335 | nand_get_device(mtd, FL_READING); |
1da177e4 LT |
336 | |
337 | /* Select the NAND device */ | |
ace4dfee | 338 | chip->select_chip(mtd, chipnr); |
1a12f46a | 339 | } |
1da177e4 | 340 | |
cdbec050 BN |
341 | do { |
342 | if (chip->options & NAND_BUSWIDTH_16) { | |
343 | chip->cmdfunc(mtd, NAND_CMD_READOOB, | |
344 | chip->badblockpos & 0xFE, page); | |
345 | bad = cpu_to_le16(chip->read_word(mtd)); | |
346 | if (chip->badblockpos & 0x1) | |
347 | bad >>= 8; | |
348 | else | |
349 | bad &= 0xFF; | |
350 | } else { | |
351 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, | |
352 | page); | |
353 | bad = chip->read_byte(mtd); | |
354 | } | |
355 | ||
356 | if (likely(chip->badblockbits == 8)) | |
357 | res = bad != 0xFF; | |
e0b58d0a | 358 | else |
cdbec050 BN |
359 | res = hweight8(bad) < chip->badblockbits; |
360 | ofs += mtd->writesize; | |
361 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; | |
362 | i++; | |
363 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); | |
e0b58d0a | 364 | |
b0bb6903 HS |
365 | if (getchip) { |
366 | chip->select_chip(mtd, -1); | |
1da177e4 | 367 | nand_release_device(mtd); |
b0bb6903 | 368 | } |
61b03bd7 | 369 | |
1da177e4 LT |
370 | return res; |
371 | } | |
372 | ||
373 | /** | |
5a0edb25 | 374 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
8b6e50c9 BN |
375 | * @mtd: MTD device structure |
376 | * @ofs: offset from device start | |
1da177e4 | 377 | * |
8b6e50c9 | 378 | * This is the default implementation, which can be overridden by a hardware |
5a0edb25 BN |
379 | * specific driver. It provides the details for writing a bad block marker to a |
380 | * block. | |
381 | */ | |
382 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
383 | { | |
384 | struct nand_chip *chip = mtd->priv; | |
385 | struct mtd_oob_ops ops; | |
386 | uint8_t buf[2] = { 0, 0 }; | |
387 | int ret = 0, res, i = 0; | |
388 | ||
389 | ops.datbuf = NULL; | |
390 | ops.oobbuf = buf; | |
391 | ops.ooboffs = chip->badblockpos; | |
392 | if (chip->options & NAND_BUSWIDTH_16) { | |
393 | ops.ooboffs &= ~0x01; | |
394 | ops.len = ops.ooblen = 2; | |
395 | } else { | |
396 | ops.len = ops.ooblen = 1; | |
397 | } | |
398 | ops.mode = MTD_OPS_PLACE_OOB; | |
399 | ||
400 | /* Write to first/last page(s) if necessary */ | |
401 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) | |
402 | ofs += mtd->erasesize - mtd->writesize; | |
403 | do { | |
404 | res = nand_do_write_oob(mtd, ofs, &ops); | |
405 | if (!ret) | |
406 | ret = res; | |
407 | ||
408 | i++; | |
409 | ofs += mtd->writesize; | |
410 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); | |
411 | ||
412 | return ret; | |
413 | } | |
414 | ||
415 | /** | |
416 | * nand_block_markbad_lowlevel - mark a block bad | |
417 | * @mtd: MTD device structure | |
418 | * @ofs: offset from device start | |
419 | * | |
420 | * This function performs the generic NAND bad block marking steps (i.e., bad | |
421 | * block table(s) and/or marker(s)). We only allow the hardware driver to | |
422 | * specify how to write bad block markers to OOB (chip->block_markbad). | |
423 | * | |
b32843b7 | 424 | * We try operations in the following order: |
e2414f4c | 425 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
b32843b7 BN |
426 | * (2) write bad block marker to OOB area of affected block (unless flag |
427 | * NAND_BBT_NO_OOB_BBM is present) | |
428 | * (3) update the BBT | |
429 | * Note that we retain the first error encountered in (2) or (3), finish the | |
e2414f4c | 430 | * procedures, and dump the error in the end. |
1da177e4 | 431 | */ |
5a0edb25 | 432 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 433 | { |
ace4dfee | 434 | struct nand_chip *chip = mtd->priv; |
b32843b7 | 435 | int res, ret = 0; |
61b03bd7 | 436 | |
b32843b7 | 437 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
00918429 BN |
438 | struct erase_info einfo; |
439 | ||
440 | /* Attempt erase before marking OOB */ | |
441 | memset(&einfo, 0, sizeof(einfo)); | |
442 | einfo.mtd = mtd; | |
443 | einfo.addr = ofs; | |
daae74ca | 444 | einfo.len = 1ULL << chip->phys_erase_shift; |
00918429 | 445 | nand_erase_nand(mtd, &einfo, 0); |
1da177e4 | 446 | |
b32843b7 | 447 | /* Write bad block marker to OOB */ |
6a8214aa | 448 | nand_get_device(mtd, FL_WRITING); |
5a0edb25 | 449 | ret = chip->block_markbad(mtd, ofs); |
c0b8ba7b | 450 | nand_release_device(mtd); |
f1a28c02 | 451 | } |
e2414f4c | 452 | |
b32843b7 BN |
453 | /* Mark block bad in BBT */ |
454 | if (chip->bbt) { | |
455 | res = nand_markbad_bbt(mtd, ofs); | |
e2414f4c BN |
456 | if (!ret) |
457 | ret = res; | |
458 | } | |
459 | ||
f1a28c02 TG |
460 | if (!ret) |
461 | mtd->ecc_stats.badblocks++; | |
c0b8ba7b | 462 | |
f1a28c02 | 463 | return ret; |
1da177e4 LT |
464 | } |
465 | ||
61b03bd7 | 466 | /** |
1da177e4 | 467 | * nand_check_wp - [GENERIC] check if the chip is write protected |
8b6e50c9 | 468 | * @mtd: MTD device structure |
1da177e4 | 469 | * |
8b6e50c9 BN |
470 | * Check, if the device is write protected. The function expects, that the |
471 | * device is already selected. | |
1da177e4 | 472 | */ |
e0c7d767 | 473 | static int nand_check_wp(struct mtd_info *mtd) |
1da177e4 | 474 | { |
ace4dfee | 475 | struct nand_chip *chip = mtd->priv; |
93edbad6 | 476 | |
8b6e50c9 | 477 | /* Broken xD cards report WP despite being writable */ |
93edbad6 ML |
478 | if (chip->options & NAND_BROKEN_XD) |
479 | return 0; | |
480 | ||
1da177e4 | 481 | /* Check the WP bit */ |
ace4dfee TG |
482 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
483 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; | |
1da177e4 LT |
484 | } |
485 | ||
486 | /** | |
487 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad | |
8b6e50c9 BN |
488 | * @mtd: MTD device structure |
489 | * @ofs: offset from device start | |
490 | * @getchip: 0, if the chip is already selected | |
491 | * @allowbbt: 1, if its allowed to access the bbt area | |
1da177e4 LT |
492 | * |
493 | * Check, if the block is bad. Either by reading the bad block table or | |
494 | * calling of the scan function. | |
495 | */ | |
2c0a2bed TG |
496 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, |
497 | int allowbbt) | |
1da177e4 | 498 | { |
ace4dfee | 499 | struct nand_chip *chip = mtd->priv; |
61b03bd7 | 500 | |
ace4dfee TG |
501 | if (!chip->bbt) |
502 | return chip->block_bad(mtd, ofs, getchip); | |
61b03bd7 | 503 | |
1da177e4 | 504 | /* Return info from the table */ |
e0c7d767 | 505 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
1da177e4 LT |
506 | } |
507 | ||
2af7c653 SK |
508 | /** |
509 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. | |
8b6e50c9 BN |
510 | * @mtd: MTD device structure |
511 | * @timeo: Timeout | |
2af7c653 SK |
512 | * |
513 | * Helper function for nand_wait_ready used when needing to wait in interrupt | |
514 | * context. | |
515 | */ | |
516 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) | |
517 | { | |
518 | struct nand_chip *chip = mtd->priv; | |
519 | int i; | |
520 | ||
521 | /* Wait for the device to get ready */ | |
522 | for (i = 0; i < timeo; i++) { | |
523 | if (chip->dev_ready(mtd)) | |
524 | break; | |
525 | touch_softlockup_watchdog(); | |
526 | mdelay(1); | |
527 | } | |
528 | } | |
529 | ||
7854d3f7 | 530 | /* Wait for the ready pin, after a command. The timeout is caught later. */ |
4b648b02 | 531 | void nand_wait_ready(struct mtd_info *mtd) |
3b88775c | 532 | { |
ace4dfee | 533 | struct nand_chip *chip = mtd->priv; |
ca6a2489 | 534 | unsigned long timeo = jiffies + msecs_to_jiffies(20); |
3b88775c | 535 | |
2af7c653 SK |
536 | /* 400ms timeout */ |
537 | if (in_interrupt() || oops_in_progress) | |
538 | return panic_nand_wait_ready(mtd, 400); | |
539 | ||
8fe833c1 | 540 | led_trigger_event(nand_led_trigger, LED_FULL); |
7854d3f7 | 541 | /* Wait until command is processed or timeout occurs */ |
3b88775c | 542 | do { |
ace4dfee | 543 | if (chip->dev_ready(mtd)) |
8fe833c1 | 544 | break; |
8446f1d3 | 545 | touch_softlockup_watchdog(); |
61b03bd7 | 546 | } while (time_before(jiffies, timeo)); |
8fe833c1 | 547 | led_trigger_event(nand_led_trigger, LED_OFF); |
3b88775c | 548 | } |
4b648b02 | 549 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
3b88775c | 550 | |
1da177e4 LT |
551 | /** |
552 | * nand_command - [DEFAULT] Send command to NAND device | |
8b6e50c9 BN |
553 | * @mtd: MTD device structure |
554 | * @command: the command to be sent | |
555 | * @column: the column address for this command, -1 if none | |
556 | * @page_addr: the page address for this command, -1 if none | |
1da177e4 | 557 | * |
8b6e50c9 | 558 | * Send command to NAND device. This function is used for small page devices |
51148f1f | 559 | * (512 Bytes per page). |
1da177e4 | 560 | */ |
7abd3ef9 TG |
561 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
562 | int column, int page_addr) | |
1da177e4 | 563 | { |
ace4dfee | 564 | register struct nand_chip *chip = mtd->priv; |
7abd3ef9 | 565 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
1da177e4 | 566 | |
8b6e50c9 | 567 | /* Write out the command to the device */ |
1da177e4 LT |
568 | if (command == NAND_CMD_SEQIN) { |
569 | int readcmd; | |
570 | ||
28318776 | 571 | if (column >= mtd->writesize) { |
1da177e4 | 572 | /* OOB area */ |
28318776 | 573 | column -= mtd->writesize; |
1da177e4 LT |
574 | readcmd = NAND_CMD_READOOB; |
575 | } else if (column < 256) { | |
576 | /* First 256 bytes --> READ0 */ | |
577 | readcmd = NAND_CMD_READ0; | |
578 | } else { | |
579 | column -= 256; | |
580 | readcmd = NAND_CMD_READ1; | |
581 | } | |
ace4dfee | 582 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
7abd3ef9 | 583 | ctrl &= ~NAND_CTRL_CHANGE; |
1da177e4 | 584 | } |
ace4dfee | 585 | chip->cmd_ctrl(mtd, command, ctrl); |
1da177e4 | 586 | |
8b6e50c9 | 587 | /* Address cycle, when necessary */ |
7abd3ef9 TG |
588 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
589 | /* Serially input address */ | |
590 | if (column != -1) { | |
591 | /* Adjust columns for 16 bit buswidth */ | |
3dad2344 BN |
592 | if (chip->options & NAND_BUSWIDTH_16 && |
593 | !nand_opcode_8bits(command)) | |
7abd3ef9 | 594 | column >>= 1; |
ace4dfee | 595 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 TG |
596 | ctrl &= ~NAND_CTRL_CHANGE; |
597 | } | |
598 | if (page_addr != -1) { | |
ace4dfee | 599 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
7abd3ef9 | 600 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 601 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
7abd3ef9 | 602 | /* One more address cycle for devices > 32MiB */ |
ace4dfee TG |
603 | if (chip->chipsize > (32 << 20)) |
604 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); | |
1da177e4 | 605 | } |
ace4dfee | 606 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
607 | |
608 | /* | |
8b6e50c9 BN |
609 | * Program and erase have their own busy handlers status and sequential |
610 | * in needs no delay | |
e0c7d767 | 611 | */ |
1da177e4 | 612 | switch (command) { |
61b03bd7 | 613 | |
1da177e4 LT |
614 | case NAND_CMD_PAGEPROG: |
615 | case NAND_CMD_ERASE1: | |
616 | case NAND_CMD_ERASE2: | |
617 | case NAND_CMD_SEQIN: | |
618 | case NAND_CMD_STATUS: | |
619 | return; | |
620 | ||
621 | case NAND_CMD_RESET: | |
ace4dfee | 622 | if (chip->dev_ready) |
1da177e4 | 623 | break; |
ace4dfee TG |
624 | udelay(chip->chip_delay); |
625 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, | |
7abd3ef9 | 626 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
12efdde3 TG |
627 | chip->cmd_ctrl(mtd, |
628 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); | |
f8ac0414 FF |
629 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
630 | ; | |
1da177e4 LT |
631 | return; |
632 | ||
e0c7d767 | 633 | /* This applies to read commands */ |
1da177e4 | 634 | default: |
61b03bd7 | 635 | /* |
1da177e4 LT |
636 | * If we don't have access to the busy pin, we apply the given |
637 | * command delay | |
e0c7d767 | 638 | */ |
ace4dfee TG |
639 | if (!chip->dev_ready) { |
640 | udelay(chip->chip_delay); | |
1da177e4 | 641 | return; |
61b03bd7 | 642 | } |
1da177e4 | 643 | } |
8b6e50c9 BN |
644 | /* |
645 | * Apply this short delay always to ensure that we do wait tWB in | |
646 | * any case on any machine. | |
647 | */ | |
e0c7d767 | 648 | ndelay(100); |
3b88775c TG |
649 | |
650 | nand_wait_ready(mtd); | |
1da177e4 LT |
651 | } |
652 | ||
653 | /** | |
654 | * nand_command_lp - [DEFAULT] Send command to NAND large page device | |
8b6e50c9 BN |
655 | * @mtd: MTD device structure |
656 | * @command: the command to be sent | |
657 | * @column: the column address for this command, -1 if none | |
658 | * @page_addr: the page address for this command, -1 if none | |
1da177e4 | 659 | * |
7abd3ef9 | 660 | * Send command to NAND device. This is the version for the new large page |
7854d3f7 BN |
661 | * devices. We don't have the separate regions as we have in the small page |
662 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. | |
1da177e4 | 663 | */ |
7abd3ef9 TG |
664 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
665 | int column, int page_addr) | |
1da177e4 | 666 | { |
ace4dfee | 667 | register struct nand_chip *chip = mtd->priv; |
1da177e4 LT |
668 | |
669 | /* Emulate NAND_CMD_READOOB */ | |
670 | if (command == NAND_CMD_READOOB) { | |
28318776 | 671 | column += mtd->writesize; |
1da177e4 LT |
672 | command = NAND_CMD_READ0; |
673 | } | |
61b03bd7 | 674 | |
7abd3ef9 | 675 | /* Command latch cycle */ |
fb066ada | 676 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
1da177e4 LT |
677 | |
678 | if (column != -1 || page_addr != -1) { | |
7abd3ef9 | 679 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
1da177e4 LT |
680 | |
681 | /* Serially input address */ | |
682 | if (column != -1) { | |
683 | /* Adjust columns for 16 bit buswidth */ | |
3dad2344 BN |
684 | if (chip->options & NAND_BUSWIDTH_16 && |
685 | !nand_opcode_8bits(command)) | |
1da177e4 | 686 | column >>= 1; |
ace4dfee | 687 | chip->cmd_ctrl(mtd, column, ctrl); |
7abd3ef9 | 688 | ctrl &= ~NAND_CTRL_CHANGE; |
ace4dfee | 689 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
61b03bd7 | 690 | } |
1da177e4 | 691 | if (page_addr != -1) { |
ace4dfee TG |
692 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
693 | chip->cmd_ctrl(mtd, page_addr >> 8, | |
7abd3ef9 | 694 | NAND_NCE | NAND_ALE); |
1da177e4 | 695 | /* One more address cycle for devices > 128MiB */ |
ace4dfee TG |
696 | if (chip->chipsize > (128 << 20)) |
697 | chip->cmd_ctrl(mtd, page_addr >> 16, | |
7abd3ef9 | 698 | NAND_NCE | NAND_ALE); |
1da177e4 | 699 | } |
1da177e4 | 700 | } |
ace4dfee | 701 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
61b03bd7 TG |
702 | |
703 | /* | |
8b6e50c9 BN |
704 | * Program and erase have their own busy handlers status, sequential |
705 | * in, and deplete1 need no delay. | |
30f464b7 | 706 | */ |
1da177e4 | 707 | switch (command) { |
61b03bd7 | 708 | |
1da177e4 LT |
709 | case NAND_CMD_CACHEDPROG: |
710 | case NAND_CMD_PAGEPROG: | |
711 | case NAND_CMD_ERASE1: | |
712 | case NAND_CMD_ERASE2: | |
713 | case NAND_CMD_SEQIN: | |
7bc3312b | 714 | case NAND_CMD_RNDIN: |
1da177e4 | 715 | case NAND_CMD_STATUS: |
30f464b7 | 716 | return; |
1da177e4 LT |
717 | |
718 | case NAND_CMD_RESET: | |
ace4dfee | 719 | if (chip->dev_ready) |
1da177e4 | 720 | break; |
ace4dfee | 721 | udelay(chip->chip_delay); |
12efdde3 TG |
722 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
723 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
724 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
725 | NAND_NCE | NAND_CTRL_CHANGE); | |
f8ac0414 FF |
726 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) |
727 | ; | |
1da177e4 LT |
728 | return; |
729 | ||
7bc3312b TG |
730 | case NAND_CMD_RNDOUT: |
731 | /* No ready / busy check necessary */ | |
732 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, | |
733 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
734 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
735 | NAND_NCE | NAND_CTRL_CHANGE); | |
736 | return; | |
737 | ||
1da177e4 | 738 | case NAND_CMD_READ0: |
12efdde3 TG |
739 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
740 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); | |
741 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, | |
742 | NAND_NCE | NAND_CTRL_CHANGE); | |
61b03bd7 | 743 | |
e0c7d767 | 744 | /* This applies to read commands */ |
1da177e4 | 745 | default: |
61b03bd7 | 746 | /* |
1da177e4 | 747 | * If we don't have access to the busy pin, we apply the given |
8b6e50c9 | 748 | * command delay. |
e0c7d767 | 749 | */ |
ace4dfee TG |
750 | if (!chip->dev_ready) { |
751 | udelay(chip->chip_delay); | |
1da177e4 | 752 | return; |
61b03bd7 | 753 | } |
1da177e4 | 754 | } |
3b88775c | 755 | |
8b6e50c9 BN |
756 | /* |
757 | * Apply this short delay always to ensure that we do wait tWB in | |
758 | * any case on any machine. | |
759 | */ | |
e0c7d767 | 760 | ndelay(100); |
3b88775c TG |
761 | |
762 | nand_wait_ready(mtd); | |
1da177e4 LT |
763 | } |
764 | ||
2af7c653 SK |
765 | /** |
766 | * panic_nand_get_device - [GENERIC] Get chip for selected access | |
8b6e50c9 BN |
767 | * @chip: the nand chip descriptor |
768 | * @mtd: MTD device structure | |
769 | * @new_state: the state which is requested | |
2af7c653 SK |
770 | * |
771 | * Used when in panic, no locks are taken. | |
772 | */ | |
773 | static void panic_nand_get_device(struct nand_chip *chip, | |
774 | struct mtd_info *mtd, int new_state) | |
775 | { | |
7854d3f7 | 776 | /* Hardware controller shared among independent devices */ |
2af7c653 SK |
777 | chip->controller->active = chip; |
778 | chip->state = new_state; | |
779 | } | |
780 | ||
1da177e4 LT |
781 | /** |
782 | * nand_get_device - [GENERIC] Get chip for selected access | |
8b6e50c9 BN |
783 | * @mtd: MTD device structure |
784 | * @new_state: the state which is requested | |
1da177e4 LT |
785 | * |
786 | * Get the device and lock it for exclusive access | |
787 | */ | |
2c0a2bed | 788 | static int |
6a8214aa | 789 | nand_get_device(struct mtd_info *mtd, int new_state) |
1da177e4 | 790 | { |
6a8214aa | 791 | struct nand_chip *chip = mtd->priv; |
ace4dfee TG |
792 | spinlock_t *lock = &chip->controller->lock; |
793 | wait_queue_head_t *wq = &chip->controller->wq; | |
e0c7d767 | 794 | DECLARE_WAITQUEUE(wait, current); |
7351d3a5 | 795 | retry: |
0dfc6246 TG |
796 | spin_lock(lock); |
797 | ||
b8b3ee9a | 798 | /* Hardware controller shared among independent devices */ |
ace4dfee TG |
799 | if (!chip->controller->active) |
800 | chip->controller->active = chip; | |
a36ed299 | 801 | |
ace4dfee TG |
802 | if (chip->controller->active == chip && chip->state == FL_READY) { |
803 | chip->state = new_state; | |
0dfc6246 | 804 | spin_unlock(lock); |
962034f4 VW |
805 | return 0; |
806 | } | |
807 | if (new_state == FL_PM_SUSPENDED) { | |
6b0d9a84 LY |
808 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
809 | chip->state = FL_PM_SUSPENDED; | |
810 | spin_unlock(lock); | |
811 | return 0; | |
6b0d9a84 | 812 | } |
0dfc6246 TG |
813 | } |
814 | set_current_state(TASK_UNINTERRUPTIBLE); | |
815 | add_wait_queue(wq, &wait); | |
816 | spin_unlock(lock); | |
817 | schedule(); | |
818 | remove_wait_queue(wq, &wait); | |
1da177e4 LT |
819 | goto retry; |
820 | } | |
821 | ||
2af7c653 | 822 | /** |
8b6e50c9 BN |
823 | * panic_nand_wait - [GENERIC] wait until the command is done |
824 | * @mtd: MTD device structure | |
825 | * @chip: NAND chip structure | |
826 | * @timeo: timeout | |
2af7c653 SK |
827 | * |
828 | * Wait for command done. This is a helper function for nand_wait used when | |
829 | * we are in interrupt context. May happen when in panic and trying to write | |
b595076a | 830 | * an oops through mtdoops. |
2af7c653 SK |
831 | */ |
832 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, | |
833 | unsigned long timeo) | |
834 | { | |
835 | int i; | |
836 | for (i = 0; i < timeo; i++) { | |
837 | if (chip->dev_ready) { | |
838 | if (chip->dev_ready(mtd)) | |
839 | break; | |
840 | } else { | |
841 | if (chip->read_byte(mtd) & NAND_STATUS_READY) | |
842 | break; | |
843 | } | |
844 | mdelay(1); | |
f8ac0414 | 845 | } |
2af7c653 SK |
846 | } |
847 | ||
1da177e4 | 848 | /** |
8b6e50c9 BN |
849 | * nand_wait - [DEFAULT] wait until the command is done |
850 | * @mtd: MTD device structure | |
851 | * @chip: NAND chip structure | |
1da177e4 | 852 | * |
8b6e50c9 BN |
853 | * Wait for command done. This applies to erase and program only. Erase can |
854 | * take up to 400ms and program up to 20ms according to general NAND and | |
855 | * SmartMedia specs. | |
844d3b42 | 856 | */ |
7bc3312b | 857 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
1da177e4 LT |
858 | { |
859 | ||
7bc3312b | 860 | int status, state = chip->state; |
6d2559f8 | 861 | unsigned long timeo = (state == FL_ERASING ? 400 : 20); |
1da177e4 | 862 | |
8fe833c1 RP |
863 | led_trigger_event(nand_led_trigger, LED_FULL); |
864 | ||
8b6e50c9 BN |
865 | /* |
866 | * Apply this short delay always to ensure that we do wait tWB in any | |
867 | * case on any machine. | |
868 | */ | |
e0c7d767 | 869 | ndelay(100); |
1da177e4 | 870 | |
14c65786 | 871 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
1da177e4 | 872 | |
2af7c653 SK |
873 | if (in_interrupt() || oops_in_progress) |
874 | panic_nand_wait(mtd, chip, timeo); | |
875 | else { | |
6d2559f8 | 876 | timeo = jiffies + msecs_to_jiffies(timeo); |
2af7c653 SK |
877 | while (time_before(jiffies, timeo)) { |
878 | if (chip->dev_ready) { | |
879 | if (chip->dev_ready(mtd)) | |
880 | break; | |
881 | } else { | |
882 | if (chip->read_byte(mtd) & NAND_STATUS_READY) | |
883 | break; | |
884 | } | |
885 | cond_resched(); | |
1da177e4 | 886 | } |
1da177e4 | 887 | } |
8fe833c1 RP |
888 | led_trigger_event(nand_led_trigger, LED_OFF); |
889 | ||
ace4dfee | 890 | status = (int)chip->read_byte(mtd); |
f251b8df MC |
891 | /* This can happen if in case of timeout or buggy dev_ready */ |
892 | WARN_ON(!(status & NAND_STATUS_READY)); | |
1da177e4 LT |
893 | return status; |
894 | } | |
895 | ||
7d70f334 | 896 | /** |
b6d676db | 897 | * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
b6d676db RD |
898 | * @mtd: mtd info |
899 | * @ofs: offset to start unlock from | |
900 | * @len: length to unlock | |
8b6e50c9 BN |
901 | * @invert: when = 0, unlock the range of blocks within the lower and |
902 | * upper boundary address | |
903 | * when = 1, unlock the range of blocks outside the boundaries | |
904 | * of the lower and upper boundary address | |
7d70f334 | 905 | * |
8b6e50c9 | 906 | * Returs unlock status. |
7d70f334 VS |
907 | */ |
908 | static int __nand_unlock(struct mtd_info *mtd, loff_t ofs, | |
909 | uint64_t len, int invert) | |
910 | { | |
911 | int ret = 0; | |
912 | int status, page; | |
913 | struct nand_chip *chip = mtd->priv; | |
914 | ||
915 | /* Submit address of first page to unlock */ | |
916 | page = ofs >> chip->page_shift; | |
917 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); | |
918 | ||
919 | /* Submit address of last page to unlock */ | |
920 | page = (ofs + len) >> chip->page_shift; | |
921 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, | |
922 | (page | invert) & chip->pagemask); | |
923 | ||
924 | /* Call wait ready function */ | |
925 | status = chip->waitfunc(mtd, chip); | |
7d70f334 | 926 | /* See if device thinks it succeeded */ |
74830966 | 927 | if (status & NAND_STATUS_FAIL) { |
289c0522 | 928 | pr_debug("%s: error status = 0x%08x\n", |
7d70f334 VS |
929 | __func__, status); |
930 | ret = -EIO; | |
931 | } | |
932 | ||
933 | return ret; | |
934 | } | |
935 | ||
936 | /** | |
b6d676db | 937 | * nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
b6d676db RD |
938 | * @mtd: mtd info |
939 | * @ofs: offset to start unlock from | |
940 | * @len: length to unlock | |
7d70f334 | 941 | * |
8b6e50c9 | 942 | * Returns unlock status. |
7d70f334 VS |
943 | */ |
944 | int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
945 | { | |
946 | int ret = 0; | |
947 | int chipnr; | |
948 | struct nand_chip *chip = mtd->priv; | |
949 | ||
289c0522 | 950 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
7d70f334 VS |
951 | __func__, (unsigned long long)ofs, len); |
952 | ||
953 | if (check_offs_len(mtd, ofs, len)) | |
954 | ret = -EINVAL; | |
955 | ||
956 | /* Align to last block address if size addresses end of the device */ | |
957 | if (ofs + len == mtd->size) | |
958 | len -= mtd->erasesize; | |
959 | ||
6a8214aa | 960 | nand_get_device(mtd, FL_UNLOCKING); |
7d70f334 VS |
961 | |
962 | /* Shift to get chip number */ | |
963 | chipnr = ofs >> chip->chip_shift; | |
964 | ||
965 | chip->select_chip(mtd, chipnr); | |
966 | ||
967 | /* Check, if it is write protected */ | |
968 | if (nand_check_wp(mtd)) { | |
289c0522 | 969 | pr_debug("%s: device is write protected!\n", |
7d70f334 VS |
970 | __func__); |
971 | ret = -EIO; | |
972 | goto out; | |
973 | } | |
974 | ||
975 | ret = __nand_unlock(mtd, ofs, len, 0); | |
976 | ||
977 | out: | |
b0bb6903 | 978 | chip->select_chip(mtd, -1); |
7d70f334 VS |
979 | nand_release_device(mtd); |
980 | ||
981 | return ret; | |
982 | } | |
7351d3a5 | 983 | EXPORT_SYMBOL(nand_unlock); |
7d70f334 VS |
984 | |
985 | /** | |
b6d676db | 986 | * nand_lock - [REPLACEABLE] locks all blocks present in the device |
b6d676db RD |
987 | * @mtd: mtd info |
988 | * @ofs: offset to start unlock from | |
989 | * @len: length to unlock | |
7d70f334 | 990 | * |
8b6e50c9 BN |
991 | * This feature is not supported in many NAND parts. 'Micron' NAND parts do |
992 | * have this feature, but it allows only to lock all blocks, not for specified | |
993 | * range for block. Implementing 'lock' feature by making use of 'unlock', for | |
994 | * now. | |
7d70f334 | 995 | * |
8b6e50c9 | 996 | * Returns lock status. |
7d70f334 VS |
997 | */ |
998 | int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
999 | { | |
1000 | int ret = 0; | |
1001 | int chipnr, status, page; | |
1002 | struct nand_chip *chip = mtd->priv; | |
1003 | ||
289c0522 | 1004 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
7d70f334 VS |
1005 | __func__, (unsigned long long)ofs, len); |
1006 | ||
1007 | if (check_offs_len(mtd, ofs, len)) | |
1008 | ret = -EINVAL; | |
1009 | ||
6a8214aa | 1010 | nand_get_device(mtd, FL_LOCKING); |
7d70f334 VS |
1011 | |
1012 | /* Shift to get chip number */ | |
1013 | chipnr = ofs >> chip->chip_shift; | |
1014 | ||
1015 | chip->select_chip(mtd, chipnr); | |
1016 | ||
1017 | /* Check, if it is write protected */ | |
1018 | if (nand_check_wp(mtd)) { | |
289c0522 | 1019 | pr_debug("%s: device is write protected!\n", |
7d70f334 VS |
1020 | __func__); |
1021 | status = MTD_ERASE_FAILED; | |
1022 | ret = -EIO; | |
1023 | goto out; | |
1024 | } | |
1025 | ||
1026 | /* Submit address of first page to lock */ | |
1027 | page = ofs >> chip->page_shift; | |
1028 | chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask); | |
1029 | ||
1030 | /* Call wait ready function */ | |
1031 | status = chip->waitfunc(mtd, chip); | |
7d70f334 | 1032 | /* See if device thinks it succeeded */ |
74830966 | 1033 | if (status & NAND_STATUS_FAIL) { |
289c0522 | 1034 | pr_debug("%s: error status = 0x%08x\n", |
7d70f334 VS |
1035 | __func__, status); |
1036 | ret = -EIO; | |
1037 | goto out; | |
1038 | } | |
1039 | ||
1040 | ret = __nand_unlock(mtd, ofs, len, 0x1); | |
1041 | ||
1042 | out: | |
b0bb6903 | 1043 | chip->select_chip(mtd, -1); |
7d70f334 VS |
1044 | nand_release_device(mtd); |
1045 | ||
1046 | return ret; | |
1047 | } | |
7351d3a5 | 1048 | EXPORT_SYMBOL(nand_lock); |
7d70f334 | 1049 | |
8593fbc6 | 1050 | /** |
7854d3f7 | 1051 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
8b6e50c9 BN |
1052 | * @mtd: mtd info structure |
1053 | * @chip: nand chip info structure | |
1054 | * @buf: buffer to store read data | |
1fbb938d | 1055 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1056 | * @page: page number to read |
52ff49df | 1057 | * |
7854d3f7 | 1058 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
8593fbc6 TG |
1059 | */ |
1060 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, | |
1fbb938d | 1061 | uint8_t *buf, int oob_required, int page) |
8593fbc6 TG |
1062 | { |
1063 | chip->read_buf(mtd, buf, mtd->writesize); | |
279f08d4 BN |
1064 | if (oob_required) |
1065 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
8593fbc6 TG |
1066 | return 0; |
1067 | } | |
1068 | ||
52ff49df | 1069 | /** |
7854d3f7 | 1070 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
8b6e50c9 BN |
1071 | * @mtd: mtd info structure |
1072 | * @chip: nand chip info structure | |
1073 | * @buf: buffer to store read data | |
1fbb938d | 1074 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1075 | * @page: page number to read |
52ff49df DB |
1076 | * |
1077 | * We need a special oob layout and handling even when OOB isn't used. | |
1078 | */ | |
7351d3a5 | 1079 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
1fbb938d BN |
1080 | struct nand_chip *chip, uint8_t *buf, |
1081 | int oob_required, int page) | |
52ff49df DB |
1082 | { |
1083 | int eccsize = chip->ecc.size; | |
1084 | int eccbytes = chip->ecc.bytes; | |
1085 | uint8_t *oob = chip->oob_poi; | |
1086 | int steps, size; | |
1087 | ||
1088 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
1089 | chip->read_buf(mtd, buf, eccsize); | |
1090 | buf += eccsize; | |
1091 | ||
1092 | if (chip->ecc.prepad) { | |
1093 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
1094 | oob += chip->ecc.prepad; | |
1095 | } | |
1096 | ||
1097 | chip->read_buf(mtd, oob, eccbytes); | |
1098 | oob += eccbytes; | |
1099 | ||
1100 | if (chip->ecc.postpad) { | |
1101 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
1102 | oob += chip->ecc.postpad; | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | size = mtd->oobsize - (oob - chip->oob_poi); | |
1107 | if (size) | |
1108 | chip->read_buf(mtd, oob, size); | |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
1da177e4 | 1113 | /** |
7854d3f7 | 1114 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
8b6e50c9 BN |
1115 | * @mtd: mtd info structure |
1116 | * @chip: nand chip info structure | |
1117 | * @buf: buffer to store read data | |
1fbb938d | 1118 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1119 | * @page: page number to read |
068e3c0a | 1120 | */ |
f5bbdacc | 1121 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1122 | uint8_t *buf, int oob_required, int page) |
1da177e4 | 1123 | { |
f5bbdacc TG |
1124 | int i, eccsize = chip->ecc.size; |
1125 | int eccbytes = chip->ecc.bytes; | |
1126 | int eccsteps = chip->ecc.steps; | |
1127 | uint8_t *p = buf; | |
4bf63fcb DW |
1128 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
1129 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 1130 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
3f91e94f | 1131 | unsigned int max_bitflips = 0; |
f5bbdacc | 1132 | |
1fbb938d | 1133 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
f5bbdacc TG |
1134 | |
1135 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) | |
1136 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1137 | ||
1138 | for (i = 0; i < chip->ecc.total; i++) | |
f75e5097 | 1139 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
f5bbdacc TG |
1140 | |
1141 | eccsteps = chip->ecc.steps; | |
1142 | p = buf; | |
1143 | ||
1144 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1145 | int stat; | |
1146 | ||
1147 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); | |
3f91e94f | 1148 | if (stat < 0) { |
f5bbdacc | 1149 | mtd->ecc_stats.failed++; |
3f91e94f | 1150 | } else { |
f5bbdacc | 1151 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1152 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1153 | } | |
f5bbdacc | 1154 | } |
3f91e94f | 1155 | return max_bitflips; |
22c60f5f | 1156 | } |
1da177e4 | 1157 | |
3d459559 | 1158 | /** |
837a6ba4 | 1159 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
8b6e50c9 BN |
1160 | * @mtd: mtd info structure |
1161 | * @chip: nand chip info structure | |
1162 | * @data_offs: offset of requested data within the page | |
1163 | * @readlen: data length | |
1164 | * @bufpoi: buffer to store read data | |
3d459559 | 1165 | */ |
7351d3a5 FF |
1166 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
1167 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi) | |
3d459559 AK |
1168 | { |
1169 | int start_step, end_step, num_steps; | |
1170 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
1171 | uint8_t *p; | |
1172 | int data_col_addr, i, gaps = 0; | |
1173 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; | |
1174 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; | |
7351d3a5 | 1175 | int index = 0; |
3f91e94f | 1176 | unsigned int max_bitflips = 0; |
3d459559 | 1177 | |
7854d3f7 | 1178 | /* Column address within the page aligned to ECC size (256bytes) */ |
3d459559 AK |
1179 | start_step = data_offs / chip->ecc.size; |
1180 | end_step = (data_offs + readlen - 1) / chip->ecc.size; | |
1181 | num_steps = end_step - start_step + 1; | |
1182 | ||
8b6e50c9 | 1183 | /* Data size aligned to ECC ecc.size */ |
3d459559 AK |
1184 | datafrag_len = num_steps * chip->ecc.size; |
1185 | eccfrag_len = num_steps * chip->ecc.bytes; | |
1186 | ||
1187 | data_col_addr = start_step * chip->ecc.size; | |
1188 | /* If we read not a page aligned data */ | |
1189 | if (data_col_addr != 0) | |
1190 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); | |
1191 | ||
1192 | p = bufpoi + data_col_addr; | |
1193 | chip->read_buf(mtd, p, datafrag_len); | |
1194 | ||
8b6e50c9 | 1195 | /* Calculate ECC */ |
3d459559 AK |
1196 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
1197 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); | |
1198 | ||
8b6e50c9 BN |
1199 | /* |
1200 | * The performance is faster if we position offsets according to | |
7854d3f7 | 1201 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
8b6e50c9 | 1202 | */ |
3d459559 AK |
1203 | for (i = 0; i < eccfrag_len - 1; i++) { |
1204 | if (eccpos[i + start_step * chip->ecc.bytes] + 1 != | |
1205 | eccpos[i + start_step * chip->ecc.bytes + 1]) { | |
1206 | gaps = 1; | |
1207 | break; | |
1208 | } | |
1209 | } | |
1210 | if (gaps) { | |
1211 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); | |
1212 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1213 | } else { | |
8b6e50c9 | 1214 | /* |
7854d3f7 | 1215 | * Send the command to read the particular ECC bytes take care |
8b6e50c9 BN |
1216 | * about buswidth alignment in read_buf. |
1217 | */ | |
7351d3a5 FF |
1218 | index = start_step * chip->ecc.bytes; |
1219 | ||
1220 | aligned_pos = eccpos[index] & ~(busw - 1); | |
3d459559 | 1221 | aligned_len = eccfrag_len; |
7351d3a5 | 1222 | if (eccpos[index] & (busw - 1)) |
3d459559 | 1223 | aligned_len++; |
7351d3a5 | 1224 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
3d459559 AK |
1225 | aligned_len++; |
1226 | ||
7351d3a5 FF |
1227 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
1228 | mtd->writesize + aligned_pos, -1); | |
3d459559 AK |
1229 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
1230 | } | |
1231 | ||
1232 | for (i = 0; i < eccfrag_len; i++) | |
7351d3a5 | 1233 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
3d459559 AK |
1234 | |
1235 | p = bufpoi + data_col_addr; | |
1236 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { | |
1237 | int stat; | |
1238 | ||
7351d3a5 FF |
1239 | stat = chip->ecc.correct(mtd, p, |
1240 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); | |
3f91e94f | 1241 | if (stat < 0) { |
3d459559 | 1242 | mtd->ecc_stats.failed++; |
3f91e94f | 1243 | } else { |
3d459559 | 1244 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1245 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1246 | } | |
3d459559 | 1247 | } |
3f91e94f | 1248 | return max_bitflips; |
3d459559 AK |
1249 | } |
1250 | ||
068e3c0a | 1251 | /** |
7854d3f7 | 1252 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
8b6e50c9 BN |
1253 | * @mtd: mtd info structure |
1254 | * @chip: nand chip info structure | |
1255 | * @buf: buffer to store read data | |
1fbb938d | 1256 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1257 | * @page: page number to read |
068e3c0a | 1258 | * |
7854d3f7 | 1259 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
068e3c0a | 1260 | */ |
f5bbdacc | 1261 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1262 | uint8_t *buf, int oob_required, int page) |
1da177e4 | 1263 | { |
f5bbdacc TG |
1264 | int i, eccsize = chip->ecc.size; |
1265 | int eccbytes = chip->ecc.bytes; | |
1266 | int eccsteps = chip->ecc.steps; | |
1267 | uint8_t *p = buf; | |
4bf63fcb DW |
1268 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
1269 | uint8_t *ecc_code = chip->buffers->ecccode; | |
8b099a39 | 1270 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
3f91e94f | 1271 | unsigned int max_bitflips = 0; |
f5bbdacc TG |
1272 | |
1273 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1274 | chip->ecc.hwctl(mtd, NAND_ECC_READ); | |
1275 | chip->read_buf(mtd, p, eccsize); | |
1276 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1da177e4 | 1277 | } |
f75e5097 | 1278 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
1da177e4 | 1279 | |
f5bbdacc | 1280 | for (i = 0; i < chip->ecc.total; i++) |
f75e5097 | 1281 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
1da177e4 | 1282 | |
f5bbdacc TG |
1283 | eccsteps = chip->ecc.steps; |
1284 | p = buf; | |
61b03bd7 | 1285 | |
f5bbdacc TG |
1286 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1287 | int stat; | |
1da177e4 | 1288 | |
f5bbdacc | 1289 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
3f91e94f | 1290 | if (stat < 0) { |
f5bbdacc | 1291 | mtd->ecc_stats.failed++; |
3f91e94f | 1292 | } else { |
f5bbdacc | 1293 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1294 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1295 | } | |
f5bbdacc | 1296 | } |
3f91e94f | 1297 | return max_bitflips; |
f5bbdacc | 1298 | } |
1da177e4 | 1299 | |
6e0cb135 | 1300 | /** |
7854d3f7 | 1301 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
8b6e50c9 BN |
1302 | * @mtd: mtd info structure |
1303 | * @chip: nand chip info structure | |
1304 | * @buf: buffer to store read data | |
1fbb938d | 1305 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1306 | * @page: page number to read |
6e0cb135 | 1307 | * |
8b6e50c9 BN |
1308 | * Hardware ECC for large page chips, require OOB to be read first. For this |
1309 | * ECC mode, the write_page method is re-used from ECC_HW. These methods | |
1310 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with | |
1311 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from | |
1312 | * the data area, by overwriting the NAND manufacturer bad block markings. | |
6e0cb135 SN |
1313 | */ |
1314 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, | |
1fbb938d | 1315 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
6e0cb135 SN |
1316 | { |
1317 | int i, eccsize = chip->ecc.size; | |
1318 | int eccbytes = chip->ecc.bytes; | |
1319 | int eccsteps = chip->ecc.steps; | |
1320 | uint8_t *p = buf; | |
1321 | uint8_t *ecc_code = chip->buffers->ecccode; | |
1322 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
1323 | uint8_t *ecc_calc = chip->buffers->ecccalc; | |
3f91e94f | 1324 | unsigned int max_bitflips = 0; |
6e0cb135 SN |
1325 | |
1326 | /* Read the OOB area first */ | |
1327 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); | |
1328 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); | |
1329 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); | |
1330 | ||
1331 | for (i = 0; i < chip->ecc.total; i++) | |
1332 | ecc_code[i] = chip->oob_poi[eccpos[i]]; | |
1333 | ||
1334 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { | |
1335 | int stat; | |
1336 | ||
1337 | chip->ecc.hwctl(mtd, NAND_ECC_READ); | |
1338 | chip->read_buf(mtd, p, eccsize); | |
1339 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
1340 | ||
1341 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); | |
3f91e94f | 1342 | if (stat < 0) { |
6e0cb135 | 1343 | mtd->ecc_stats.failed++; |
3f91e94f | 1344 | } else { |
6e0cb135 | 1345 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1346 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1347 | } | |
6e0cb135 | 1348 | } |
3f91e94f | 1349 | return max_bitflips; |
6e0cb135 SN |
1350 | } |
1351 | ||
f5bbdacc | 1352 | /** |
7854d3f7 | 1353 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
8b6e50c9 BN |
1354 | * @mtd: mtd info structure |
1355 | * @chip: nand chip info structure | |
1356 | * @buf: buffer to store read data | |
1fbb938d | 1357 | * @oob_required: caller requires OOB data read to chip->oob_poi |
8b6e50c9 | 1358 | * @page: page number to read |
f5bbdacc | 1359 | * |
8b6e50c9 BN |
1360 | * The hw generator calculates the error syndrome automatically. Therefore we |
1361 | * need a special oob layout and handling. | |
f5bbdacc TG |
1362 | */ |
1363 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
1fbb938d | 1364 | uint8_t *buf, int oob_required, int page) |
f5bbdacc TG |
1365 | { |
1366 | int i, eccsize = chip->ecc.size; | |
1367 | int eccbytes = chip->ecc.bytes; | |
1368 | int eccsteps = chip->ecc.steps; | |
1369 | uint8_t *p = buf; | |
f75e5097 | 1370 | uint8_t *oob = chip->oob_poi; |
3f91e94f | 1371 | unsigned int max_bitflips = 0; |
1da177e4 | 1372 | |
f5bbdacc TG |
1373 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1374 | int stat; | |
61b03bd7 | 1375 | |
f5bbdacc TG |
1376 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
1377 | chip->read_buf(mtd, p, eccsize); | |
1da177e4 | 1378 | |
f5bbdacc TG |
1379 | if (chip->ecc.prepad) { |
1380 | chip->read_buf(mtd, oob, chip->ecc.prepad); | |
1381 | oob += chip->ecc.prepad; | |
1382 | } | |
1da177e4 | 1383 | |
f5bbdacc TG |
1384 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
1385 | chip->read_buf(mtd, oob, eccbytes); | |
1386 | stat = chip->ecc.correct(mtd, p, oob, NULL); | |
61b03bd7 | 1387 | |
3f91e94f | 1388 | if (stat < 0) { |
f5bbdacc | 1389 | mtd->ecc_stats.failed++; |
3f91e94f | 1390 | } else { |
f5bbdacc | 1391 | mtd->ecc_stats.corrected += stat; |
3f91e94f MD |
1392 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
1393 | } | |
61b03bd7 | 1394 | |
f5bbdacc | 1395 | oob += eccbytes; |
1da177e4 | 1396 | |
f5bbdacc TG |
1397 | if (chip->ecc.postpad) { |
1398 | chip->read_buf(mtd, oob, chip->ecc.postpad); | |
1399 | oob += chip->ecc.postpad; | |
61b03bd7 | 1400 | } |
f5bbdacc | 1401 | } |
1da177e4 | 1402 | |
f5bbdacc | 1403 | /* Calculate remaining oob bytes */ |
7e4178f9 | 1404 | i = mtd->oobsize - (oob - chip->oob_poi); |
f5bbdacc TG |
1405 | if (i) |
1406 | chip->read_buf(mtd, oob, i); | |
61b03bd7 | 1407 | |
3f91e94f | 1408 | return max_bitflips; |
f5bbdacc | 1409 | } |
1da177e4 | 1410 | |
f5bbdacc | 1411 | /** |
7854d3f7 | 1412 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
8b6e50c9 BN |
1413 | * @chip: nand chip structure |
1414 | * @oob: oob destination address | |
1415 | * @ops: oob ops structure | |
1416 | * @len: size of oob to transfer | |
8593fbc6 TG |
1417 | */ |
1418 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, | |
7014568b | 1419 | struct mtd_oob_ops *ops, size_t len) |
8593fbc6 | 1420 | { |
f8ac0414 | 1421 | switch (ops->mode) { |
8593fbc6 | 1422 | |
0612b9dd BN |
1423 | case MTD_OPS_PLACE_OOB: |
1424 | case MTD_OPS_RAW: | |
8593fbc6 TG |
1425 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
1426 | return oob + len; | |
1427 | ||
0612b9dd | 1428 | case MTD_OPS_AUTO_OOB: { |
8593fbc6 | 1429 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
7bc3312b TG |
1430 | uint32_t boffs = 0, roffs = ops->ooboffs; |
1431 | size_t bytes = 0; | |
8593fbc6 | 1432 | |
f8ac0414 | 1433 | for (; free->length && len; free++, len -= bytes) { |
8b6e50c9 | 1434 | /* Read request not from offset 0? */ |
7bc3312b TG |
1435 | if (unlikely(roffs)) { |
1436 | if (roffs >= free->length) { | |
1437 | roffs -= free->length; | |
1438 | continue; | |
1439 | } | |
1440 | boffs = free->offset + roffs; | |
1441 | bytes = min_t(size_t, len, | |
1442 | (free->length - roffs)); | |
1443 | roffs = 0; | |
1444 | } else { | |
1445 | bytes = min_t(size_t, len, free->length); | |
1446 | boffs = free->offset; | |
1447 | } | |
1448 | memcpy(oob, chip->oob_poi + boffs, bytes); | |
8593fbc6 TG |
1449 | oob += bytes; |
1450 | } | |
1451 | return oob; | |
1452 | } | |
1453 | default: | |
1454 | BUG(); | |
1455 | } | |
1456 | return NULL; | |
1457 | } | |
1458 | ||
ba84fb59 BN |
1459 | /** |
1460 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode | |
1461 | * @mtd: MTD device structure | |
1462 | * @retry_mode: the retry mode to use | |
1463 | * | |
1464 | * Some vendors supply a special command to shift the Vt threshold, to be used | |
1465 | * when there are too many bitflips in a page (i.e., ECC error). After setting | |
1466 | * a new threshold, the host should retry reading the page. | |
1467 | */ | |
1468 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) | |
1469 | { | |
1470 | struct nand_chip *chip = mtd->priv; | |
1471 | ||
1472 | pr_debug("setting READ RETRY mode %d\n", retry_mode); | |
1473 | ||
1474 | if (retry_mode >= chip->read_retries) | |
1475 | return -EINVAL; | |
1476 | ||
1477 | if (!chip->setup_read_retry) | |
1478 | return -EOPNOTSUPP; | |
1479 | ||
1480 | return chip->setup_read_retry(mtd, retry_mode); | |
1481 | } | |
1482 | ||
8593fbc6 | 1483 | /** |
7854d3f7 | 1484 | * nand_do_read_ops - [INTERN] Read data with ECC |
8b6e50c9 BN |
1485 | * @mtd: MTD device structure |
1486 | * @from: offset to read from | |
1487 | * @ops: oob ops structure | |
f5bbdacc TG |
1488 | * |
1489 | * Internal function. Called with chip held. | |
1490 | */ | |
8593fbc6 TG |
1491 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
1492 | struct mtd_oob_ops *ops) | |
f5bbdacc | 1493 | { |
e47f3db4 | 1494 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
f5bbdacc | 1495 | struct nand_chip *chip = mtd->priv; |
f5bbdacc | 1496 | int ret = 0; |
8593fbc6 | 1497 | uint32_t readlen = ops->len; |
7014568b | 1498 | uint32_t oobreadlen = ops->ooblen; |
0612b9dd | 1499 | uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ? |
9aca334e ML |
1500 | mtd->oobavail : mtd->oobsize; |
1501 | ||
8593fbc6 | 1502 | uint8_t *bufpoi, *oob, *buf; |
edbc4540 | 1503 | unsigned int max_bitflips = 0; |
ba84fb59 | 1504 | int retry_mode = 0; |
b72f3dfb | 1505 | bool ecc_fail = false; |
1da177e4 | 1506 | |
f5bbdacc TG |
1507 | chipnr = (int)(from >> chip->chip_shift); |
1508 | chip->select_chip(mtd, chipnr); | |
61b03bd7 | 1509 | |
f5bbdacc TG |
1510 | realpage = (int)(from >> chip->page_shift); |
1511 | page = realpage & chip->pagemask; | |
1da177e4 | 1512 | |
f5bbdacc | 1513 | col = (int)(from & (mtd->writesize - 1)); |
61b03bd7 | 1514 | |
8593fbc6 TG |
1515 | buf = ops->datbuf; |
1516 | oob = ops->oobbuf; | |
e47f3db4 | 1517 | oob_required = oob ? 1 : 0; |
8593fbc6 | 1518 | |
f8ac0414 | 1519 | while (1) { |
b72f3dfb BN |
1520 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
1521 | ||
f5bbdacc TG |
1522 | bytes = min(mtd->writesize - col, readlen); |
1523 | aligned = (bytes == mtd->writesize); | |
61b03bd7 | 1524 | |
8b6e50c9 | 1525 | /* Is the current page in the buffer? */ |
8593fbc6 | 1526 | if (realpage != chip->pagebuf || oob) { |
4bf63fcb | 1527 | bufpoi = aligned ? buf : chip->buffers->databuf; |
61b03bd7 | 1528 | |
ba84fb59 | 1529 | read_retry: |
c00a0991 | 1530 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
1da177e4 | 1531 | |
edbc4540 MD |
1532 | /* |
1533 | * Now read the page into the buffer. Absent an error, | |
1534 | * the read methods return max bitflips per ecc step. | |
1535 | */ | |
0612b9dd | 1536 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
1fbb938d | 1537 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
e47f3db4 BN |
1538 | oob_required, |
1539 | page); | |
a5ff4f10 JW |
1540 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
1541 | !oob) | |
7351d3a5 FF |
1542 | ret = chip->ecc.read_subpage(mtd, chip, |
1543 | col, bytes, bufpoi); | |
956e944c | 1544 | else |
46a8cf2d | 1545 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
e47f3db4 | 1546 | oob_required, page); |
6d77b9d0 BN |
1547 | if (ret < 0) { |
1548 | if (!aligned) | |
1549 | /* Invalidate page cache */ | |
1550 | chip->pagebuf = -1; | |
1da177e4 | 1551 | break; |
6d77b9d0 | 1552 | } |
f5bbdacc | 1553 | |
edbc4540 MD |
1554 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
1555 | ||
f5bbdacc TG |
1556 | /* Transfer not aligned data */ |
1557 | if (!aligned) { | |
a5ff4f10 | 1558 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
b72f3dfb | 1559 | !(mtd->ecc_stats.failed - ecc_failures) && |
edbc4540 | 1560 | (ops->mode != MTD_OPS_RAW)) { |
3d459559 | 1561 | chip->pagebuf = realpage; |
edbc4540 MD |
1562 | chip->pagebuf_bitflips = ret; |
1563 | } else { | |
6d77b9d0 BN |
1564 | /* Invalidate page cache */ |
1565 | chip->pagebuf = -1; | |
edbc4540 | 1566 | } |
4bf63fcb | 1567 | memcpy(buf, chip->buffers->databuf + col, bytes); |
f5bbdacc TG |
1568 | } |
1569 | ||
8593fbc6 | 1570 | if (unlikely(oob)) { |
b64d39d8 ML |
1571 | int toread = min(oobreadlen, max_oobsize); |
1572 | ||
1573 | if (toread) { | |
1574 | oob = nand_transfer_oob(chip, | |
1575 | oob, ops, toread); | |
1576 | oobreadlen -= toread; | |
1577 | } | |
8593fbc6 | 1578 | } |
5bc7c33c BN |
1579 | |
1580 | if (chip->options & NAND_NEED_READRDY) { | |
1581 | /* Apply delay or wait for ready/busy pin */ | |
1582 | if (!chip->dev_ready) | |
1583 | udelay(chip->chip_delay); | |
1584 | else | |
1585 | nand_wait_ready(mtd); | |
1586 | } | |
b72f3dfb | 1587 | |
ba84fb59 | 1588 | if (mtd->ecc_stats.failed - ecc_failures) { |
28fa65e6 | 1589 | if (retry_mode + 1 < chip->read_retries) { |
ba84fb59 BN |
1590 | retry_mode++; |
1591 | ret = nand_setup_read_retry(mtd, | |
1592 | retry_mode); | |
1593 | if (ret < 0) | |
1594 | break; | |
1595 | ||
1596 | /* Reset failures; retry */ | |
1597 | mtd->ecc_stats.failed = ecc_failures; | |
1598 | goto read_retry; | |
1599 | } else { | |
1600 | /* No more retry modes; real failure */ | |
1601 | ecc_fail = true; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | buf += bytes; | |
8593fbc6 | 1606 | } else { |
4bf63fcb | 1607 | memcpy(buf, chip->buffers->databuf + col, bytes); |
8593fbc6 | 1608 | buf += bytes; |
edbc4540 MD |
1609 | max_bitflips = max_t(unsigned int, max_bitflips, |
1610 | chip->pagebuf_bitflips); | |
8593fbc6 | 1611 | } |
1da177e4 | 1612 | |
f5bbdacc | 1613 | readlen -= bytes; |
61b03bd7 | 1614 | |
ba84fb59 BN |
1615 | /* Reset to retry mode 0 */ |
1616 | if (retry_mode) { | |
1617 | ret = nand_setup_read_retry(mtd, 0); | |
1618 | if (ret < 0) | |
1619 | break; | |
1620 | retry_mode = 0; | |
1621 | } | |
1622 | ||
f5bbdacc | 1623 | if (!readlen) |
61b03bd7 | 1624 | break; |
1da177e4 | 1625 | |
8b6e50c9 | 1626 | /* For subsequent reads align to page boundary */ |
1da177e4 LT |
1627 | col = 0; |
1628 | /* Increment page address */ | |
1629 | realpage++; | |
1630 | ||
ace4dfee | 1631 | page = realpage & chip->pagemask; |
1da177e4 LT |
1632 | /* Check, if we cross a chip boundary */ |
1633 | if (!page) { | |
1634 | chipnr++; | |
ace4dfee TG |
1635 | chip->select_chip(mtd, -1); |
1636 | chip->select_chip(mtd, chipnr); | |
1da177e4 | 1637 | } |
1da177e4 | 1638 | } |
b0bb6903 | 1639 | chip->select_chip(mtd, -1); |
1da177e4 | 1640 | |
8593fbc6 | 1641 | ops->retlen = ops->len - (size_t) readlen; |
7014568b VW |
1642 | if (oob) |
1643 | ops->oobretlen = ops->ooblen - oobreadlen; | |
1da177e4 | 1644 | |
3f91e94f | 1645 | if (ret < 0) |
f5bbdacc TG |
1646 | return ret; |
1647 | ||
b72f3dfb | 1648 | if (ecc_fail) |
9a1fcdfd TG |
1649 | return -EBADMSG; |
1650 | ||
edbc4540 | 1651 | return max_bitflips; |
f5bbdacc TG |
1652 | } |
1653 | ||
1654 | /** | |
25985edc | 1655 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
8b6e50c9 BN |
1656 | * @mtd: MTD device structure |
1657 | * @from: offset to read from | |
1658 | * @len: number of bytes to read | |
1659 | * @retlen: pointer to variable to store the number of read bytes | |
1660 | * @buf: the databuffer to put data | |
f5bbdacc | 1661 | * |
8b6e50c9 | 1662 | * Get hold of the chip and call nand_do_read. |
f5bbdacc TG |
1663 | */ |
1664 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, | |
1665 | size_t *retlen, uint8_t *buf) | |
1666 | { | |
4a89ff88 | 1667 | struct mtd_oob_ops ops; |
f5bbdacc TG |
1668 | int ret; |
1669 | ||
6a8214aa | 1670 | nand_get_device(mtd, FL_READING); |
4a89ff88 BN |
1671 | ops.len = len; |
1672 | ops.datbuf = buf; | |
1673 | ops.oobbuf = NULL; | |
11041ae6 | 1674 | ops.mode = MTD_OPS_PLACE_OOB; |
4a89ff88 | 1675 | ret = nand_do_read_ops(mtd, from, &ops); |
4a89ff88 | 1676 | *retlen = ops.retlen; |
f5bbdacc | 1677 | nand_release_device(mtd); |
f5bbdacc | 1678 | return ret; |
1da177e4 LT |
1679 | } |
1680 | ||
7bc3312b | 1681 | /** |
7854d3f7 | 1682 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
8b6e50c9 BN |
1683 | * @mtd: mtd info structure |
1684 | * @chip: nand chip info structure | |
1685 | * @page: page number to read | |
7bc3312b TG |
1686 | */ |
1687 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
5c2ffb11 | 1688 | int page) |
7bc3312b | 1689 | { |
5c2ffb11 | 1690 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
7bc3312b | 1691 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
5c2ffb11 | 1692 | return 0; |
7bc3312b TG |
1693 | } |
1694 | ||
1695 | /** | |
7854d3f7 | 1696 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
7bc3312b | 1697 | * with syndromes |
8b6e50c9 BN |
1698 | * @mtd: mtd info structure |
1699 | * @chip: nand chip info structure | |
1700 | * @page: page number to read | |
7bc3312b TG |
1701 | */ |
1702 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, | |
5c2ffb11 | 1703 | int page) |
7bc3312b TG |
1704 | { |
1705 | uint8_t *buf = chip->oob_poi; | |
1706 | int length = mtd->oobsize; | |
1707 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1708 | int eccsize = chip->ecc.size; | |
1709 | uint8_t *bufpoi = buf; | |
1710 | int i, toread, sndrnd = 0, pos; | |
1711 | ||
1712 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); | |
1713 | for (i = 0; i < chip->ecc.steps; i++) { | |
1714 | if (sndrnd) { | |
1715 | pos = eccsize + i * (eccsize + chunk); | |
1716 | if (mtd->writesize > 512) | |
1717 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); | |
1718 | else | |
1719 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); | |
1720 | } else | |
1721 | sndrnd = 1; | |
1722 | toread = min_t(int, length, chunk); | |
1723 | chip->read_buf(mtd, bufpoi, toread); | |
1724 | bufpoi += toread; | |
1725 | length -= toread; | |
1726 | } | |
1727 | if (length > 0) | |
1728 | chip->read_buf(mtd, bufpoi, length); | |
1729 | ||
5c2ffb11 | 1730 | return 0; |
7bc3312b TG |
1731 | } |
1732 | ||
1733 | /** | |
7854d3f7 | 1734 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
8b6e50c9 BN |
1735 | * @mtd: mtd info structure |
1736 | * @chip: nand chip info structure | |
1737 | * @page: page number to write | |
7bc3312b TG |
1738 | */ |
1739 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, | |
1740 | int page) | |
1741 | { | |
1742 | int status = 0; | |
1743 | const uint8_t *buf = chip->oob_poi; | |
1744 | int length = mtd->oobsize; | |
1745 | ||
1746 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); | |
1747 | chip->write_buf(mtd, buf, length); | |
1748 | /* Send command to program the OOB data */ | |
1749 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1750 | ||
1751 | status = chip->waitfunc(mtd, chip); | |
1752 | ||
0d420f9d | 1753 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
7bc3312b TG |
1754 | } |
1755 | ||
1756 | /** | |
7854d3f7 | 1757 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
8b6e50c9 BN |
1758 | * with syndrome - only for large page flash |
1759 | * @mtd: mtd info structure | |
1760 | * @chip: nand chip info structure | |
1761 | * @page: page number to write | |
7bc3312b TG |
1762 | */ |
1763 | static int nand_write_oob_syndrome(struct mtd_info *mtd, | |
1764 | struct nand_chip *chip, int page) | |
1765 | { | |
1766 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; | |
1767 | int eccsize = chip->ecc.size, length = mtd->oobsize; | |
1768 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; | |
1769 | const uint8_t *bufpoi = chip->oob_poi; | |
1770 | ||
1771 | /* | |
1772 | * data-ecc-data-ecc ... ecc-oob | |
1773 | * or | |
1774 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob | |
1775 | */ | |
1776 | if (!chip->ecc.prepad && !chip->ecc.postpad) { | |
1777 | pos = steps * (eccsize + chunk); | |
1778 | steps = 0; | |
1779 | } else | |
8b0036ee | 1780 | pos = eccsize; |
7bc3312b TG |
1781 | |
1782 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); | |
1783 | for (i = 0; i < steps; i++) { | |
1784 | if (sndcmd) { | |
1785 | if (mtd->writesize <= 512) { | |
1786 | uint32_t fill = 0xFFFFFFFF; | |
1787 | ||
1788 | len = eccsize; | |
1789 | while (len > 0) { | |
1790 | int num = min_t(int, len, 4); | |
1791 | chip->write_buf(mtd, (uint8_t *)&fill, | |
1792 | num); | |
1793 | len -= num; | |
1794 | } | |
1795 | } else { | |
1796 | pos = eccsize + i * (eccsize + chunk); | |
1797 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); | |
1798 | } | |
1799 | } else | |
1800 | sndcmd = 1; | |
1801 | len = min_t(int, length, chunk); | |
1802 | chip->write_buf(mtd, bufpoi, len); | |
1803 | bufpoi += len; | |
1804 | length -= len; | |
1805 | } | |
1806 | if (length > 0) | |
1807 | chip->write_buf(mtd, bufpoi, length); | |
1808 | ||
1809 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1810 | status = chip->waitfunc(mtd, chip); | |
1811 | ||
1812 | return status & NAND_STATUS_FAIL ? -EIO : 0; | |
1813 | } | |
1814 | ||
1da177e4 | 1815 | /** |
7854d3f7 | 1816 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
8b6e50c9 BN |
1817 | * @mtd: MTD device structure |
1818 | * @from: offset to read from | |
1819 | * @ops: oob operations description structure | |
1da177e4 | 1820 | * |
8b6e50c9 | 1821 | * NAND read out-of-band data from the spare area. |
1da177e4 | 1822 | */ |
8593fbc6 TG |
1823 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
1824 | struct mtd_oob_ops *ops) | |
1da177e4 | 1825 | { |
c00a0991 | 1826 | int page, realpage, chipnr; |
ace4dfee | 1827 | struct nand_chip *chip = mtd->priv; |
041e4575 | 1828 | struct mtd_ecc_stats stats; |
7014568b VW |
1829 | int readlen = ops->ooblen; |
1830 | int len; | |
7bc3312b | 1831 | uint8_t *buf = ops->oobbuf; |
1951f2f7 | 1832 | int ret = 0; |
61b03bd7 | 1833 | |
289c0522 | 1834 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
20d8e248 | 1835 | __func__, (unsigned long long)from, readlen); |
1da177e4 | 1836 | |
041e4575 BN |
1837 | stats = mtd->ecc_stats; |
1838 | ||
0612b9dd | 1839 | if (ops->mode == MTD_OPS_AUTO_OOB) |
7014568b | 1840 | len = chip->ecc.layout->oobavail; |
03736155 AH |
1841 | else |
1842 | len = mtd->oobsize; | |
1843 | ||
1844 | if (unlikely(ops->ooboffs >= len)) { | |
289c0522 BN |
1845 | pr_debug("%s: attempt to start read outside oob\n", |
1846 | __func__); | |
03736155 AH |
1847 | return -EINVAL; |
1848 | } | |
1849 | ||
1850 | /* Do not allow reads past end of device */ | |
1851 | if (unlikely(from >= mtd->size || | |
1852 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - | |
1853 | (from >> chip->page_shift)) * len)) { | |
289c0522 BN |
1854 | pr_debug("%s: attempt to read beyond end of device\n", |
1855 | __func__); | |
03736155 AH |
1856 | return -EINVAL; |
1857 | } | |
7014568b | 1858 | |
7314e9e7 | 1859 | chipnr = (int)(from >> chip->chip_shift); |
ace4dfee | 1860 | chip->select_chip(mtd, chipnr); |
1da177e4 | 1861 | |
7314e9e7 TG |
1862 | /* Shift to get page */ |
1863 | realpage = (int)(from >> chip->page_shift); | |
1864 | page = realpage & chip->pagemask; | |
1da177e4 | 1865 | |
f8ac0414 | 1866 | while (1) { |
0612b9dd | 1867 | if (ops->mode == MTD_OPS_RAW) |
1951f2f7 | 1868 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
c46f6483 | 1869 | else |
1951f2f7 SL |
1870 | ret = chip->ecc.read_oob(mtd, chip, page); |
1871 | ||
1872 | if (ret < 0) | |
1873 | break; | |
7014568b VW |
1874 | |
1875 | len = min(len, readlen); | |
1876 | buf = nand_transfer_oob(chip, buf, ops, len); | |
8593fbc6 | 1877 | |
5bc7c33c BN |
1878 | if (chip->options & NAND_NEED_READRDY) { |
1879 | /* Apply delay or wait for ready/busy pin */ | |
1880 | if (!chip->dev_ready) | |
1881 | udelay(chip->chip_delay); | |
1882 | else | |
1883 | nand_wait_ready(mtd); | |
1884 | } | |
1885 | ||
7014568b | 1886 | readlen -= len; |
0d420f9d SZ |
1887 | if (!readlen) |
1888 | break; | |
1889 | ||
7314e9e7 TG |
1890 | /* Increment page address */ |
1891 | realpage++; | |
1892 | ||
1893 | page = realpage & chip->pagemask; | |
1894 | /* Check, if we cross a chip boundary */ | |
1895 | if (!page) { | |
1896 | chipnr++; | |
1897 | chip->select_chip(mtd, -1); | |
1898 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
1899 | } |
1900 | } | |
b0bb6903 | 1901 | chip->select_chip(mtd, -1); |
1da177e4 | 1902 | |
1951f2f7 SL |
1903 | ops->oobretlen = ops->ooblen - readlen; |
1904 | ||
1905 | if (ret < 0) | |
1906 | return ret; | |
041e4575 BN |
1907 | |
1908 | if (mtd->ecc_stats.failed - stats.failed) | |
1909 | return -EBADMSG; | |
1910 | ||
1911 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; | |
1da177e4 LT |
1912 | } |
1913 | ||
1914 | /** | |
8593fbc6 | 1915 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
8b6e50c9 BN |
1916 | * @mtd: MTD device structure |
1917 | * @from: offset to read from | |
1918 | * @ops: oob operation description structure | |
1da177e4 | 1919 | * |
8b6e50c9 | 1920 | * NAND read data and/or out-of-band data. |
1da177e4 | 1921 | */ |
8593fbc6 TG |
1922 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
1923 | struct mtd_oob_ops *ops) | |
1da177e4 | 1924 | { |
8593fbc6 TG |
1925 | int ret = -ENOTSUPP; |
1926 | ||
1927 | ops->retlen = 0; | |
1da177e4 LT |
1928 | |
1929 | /* Do not allow reads past end of device */ | |
7014568b | 1930 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
289c0522 BN |
1931 | pr_debug("%s: attempt to read beyond end of device\n", |
1932 | __func__); | |
1da177e4 LT |
1933 | return -EINVAL; |
1934 | } | |
1935 | ||
6a8214aa | 1936 | nand_get_device(mtd, FL_READING); |
1da177e4 | 1937 | |
f8ac0414 | 1938 | switch (ops->mode) { |
0612b9dd BN |
1939 | case MTD_OPS_PLACE_OOB: |
1940 | case MTD_OPS_AUTO_OOB: | |
1941 | case MTD_OPS_RAW: | |
8593fbc6 | 1942 | break; |
1da177e4 | 1943 | |
8593fbc6 TG |
1944 | default: |
1945 | goto out; | |
1946 | } | |
1da177e4 | 1947 | |
8593fbc6 TG |
1948 | if (!ops->datbuf) |
1949 | ret = nand_do_read_oob(mtd, from, ops); | |
1950 | else | |
1951 | ret = nand_do_read_ops(mtd, from, ops); | |
61b03bd7 | 1952 | |
7351d3a5 | 1953 | out: |
8593fbc6 TG |
1954 | nand_release_device(mtd); |
1955 | return ret; | |
1956 | } | |
61b03bd7 | 1957 | |
1da177e4 | 1958 | |
8593fbc6 | 1959 | /** |
7854d3f7 | 1960 | * nand_write_page_raw - [INTERN] raw page write function |
8b6e50c9 BN |
1961 | * @mtd: mtd info structure |
1962 | * @chip: nand chip info structure | |
1963 | * @buf: data buffer | |
1fbb938d | 1964 | * @oob_required: must write chip->oob_poi to OOB |
52ff49df | 1965 | * |
7854d3f7 | 1966 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
8593fbc6 | 1967 | */ |
fdbad98d | 1968 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 1969 | const uint8_t *buf, int oob_required) |
8593fbc6 TG |
1970 | { |
1971 | chip->write_buf(mtd, buf, mtd->writesize); | |
279f08d4 BN |
1972 | if (oob_required) |
1973 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
fdbad98d JW |
1974 | |
1975 | return 0; | |
1da177e4 LT |
1976 | } |
1977 | ||
52ff49df | 1978 | /** |
7854d3f7 | 1979 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
8b6e50c9 BN |
1980 | * @mtd: mtd info structure |
1981 | * @chip: nand chip info structure | |
1982 | * @buf: data buffer | |
1fbb938d | 1983 | * @oob_required: must write chip->oob_poi to OOB |
52ff49df DB |
1984 | * |
1985 | * We need a special oob layout and handling even when ECC isn't checked. | |
1986 | */ | |
fdbad98d | 1987 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
7351d3a5 | 1988 | struct nand_chip *chip, |
1fbb938d | 1989 | const uint8_t *buf, int oob_required) |
52ff49df DB |
1990 | { |
1991 | int eccsize = chip->ecc.size; | |
1992 | int eccbytes = chip->ecc.bytes; | |
1993 | uint8_t *oob = chip->oob_poi; | |
1994 | int steps, size; | |
1995 | ||
1996 | for (steps = chip->ecc.steps; steps > 0; steps--) { | |
1997 | chip->write_buf(mtd, buf, eccsize); | |
1998 | buf += eccsize; | |
1999 | ||
2000 | if (chip->ecc.prepad) { | |
2001 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
2002 | oob += chip->ecc.prepad; | |
2003 | } | |
2004 | ||
2005 | chip->read_buf(mtd, oob, eccbytes); | |
2006 | oob += eccbytes; | |
2007 | ||
2008 | if (chip->ecc.postpad) { | |
2009 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
2010 | oob += chip->ecc.postpad; | |
2011 | } | |
2012 | } | |
2013 | ||
2014 | size = mtd->oobsize - (oob - chip->oob_poi); | |
2015 | if (size) | |
2016 | chip->write_buf(mtd, oob, size); | |
fdbad98d JW |
2017 | |
2018 | return 0; | |
52ff49df | 2019 | } |
9223a456 | 2020 | /** |
7854d3f7 | 2021 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
8b6e50c9 BN |
2022 | * @mtd: mtd info structure |
2023 | * @chip: nand chip info structure | |
2024 | * @buf: data buffer | |
1fbb938d | 2025 | * @oob_required: must write chip->oob_poi to OOB |
9223a456 | 2026 | */ |
fdbad98d | 2027 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 2028 | const uint8_t *buf, int oob_required) |
9223a456 | 2029 | { |
f75e5097 TG |
2030 | int i, eccsize = chip->ecc.size; |
2031 | int eccbytes = chip->ecc.bytes; | |
2032 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 2033 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 2034 | const uint8_t *p = buf; |
8b099a39 | 2035 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 2036 | |
7854d3f7 | 2037 | /* Software ECC calculation */ |
8593fbc6 TG |
2038 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
2039 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); | |
9223a456 | 2040 | |
8593fbc6 TG |
2041 | for (i = 0; i < chip->ecc.total; i++) |
2042 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
9223a456 | 2043 | |
fdbad98d | 2044 | return chip->ecc.write_page_raw(mtd, chip, buf, 1); |
f75e5097 | 2045 | } |
9223a456 | 2046 | |
f75e5097 | 2047 | /** |
7854d3f7 | 2048 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
8b6e50c9 BN |
2049 | * @mtd: mtd info structure |
2050 | * @chip: nand chip info structure | |
2051 | * @buf: data buffer | |
1fbb938d | 2052 | * @oob_required: must write chip->oob_poi to OOB |
f75e5097 | 2053 | */ |
fdbad98d | 2054 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
1fbb938d | 2055 | const uint8_t *buf, int oob_required) |
f75e5097 TG |
2056 | { |
2057 | int i, eccsize = chip->ecc.size; | |
2058 | int eccbytes = chip->ecc.bytes; | |
2059 | int eccsteps = chip->ecc.steps; | |
4bf63fcb | 2060 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
f75e5097 | 2061 | const uint8_t *p = buf; |
8b099a39 | 2062 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
9223a456 | 2063 | |
f75e5097 TG |
2064 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
2065 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); | |
29da9cea | 2066 | chip->write_buf(mtd, p, eccsize); |
f75e5097 | 2067 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
9223a456 TG |
2068 | } |
2069 | ||
f75e5097 TG |
2070 | for (i = 0; i < chip->ecc.total; i++) |
2071 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
2072 | ||
2073 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
fdbad98d JW |
2074 | |
2075 | return 0; | |
9223a456 TG |
2076 | } |
2077 | ||
837a6ba4 GP |
2078 | |
2079 | /** | |
2080 | * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write | |
2081 | * @mtd: mtd info structure | |
2082 | * @chip: nand chip info structure | |
d6a95080 | 2083 | * @offset: column address of subpage within the page |
837a6ba4 | 2084 | * @data_len: data length |
d6a95080 | 2085 | * @buf: data buffer |
837a6ba4 GP |
2086 | * @oob_required: must write chip->oob_poi to OOB |
2087 | */ | |
2088 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, | |
2089 | struct nand_chip *chip, uint32_t offset, | |
d6a95080 | 2090 | uint32_t data_len, const uint8_t *buf, |
837a6ba4 GP |
2091 | int oob_required) |
2092 | { | |
2093 | uint8_t *oob_buf = chip->oob_poi; | |
2094 | uint8_t *ecc_calc = chip->buffers->ecccalc; | |
2095 | int ecc_size = chip->ecc.size; | |
2096 | int ecc_bytes = chip->ecc.bytes; | |
2097 | int ecc_steps = chip->ecc.steps; | |
2098 | uint32_t *eccpos = chip->ecc.layout->eccpos; | |
2099 | uint32_t start_step = offset / ecc_size; | |
2100 | uint32_t end_step = (offset + data_len - 1) / ecc_size; | |
2101 | int oob_bytes = mtd->oobsize / ecc_steps; | |
2102 | int step, i; | |
2103 | ||
2104 | for (step = 0; step < ecc_steps; step++) { | |
2105 | /* configure controller for WRITE access */ | |
2106 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); | |
2107 | ||
2108 | /* write data (untouched subpages already masked by 0xFF) */ | |
d6a95080 | 2109 | chip->write_buf(mtd, buf, ecc_size); |
837a6ba4 GP |
2110 | |
2111 | /* mask ECC of un-touched subpages by padding 0xFF */ | |
2112 | if ((step < start_step) || (step > end_step)) | |
2113 | memset(ecc_calc, 0xff, ecc_bytes); | |
2114 | else | |
d6a95080 | 2115 | chip->ecc.calculate(mtd, buf, ecc_calc); |
837a6ba4 GP |
2116 | |
2117 | /* mask OOB of un-touched subpages by padding 0xFF */ | |
2118 | /* if oob_required, preserve OOB metadata of written subpage */ | |
2119 | if (!oob_required || (step < start_step) || (step > end_step)) | |
2120 | memset(oob_buf, 0xff, oob_bytes); | |
2121 | ||
d6a95080 | 2122 | buf += ecc_size; |
837a6ba4 GP |
2123 | ecc_calc += ecc_bytes; |
2124 | oob_buf += oob_bytes; | |
2125 | } | |
2126 | ||
2127 | /* copy calculated ECC for whole page to chip->buffer->oob */ | |
2128 | /* this include masked-value(0xFF) for unwritten subpages */ | |
2129 | ecc_calc = chip->buffers->ecccalc; | |
2130 | for (i = 0; i < chip->ecc.total; i++) | |
2131 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; | |
2132 | ||
2133 | /* write OOB buffer to NAND device */ | |
2134 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); | |
2135 | ||
2136 | return 0; | |
2137 | } | |
2138 | ||
2139 | ||
61b03bd7 | 2140 | /** |
7854d3f7 | 2141 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
8b6e50c9 BN |
2142 | * @mtd: mtd info structure |
2143 | * @chip: nand chip info structure | |
2144 | * @buf: data buffer | |
1fbb938d | 2145 | * @oob_required: must write chip->oob_poi to OOB |
1da177e4 | 2146 | * |
8b6e50c9 BN |
2147 | * The hw generator calculates the error syndrome automatically. Therefore we |
2148 | * need a special oob layout and handling. | |
f75e5097 | 2149 | */ |
fdbad98d | 2150 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
1fbb938d BN |
2151 | struct nand_chip *chip, |
2152 | const uint8_t *buf, int oob_required) | |
1da177e4 | 2153 | { |
f75e5097 TG |
2154 | int i, eccsize = chip->ecc.size; |
2155 | int eccbytes = chip->ecc.bytes; | |
2156 | int eccsteps = chip->ecc.steps; | |
2157 | const uint8_t *p = buf; | |
2158 | uint8_t *oob = chip->oob_poi; | |
1da177e4 | 2159 | |
f75e5097 | 2160 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
1da177e4 | 2161 | |
f75e5097 TG |
2162 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
2163 | chip->write_buf(mtd, p, eccsize); | |
61b03bd7 | 2164 | |
f75e5097 TG |
2165 | if (chip->ecc.prepad) { |
2166 | chip->write_buf(mtd, oob, chip->ecc.prepad); | |
2167 | oob += chip->ecc.prepad; | |
2168 | } | |
2169 | ||
2170 | chip->ecc.calculate(mtd, p, oob); | |
2171 | chip->write_buf(mtd, oob, eccbytes); | |
2172 | oob += eccbytes; | |
2173 | ||
2174 | if (chip->ecc.postpad) { | |
2175 | chip->write_buf(mtd, oob, chip->ecc.postpad); | |
2176 | oob += chip->ecc.postpad; | |
1da177e4 | 2177 | } |
1da177e4 | 2178 | } |
f75e5097 TG |
2179 | |
2180 | /* Calculate remaining oob bytes */ | |
7e4178f9 | 2181 | i = mtd->oobsize - (oob - chip->oob_poi); |
f75e5097 TG |
2182 | if (i) |
2183 | chip->write_buf(mtd, oob, i); | |
fdbad98d JW |
2184 | |
2185 | return 0; | |
f75e5097 TG |
2186 | } |
2187 | ||
2188 | /** | |
956e944c | 2189 | * nand_write_page - [REPLACEABLE] write one page |
8b6e50c9 BN |
2190 | * @mtd: MTD device structure |
2191 | * @chip: NAND chip descriptor | |
837a6ba4 GP |
2192 | * @offset: address offset within the page |
2193 | * @data_len: length of actual data to be written | |
8b6e50c9 | 2194 | * @buf: the data to write |
1fbb938d | 2195 | * @oob_required: must write chip->oob_poi to OOB |
8b6e50c9 BN |
2196 | * @page: page number to write |
2197 | * @cached: cached programming | |
2198 | * @raw: use _raw version of write_page | |
f75e5097 TG |
2199 | */ |
2200 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, | |
837a6ba4 GP |
2201 | uint32_t offset, int data_len, const uint8_t *buf, |
2202 | int oob_required, int page, int cached, int raw) | |
f75e5097 | 2203 | { |
837a6ba4 GP |
2204 | int status, subpage; |
2205 | ||
2206 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && | |
2207 | chip->ecc.write_subpage) | |
2208 | subpage = offset || (data_len < mtd->writesize); | |
2209 | else | |
2210 | subpage = 0; | |
f75e5097 TG |
2211 | |
2212 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); | |
2213 | ||
956e944c | 2214 | if (unlikely(raw)) |
837a6ba4 GP |
2215 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
2216 | oob_required); | |
2217 | else if (subpage) | |
2218 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, | |
2219 | buf, oob_required); | |
956e944c | 2220 | else |
fdbad98d JW |
2221 | status = chip->ecc.write_page(mtd, chip, buf, oob_required); |
2222 | ||
2223 | if (status < 0) | |
2224 | return status; | |
f75e5097 TG |
2225 | |
2226 | /* | |
7854d3f7 | 2227 | * Cached progamming disabled for now. Not sure if it's worth the |
8b6e50c9 | 2228 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s). |
f75e5097 TG |
2229 | */ |
2230 | cached = 0; | |
2231 | ||
3239a6cd | 2232 | if (!cached || !NAND_HAS_CACHEPROG(chip)) { |
f75e5097 TG |
2233 | |
2234 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); | |
7bc3312b | 2235 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
2236 | /* |
2237 | * See if operation failed and additional status checks are | |
8b6e50c9 | 2238 | * available. |
f75e5097 TG |
2239 | */ |
2240 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
2241 | status = chip->errstat(mtd, chip, FL_WRITING, status, | |
2242 | page); | |
2243 | ||
2244 | if (status & NAND_STATUS_FAIL) | |
2245 | return -EIO; | |
2246 | } else { | |
2247 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); | |
7bc3312b | 2248 | status = chip->waitfunc(mtd, chip); |
f75e5097 TG |
2249 | } |
2250 | ||
f75e5097 | 2251 | return 0; |
1da177e4 LT |
2252 | } |
2253 | ||
8593fbc6 | 2254 | /** |
7854d3f7 | 2255 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
f722013e | 2256 | * @mtd: MTD device structure |
8b6e50c9 BN |
2257 | * @oob: oob data buffer |
2258 | * @len: oob data write length | |
2259 | * @ops: oob ops structure | |
8593fbc6 | 2260 | */ |
f722013e TAA |
2261 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
2262 | struct mtd_oob_ops *ops) | |
8593fbc6 | 2263 | { |
f722013e TAA |
2264 | struct nand_chip *chip = mtd->priv; |
2265 | ||
2266 | /* | |
2267 | * Initialise to all 0xFF, to avoid the possibility of left over OOB | |
2268 | * data from a previous OOB read. | |
2269 | */ | |
2270 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
2271 | ||
f8ac0414 | 2272 | switch (ops->mode) { |
8593fbc6 | 2273 | |
0612b9dd BN |
2274 | case MTD_OPS_PLACE_OOB: |
2275 | case MTD_OPS_RAW: | |
8593fbc6 TG |
2276 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
2277 | return oob + len; | |
2278 | ||
0612b9dd | 2279 | case MTD_OPS_AUTO_OOB: { |
8593fbc6 | 2280 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
7bc3312b TG |
2281 | uint32_t boffs = 0, woffs = ops->ooboffs; |
2282 | size_t bytes = 0; | |
8593fbc6 | 2283 | |
f8ac0414 | 2284 | for (; free->length && len; free++, len -= bytes) { |
8b6e50c9 | 2285 | /* Write request not from offset 0? */ |
7bc3312b TG |
2286 | if (unlikely(woffs)) { |
2287 | if (woffs >= free->length) { | |
2288 | woffs -= free->length; | |
2289 | continue; | |
2290 | } | |
2291 | boffs = free->offset + woffs; | |
2292 | bytes = min_t(size_t, len, | |
2293 | (free->length - woffs)); | |
2294 | woffs = 0; | |
2295 | } else { | |
2296 | bytes = min_t(size_t, len, free->length); | |
2297 | boffs = free->offset; | |
2298 | } | |
8b0036ee | 2299 | memcpy(chip->oob_poi + boffs, oob, bytes); |
8593fbc6 TG |
2300 | oob += bytes; |
2301 | } | |
2302 | return oob; | |
2303 | } | |
2304 | default: | |
2305 | BUG(); | |
2306 | } | |
2307 | return NULL; | |
2308 | } | |
2309 | ||
f8ac0414 | 2310 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
1da177e4 LT |
2311 | |
2312 | /** | |
7854d3f7 | 2313 | * nand_do_write_ops - [INTERN] NAND write with ECC |
8b6e50c9 BN |
2314 | * @mtd: MTD device structure |
2315 | * @to: offset to write to | |
2316 | * @ops: oob operations description structure | |
1da177e4 | 2317 | * |
8b6e50c9 | 2318 | * NAND write with ECC. |
1da177e4 | 2319 | */ |
8593fbc6 TG |
2320 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
2321 | struct mtd_oob_ops *ops) | |
1da177e4 | 2322 | { |
29072b96 | 2323 | int chipnr, realpage, page, blockmask, column; |
ace4dfee | 2324 | struct nand_chip *chip = mtd->priv; |
8593fbc6 | 2325 | uint32_t writelen = ops->len; |
782ce79a ML |
2326 | |
2327 | uint32_t oobwritelen = ops->ooblen; | |
0612b9dd | 2328 | uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ? |
782ce79a ML |
2329 | mtd->oobavail : mtd->oobsize; |
2330 | ||
8593fbc6 TG |
2331 | uint8_t *oob = ops->oobbuf; |
2332 | uint8_t *buf = ops->datbuf; | |
837a6ba4 | 2333 | int ret; |
e47f3db4 | 2334 | int oob_required = oob ? 1 : 0; |
1da177e4 | 2335 | |
8593fbc6 | 2336 | ops->retlen = 0; |
29072b96 TG |
2337 | if (!writelen) |
2338 | return 0; | |
1da177e4 | 2339 | |
8b6e50c9 | 2340 | /* Reject writes, which are not page aligned */ |
8593fbc6 | 2341 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
d0370219 BN |
2342 | pr_notice("%s: attempt to write non page aligned data\n", |
2343 | __func__); | |
1da177e4 LT |
2344 | return -EINVAL; |
2345 | } | |
2346 | ||
29072b96 | 2347 | column = to & (mtd->writesize - 1); |
1da177e4 | 2348 | |
6a930961 TG |
2349 | chipnr = (int)(to >> chip->chip_shift); |
2350 | chip->select_chip(mtd, chipnr); | |
2351 | ||
1da177e4 | 2352 | /* Check, if it is write protected */ |
b0bb6903 HS |
2353 | if (nand_check_wp(mtd)) { |
2354 | ret = -EIO; | |
2355 | goto err_out; | |
2356 | } | |
1da177e4 | 2357 | |
f75e5097 TG |
2358 | realpage = (int)(to >> chip->page_shift); |
2359 | page = realpage & chip->pagemask; | |
2360 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; | |
2361 | ||
2362 | /* Invalidate the page cache, when we write to the cached page */ | |
2363 | if (to <= (chip->pagebuf << chip->page_shift) && | |
8593fbc6 | 2364 | (chip->pagebuf << chip->page_shift) < (to + ops->len)) |
ace4dfee | 2365 | chip->pagebuf = -1; |
61b03bd7 | 2366 | |
782ce79a | 2367 | /* Don't allow multipage oob writes with offset */ |
b0bb6903 HS |
2368 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
2369 | ret = -EINVAL; | |
2370 | goto err_out; | |
2371 | } | |
782ce79a | 2372 | |
f8ac0414 | 2373 | while (1) { |
29072b96 | 2374 | int bytes = mtd->writesize; |
f75e5097 | 2375 | int cached = writelen > bytes && page != blockmask; |
29072b96 TG |
2376 | uint8_t *wbuf = buf; |
2377 | ||
8b6e50c9 | 2378 | /* Partial page write? */ |
29072b96 TG |
2379 | if (unlikely(column || writelen < (mtd->writesize - 1))) { |
2380 | cached = 0; | |
2381 | bytes = min_t(int, bytes - column, (int) writelen); | |
2382 | chip->pagebuf = -1; | |
2383 | memset(chip->buffers->databuf, 0xff, mtd->writesize); | |
2384 | memcpy(&chip->buffers->databuf[column], buf, bytes); | |
2385 | wbuf = chip->buffers->databuf; | |
2386 | } | |
1da177e4 | 2387 | |
782ce79a ML |
2388 | if (unlikely(oob)) { |
2389 | size_t len = min(oobwritelen, oobmaxlen); | |
f722013e | 2390 | oob = nand_fill_oob(mtd, oob, len, ops); |
782ce79a | 2391 | oobwritelen -= len; |
f722013e TAA |
2392 | } else { |
2393 | /* We still need to erase leftover OOB data */ | |
2394 | memset(chip->oob_poi, 0xff, mtd->oobsize); | |
782ce79a | 2395 | } |
837a6ba4 GP |
2396 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
2397 | oob_required, page, cached, | |
2398 | (ops->mode == MTD_OPS_RAW)); | |
f75e5097 TG |
2399 | if (ret) |
2400 | break; | |
2401 | ||
2402 | writelen -= bytes; | |
2403 | if (!writelen) | |
2404 | break; | |
2405 | ||
29072b96 | 2406 | column = 0; |
f75e5097 TG |
2407 | buf += bytes; |
2408 | realpage++; | |
2409 | ||
2410 | page = realpage & chip->pagemask; | |
2411 | /* Check, if we cross a chip boundary */ | |
2412 | if (!page) { | |
2413 | chipnr++; | |
2414 | chip->select_chip(mtd, -1); | |
2415 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
2416 | } |
2417 | } | |
8593fbc6 | 2418 | |
8593fbc6 | 2419 | ops->retlen = ops->len - writelen; |
7014568b VW |
2420 | if (unlikely(oob)) |
2421 | ops->oobretlen = ops->ooblen; | |
b0bb6903 HS |
2422 | |
2423 | err_out: | |
2424 | chip->select_chip(mtd, -1); | |
1da177e4 LT |
2425 | return ret; |
2426 | } | |
2427 | ||
2af7c653 SK |
2428 | /** |
2429 | * panic_nand_write - [MTD Interface] NAND write with ECC | |
8b6e50c9 BN |
2430 | * @mtd: MTD device structure |
2431 | * @to: offset to write to | |
2432 | * @len: number of bytes to write | |
2433 | * @retlen: pointer to variable to store the number of written bytes | |
2434 | * @buf: the data to write | |
2af7c653 SK |
2435 | * |
2436 | * NAND write with ECC. Used when performing writes in interrupt context, this | |
2437 | * may for example be called by mtdoops when writing an oops while in panic. | |
2438 | */ | |
2439 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, | |
2440 | size_t *retlen, const uint8_t *buf) | |
2441 | { | |
2442 | struct nand_chip *chip = mtd->priv; | |
4a89ff88 | 2443 | struct mtd_oob_ops ops; |
2af7c653 SK |
2444 | int ret; |
2445 | ||
8b6e50c9 | 2446 | /* Wait for the device to get ready */ |
2af7c653 SK |
2447 | panic_nand_wait(mtd, chip, 400); |
2448 | ||
8b6e50c9 | 2449 | /* Grab the device */ |
2af7c653 SK |
2450 | panic_nand_get_device(chip, mtd, FL_WRITING); |
2451 | ||
4a89ff88 BN |
2452 | ops.len = len; |
2453 | ops.datbuf = (uint8_t *)buf; | |
2454 | ops.oobbuf = NULL; | |
11041ae6 | 2455 | ops.mode = MTD_OPS_PLACE_OOB; |
2af7c653 | 2456 | |
4a89ff88 | 2457 | ret = nand_do_write_ops(mtd, to, &ops); |
2af7c653 | 2458 | |
4a89ff88 | 2459 | *retlen = ops.retlen; |
2af7c653 SK |
2460 | return ret; |
2461 | } | |
2462 | ||
f75e5097 | 2463 | /** |
8593fbc6 | 2464 | * nand_write - [MTD Interface] NAND write with ECC |
8b6e50c9 BN |
2465 | * @mtd: MTD device structure |
2466 | * @to: offset to write to | |
2467 | * @len: number of bytes to write | |
2468 | * @retlen: pointer to variable to store the number of written bytes | |
2469 | * @buf: the data to write | |
f75e5097 | 2470 | * |
8b6e50c9 | 2471 | * NAND write with ECC. |
f75e5097 | 2472 | */ |
8593fbc6 TG |
2473 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
2474 | size_t *retlen, const uint8_t *buf) | |
f75e5097 | 2475 | { |
4a89ff88 | 2476 | struct mtd_oob_ops ops; |
f75e5097 TG |
2477 | int ret; |
2478 | ||
6a8214aa | 2479 | nand_get_device(mtd, FL_WRITING); |
4a89ff88 BN |
2480 | ops.len = len; |
2481 | ops.datbuf = (uint8_t *)buf; | |
2482 | ops.oobbuf = NULL; | |
11041ae6 | 2483 | ops.mode = MTD_OPS_PLACE_OOB; |
4a89ff88 | 2484 | ret = nand_do_write_ops(mtd, to, &ops); |
4a89ff88 | 2485 | *retlen = ops.retlen; |
f75e5097 | 2486 | nand_release_device(mtd); |
8593fbc6 | 2487 | return ret; |
f75e5097 | 2488 | } |
7314e9e7 | 2489 | |
1da177e4 | 2490 | /** |
8593fbc6 | 2491 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
8b6e50c9 BN |
2492 | * @mtd: MTD device structure |
2493 | * @to: offset to write to | |
2494 | * @ops: oob operation description structure | |
1da177e4 | 2495 | * |
8b6e50c9 | 2496 | * NAND write out-of-band. |
1da177e4 | 2497 | */ |
8593fbc6 TG |
2498 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
2499 | struct mtd_oob_ops *ops) | |
1da177e4 | 2500 | { |
03736155 | 2501 | int chipnr, page, status, len; |
ace4dfee | 2502 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2503 | |
289c0522 | 2504 | pr_debug("%s: to = 0x%08x, len = %i\n", |
20d8e248 | 2505 | __func__, (unsigned int)to, (int)ops->ooblen); |
1da177e4 | 2506 | |
0612b9dd | 2507 | if (ops->mode == MTD_OPS_AUTO_OOB) |
03736155 AH |
2508 | len = chip->ecc.layout->oobavail; |
2509 | else | |
2510 | len = mtd->oobsize; | |
2511 | ||
1da177e4 | 2512 | /* Do not allow write past end of page */ |
03736155 | 2513 | if ((ops->ooboffs + ops->ooblen) > len) { |
289c0522 BN |
2514 | pr_debug("%s: attempt to write past end of page\n", |
2515 | __func__); | |
1da177e4 LT |
2516 | return -EINVAL; |
2517 | } | |
2518 | ||
03736155 | 2519 | if (unlikely(ops->ooboffs >= len)) { |
289c0522 BN |
2520 | pr_debug("%s: attempt to start write outside oob\n", |
2521 | __func__); | |
03736155 AH |
2522 | return -EINVAL; |
2523 | } | |
2524 | ||
775adc3d | 2525 | /* Do not allow write past end of device */ |
03736155 AH |
2526 | if (unlikely(to >= mtd->size || |
2527 | ops->ooboffs + ops->ooblen > | |
2528 | ((mtd->size >> chip->page_shift) - | |
2529 | (to >> chip->page_shift)) * len)) { | |
289c0522 BN |
2530 | pr_debug("%s: attempt to write beyond end of device\n", |
2531 | __func__); | |
03736155 AH |
2532 | return -EINVAL; |
2533 | } | |
2534 | ||
7314e9e7 | 2535 | chipnr = (int)(to >> chip->chip_shift); |
ace4dfee | 2536 | chip->select_chip(mtd, chipnr); |
1da177e4 | 2537 | |
7314e9e7 TG |
2538 | /* Shift to get page */ |
2539 | page = (int)(to >> chip->page_shift); | |
2540 | ||
2541 | /* | |
2542 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one | |
2543 | * of my DiskOnChip 2000 test units) will clear the whole data page too | |
2544 | * if we don't do this. I have no clue why, but I seem to have 'fixed' | |
2545 | * it in the doc2000 driver in August 1999. dwmw2. | |
2546 | */ | |
ace4dfee | 2547 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
1da177e4 LT |
2548 | |
2549 | /* Check, if it is write protected */ | |
b0bb6903 HS |
2550 | if (nand_check_wp(mtd)) { |
2551 | chip->select_chip(mtd, -1); | |
8593fbc6 | 2552 | return -EROFS; |
b0bb6903 | 2553 | } |
61b03bd7 | 2554 | |
1da177e4 | 2555 | /* Invalidate the page cache, if we write to the cached page */ |
ace4dfee TG |
2556 | if (page == chip->pagebuf) |
2557 | chip->pagebuf = -1; | |
1da177e4 | 2558 | |
f722013e | 2559 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
9ce244b3 | 2560 | |
0612b9dd | 2561 | if (ops->mode == MTD_OPS_RAW) |
9ce244b3 BN |
2562 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
2563 | else | |
2564 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); | |
1da177e4 | 2565 | |
b0bb6903 HS |
2566 | chip->select_chip(mtd, -1); |
2567 | ||
7bc3312b TG |
2568 | if (status) |
2569 | return status; | |
1da177e4 | 2570 | |
7014568b | 2571 | ops->oobretlen = ops->ooblen; |
1da177e4 | 2572 | |
7bc3312b | 2573 | return 0; |
8593fbc6 TG |
2574 | } |
2575 | ||
2576 | /** | |
2577 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band | |
8b6e50c9 BN |
2578 | * @mtd: MTD device structure |
2579 | * @to: offset to write to | |
2580 | * @ops: oob operation description structure | |
8593fbc6 TG |
2581 | */ |
2582 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, | |
2583 | struct mtd_oob_ops *ops) | |
2584 | { | |
8593fbc6 TG |
2585 | int ret = -ENOTSUPP; |
2586 | ||
2587 | ops->retlen = 0; | |
2588 | ||
2589 | /* Do not allow writes past end of device */ | |
7014568b | 2590 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
289c0522 BN |
2591 | pr_debug("%s: attempt to write beyond end of device\n", |
2592 | __func__); | |
8593fbc6 TG |
2593 | return -EINVAL; |
2594 | } | |
2595 | ||
6a8214aa | 2596 | nand_get_device(mtd, FL_WRITING); |
8593fbc6 | 2597 | |
f8ac0414 | 2598 | switch (ops->mode) { |
0612b9dd BN |
2599 | case MTD_OPS_PLACE_OOB: |
2600 | case MTD_OPS_AUTO_OOB: | |
2601 | case MTD_OPS_RAW: | |
8593fbc6 TG |
2602 | break; |
2603 | ||
2604 | default: | |
2605 | goto out; | |
2606 | } | |
2607 | ||
2608 | if (!ops->datbuf) | |
2609 | ret = nand_do_write_oob(mtd, to, ops); | |
2610 | else | |
2611 | ret = nand_do_write_ops(mtd, to, ops); | |
2612 | ||
7351d3a5 | 2613 | out: |
1da177e4 | 2614 | nand_release_device(mtd); |
1da177e4 LT |
2615 | return ret; |
2616 | } | |
2617 | ||
1da177e4 | 2618 | /** |
7854d3f7 | 2619 | * single_erase_cmd - [GENERIC] NAND standard block erase command function |
8b6e50c9 BN |
2620 | * @mtd: MTD device structure |
2621 | * @page: the page address of the block which will be erased | |
1da177e4 | 2622 | * |
8b6e50c9 | 2623 | * Standard erase command for NAND chips. |
1da177e4 | 2624 | */ |
e0c7d767 | 2625 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
1da177e4 | 2626 | { |
ace4dfee | 2627 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 2628 | /* Send commands to erase a block */ |
ace4dfee TG |
2629 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
2630 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); | |
1da177e4 LT |
2631 | } |
2632 | ||
1da177e4 LT |
2633 | /** |
2634 | * nand_erase - [MTD Interface] erase block(s) | |
8b6e50c9 BN |
2635 | * @mtd: MTD device structure |
2636 | * @instr: erase instruction | |
1da177e4 | 2637 | * |
8b6e50c9 | 2638 | * Erase one ore more blocks. |
1da177e4 | 2639 | */ |
e0c7d767 | 2640 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
1da177e4 | 2641 | { |
e0c7d767 | 2642 | return nand_erase_nand(mtd, instr, 0); |
1da177e4 | 2643 | } |
61b03bd7 | 2644 | |
1da177e4 | 2645 | /** |
7854d3f7 | 2646 | * nand_erase_nand - [INTERN] erase block(s) |
8b6e50c9 BN |
2647 | * @mtd: MTD device structure |
2648 | * @instr: erase instruction | |
2649 | * @allowbbt: allow erasing the bbt area | |
1da177e4 | 2650 | * |
8b6e50c9 | 2651 | * Erase one ore more blocks. |
1da177e4 | 2652 | */ |
ace4dfee TG |
2653 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
2654 | int allowbbt) | |
1da177e4 | 2655 | { |
69423d99 | 2656 | int page, status, pages_per_block, ret, chipnr; |
ace4dfee | 2657 | struct nand_chip *chip = mtd->priv; |
69423d99 | 2658 | loff_t len; |
1da177e4 | 2659 | |
289c0522 BN |
2660 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
2661 | __func__, (unsigned long long)instr->addr, | |
2662 | (unsigned long long)instr->len); | |
1da177e4 | 2663 | |
6fe5a6ac | 2664 | if (check_offs_len(mtd, instr->addr, instr->len)) |
1da177e4 | 2665 | return -EINVAL; |
1da177e4 | 2666 | |
1da177e4 | 2667 | /* Grab the lock and see if the device is available */ |
6a8214aa | 2668 | nand_get_device(mtd, FL_ERASING); |
1da177e4 LT |
2669 | |
2670 | /* Shift to get first page */ | |
ace4dfee TG |
2671 | page = (int)(instr->addr >> chip->page_shift); |
2672 | chipnr = (int)(instr->addr >> chip->chip_shift); | |
1da177e4 LT |
2673 | |
2674 | /* Calculate pages in each block */ | |
ace4dfee | 2675 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
1da177e4 LT |
2676 | |
2677 | /* Select the NAND device */ | |
ace4dfee | 2678 | chip->select_chip(mtd, chipnr); |
1da177e4 | 2679 | |
1da177e4 LT |
2680 | /* Check, if it is write protected */ |
2681 | if (nand_check_wp(mtd)) { | |
289c0522 BN |
2682 | pr_debug("%s: device is write protected!\n", |
2683 | __func__); | |
1da177e4 LT |
2684 | instr->state = MTD_ERASE_FAILED; |
2685 | goto erase_exit; | |
2686 | } | |
2687 | ||
2688 | /* Loop through the pages */ | |
2689 | len = instr->len; | |
2690 | ||
2691 | instr->state = MTD_ERASING; | |
2692 | ||
2693 | while (len) { | |
12183a20 | 2694 | /* Check if we have a bad block, we do not erase bad blocks! */ |
ace4dfee TG |
2695 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
2696 | chip->page_shift, 0, allowbbt)) { | |
d0370219 BN |
2697 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
2698 | __func__, page); | |
1da177e4 LT |
2699 | instr->state = MTD_ERASE_FAILED; |
2700 | goto erase_exit; | |
2701 | } | |
61b03bd7 | 2702 | |
ace4dfee TG |
2703 | /* |
2704 | * Invalidate the page cache, if we erase the block which | |
8b6e50c9 | 2705 | * contains the current cached page. |
ace4dfee TG |
2706 | */ |
2707 | if (page <= chip->pagebuf && chip->pagebuf < | |
2708 | (page + pages_per_block)) | |
2709 | chip->pagebuf = -1; | |
1da177e4 | 2710 | |
ace4dfee | 2711 | chip->erase_cmd(mtd, page & chip->pagemask); |
61b03bd7 | 2712 | |
7bc3312b | 2713 | status = chip->waitfunc(mtd, chip); |
1da177e4 | 2714 | |
ace4dfee TG |
2715 | /* |
2716 | * See if operation failed and additional status checks are | |
2717 | * available | |
2718 | */ | |
2719 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) | |
2720 | status = chip->errstat(mtd, chip, FL_ERASING, | |
2721 | status, page); | |
068e3c0a | 2722 | |
1da177e4 | 2723 | /* See if block erase succeeded */ |
a4ab4c5d | 2724 | if (status & NAND_STATUS_FAIL) { |
289c0522 BN |
2725 | pr_debug("%s: failed erase, page 0x%08x\n", |
2726 | __func__, page); | |
1da177e4 | 2727 | instr->state = MTD_ERASE_FAILED; |
69423d99 AH |
2728 | instr->fail_addr = |
2729 | ((loff_t)page << chip->page_shift); | |
1da177e4 LT |
2730 | goto erase_exit; |
2731 | } | |
30f464b7 | 2732 | |
1da177e4 | 2733 | /* Increment page address and decrement length */ |
daae74ca | 2734 | len -= (1ULL << chip->phys_erase_shift); |
1da177e4 LT |
2735 | page += pages_per_block; |
2736 | ||
2737 | /* Check, if we cross a chip boundary */ | |
ace4dfee | 2738 | if (len && !(page & chip->pagemask)) { |
1da177e4 | 2739 | chipnr++; |
ace4dfee TG |
2740 | chip->select_chip(mtd, -1); |
2741 | chip->select_chip(mtd, chipnr); | |
1da177e4 LT |
2742 | } |
2743 | } | |
2744 | instr->state = MTD_ERASE_DONE; | |
2745 | ||
7351d3a5 | 2746 | erase_exit: |
1da177e4 LT |
2747 | |
2748 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; | |
1da177e4 LT |
2749 | |
2750 | /* Deselect and wake up anyone waiting on the device */ | |
b0bb6903 | 2751 | chip->select_chip(mtd, -1); |
1da177e4 LT |
2752 | nand_release_device(mtd); |
2753 | ||
49defc01 DW |
2754 | /* Do call back function */ |
2755 | if (!ret) | |
2756 | mtd_erase_callback(instr); | |
2757 | ||
1da177e4 LT |
2758 | /* Return more or less happy */ |
2759 | return ret; | |
2760 | } | |
2761 | ||
2762 | /** | |
2763 | * nand_sync - [MTD Interface] sync | |
8b6e50c9 | 2764 | * @mtd: MTD device structure |
1da177e4 | 2765 | * |
8b6e50c9 | 2766 | * Sync is actually a wait for chip ready function. |
1da177e4 | 2767 | */ |
e0c7d767 | 2768 | static void nand_sync(struct mtd_info *mtd) |
1da177e4 | 2769 | { |
289c0522 | 2770 | pr_debug("%s: called\n", __func__); |
1da177e4 LT |
2771 | |
2772 | /* Grab the lock and see if the device is available */ | |
6a8214aa | 2773 | nand_get_device(mtd, FL_SYNCING); |
1da177e4 | 2774 | /* Release it and go back */ |
e0c7d767 | 2775 | nand_release_device(mtd); |
1da177e4 LT |
2776 | } |
2777 | ||
1da177e4 | 2778 | /** |
ace4dfee | 2779 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
8b6e50c9 BN |
2780 | * @mtd: MTD device structure |
2781 | * @offs: offset relative to mtd start | |
1da177e4 | 2782 | */ |
ace4dfee | 2783 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
1da177e4 | 2784 | { |
ace4dfee | 2785 | return nand_block_checkbad(mtd, offs, 1, 0); |
1da177e4 LT |
2786 | } |
2787 | ||
2788 | /** | |
ace4dfee | 2789 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
8b6e50c9 BN |
2790 | * @mtd: MTD device structure |
2791 | * @ofs: offset relative to mtd start | |
1da177e4 | 2792 | */ |
e0c7d767 | 2793 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 2794 | { |
1da177e4 LT |
2795 | int ret; |
2796 | ||
f8ac0414 FF |
2797 | ret = nand_block_isbad(mtd, ofs); |
2798 | if (ret) { | |
8b6e50c9 | 2799 | /* If it was bad already, return success and do nothing */ |
1da177e4 LT |
2800 | if (ret > 0) |
2801 | return 0; | |
e0c7d767 DW |
2802 | return ret; |
2803 | } | |
1da177e4 | 2804 | |
5a0edb25 | 2805 | return nand_block_markbad_lowlevel(mtd, ofs); |
1da177e4 LT |
2806 | } |
2807 | ||
7db03ecc HS |
2808 | /** |
2809 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand | |
2810 | * @mtd: MTD device structure | |
2811 | * @chip: nand chip info structure | |
2812 | * @addr: feature address. | |
2813 | * @subfeature_param: the subfeature parameters, a four bytes array. | |
2814 | */ | |
2815 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, | |
2816 | int addr, uint8_t *subfeature_param) | |
2817 | { | |
2818 | int status; | |
05f78359 | 2819 | int i; |
7db03ecc | 2820 | |
d914c932 DM |
2821 | if (!chip->onfi_version || |
2822 | !(le16_to_cpu(chip->onfi_params.opt_cmd) | |
2823 | & ONFI_OPT_CMD_SET_GET_FEATURES)) | |
7db03ecc HS |
2824 | return -EINVAL; |
2825 | ||
2826 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); | |
05f78359 UKK |
2827 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
2828 | chip->write_byte(mtd, subfeature_param[i]); | |
2829 | ||
7db03ecc HS |
2830 | status = chip->waitfunc(mtd, chip); |
2831 | if (status & NAND_STATUS_FAIL) | |
2832 | return -EIO; | |
2833 | return 0; | |
2834 | } | |
2835 | ||
2836 | /** | |
2837 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand | |
2838 | * @mtd: MTD device structure | |
2839 | * @chip: nand chip info structure | |
2840 | * @addr: feature address. | |
2841 | * @subfeature_param: the subfeature parameters, a four bytes array. | |
2842 | */ | |
2843 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, | |
2844 | int addr, uint8_t *subfeature_param) | |
2845 | { | |
05f78359 UKK |
2846 | int i; |
2847 | ||
d914c932 DM |
2848 | if (!chip->onfi_version || |
2849 | !(le16_to_cpu(chip->onfi_params.opt_cmd) | |
2850 | & ONFI_OPT_CMD_SET_GET_FEATURES)) | |
7db03ecc HS |
2851 | return -EINVAL; |
2852 | ||
2853 | /* clear the sub feature parameters */ | |
2854 | memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN); | |
2855 | ||
2856 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); | |
05f78359 UKK |
2857 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
2858 | *subfeature_param++ = chip->read_byte(mtd); | |
7db03ecc HS |
2859 | return 0; |
2860 | } | |
2861 | ||
962034f4 VW |
2862 | /** |
2863 | * nand_suspend - [MTD Interface] Suspend the NAND flash | |
8b6e50c9 | 2864 | * @mtd: MTD device structure |
962034f4 VW |
2865 | */ |
2866 | static int nand_suspend(struct mtd_info *mtd) | |
2867 | { | |
6a8214aa | 2868 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
962034f4 VW |
2869 | } |
2870 | ||
2871 | /** | |
2872 | * nand_resume - [MTD Interface] Resume the NAND flash | |
8b6e50c9 | 2873 | * @mtd: MTD device structure |
962034f4 VW |
2874 | */ |
2875 | static void nand_resume(struct mtd_info *mtd) | |
2876 | { | |
ace4dfee | 2877 | struct nand_chip *chip = mtd->priv; |
962034f4 | 2878 | |
ace4dfee | 2879 | if (chip->state == FL_PM_SUSPENDED) |
962034f4 VW |
2880 | nand_release_device(mtd); |
2881 | else | |
d0370219 BN |
2882 | pr_err("%s called for a chip which is not in suspended state\n", |
2883 | __func__); | |
962034f4 VW |
2884 | } |
2885 | ||
8b6e50c9 | 2886 | /* Set default functions */ |
ace4dfee | 2887 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
7aa65bfd | 2888 | { |
1da177e4 | 2889 | /* check for proper chip_delay setup, set 20us if not */ |
ace4dfee TG |
2890 | if (!chip->chip_delay) |
2891 | chip->chip_delay = 20; | |
1da177e4 LT |
2892 | |
2893 | /* check, if a user supplied command function given */ | |
ace4dfee TG |
2894 | if (chip->cmdfunc == NULL) |
2895 | chip->cmdfunc = nand_command; | |
1da177e4 LT |
2896 | |
2897 | /* check, if a user supplied wait function given */ | |
ace4dfee TG |
2898 | if (chip->waitfunc == NULL) |
2899 | chip->waitfunc = nand_wait; | |
2900 | ||
2901 | if (!chip->select_chip) | |
2902 | chip->select_chip = nand_select_chip; | |
68e80780 | 2903 | |
4204cccd HS |
2904 | /* set for ONFI nand */ |
2905 | if (!chip->onfi_set_features) | |
2906 | chip->onfi_set_features = nand_onfi_set_features; | |
2907 | if (!chip->onfi_get_features) | |
2908 | chip->onfi_get_features = nand_onfi_get_features; | |
2909 | ||
68e80780 BN |
2910 | /* If called twice, pointers that depend on busw may need to be reset */ |
2911 | if (!chip->read_byte || chip->read_byte == nand_read_byte) | |
ace4dfee TG |
2912 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
2913 | if (!chip->read_word) | |
2914 | chip->read_word = nand_read_word; | |
2915 | if (!chip->block_bad) | |
2916 | chip->block_bad = nand_block_bad; | |
2917 | if (!chip->block_markbad) | |
2918 | chip->block_markbad = nand_default_block_markbad; | |
68e80780 | 2919 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
ace4dfee | 2920 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
05f78359 UKK |
2921 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
2922 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; | |
68e80780 | 2923 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
ace4dfee | 2924 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
ace4dfee TG |
2925 | if (!chip->scan_bbt) |
2926 | chip->scan_bbt = nand_default_bbt; | |
f75e5097 TG |
2927 | |
2928 | if (!chip->controller) { | |
2929 | chip->controller = &chip->hwcontrol; | |
2930 | spin_lock_init(&chip->controller->lock); | |
2931 | init_waitqueue_head(&chip->controller->wq); | |
2932 | } | |
2933 | ||
7aa65bfd TG |
2934 | } |
2935 | ||
8b6e50c9 | 2936 | /* Sanitize ONFI strings so we can safely print them */ |
d1e1f4e4 FF |
2937 | static void sanitize_string(uint8_t *s, size_t len) |
2938 | { | |
2939 | ssize_t i; | |
2940 | ||
8b6e50c9 | 2941 | /* Null terminate */ |
d1e1f4e4 FF |
2942 | s[len - 1] = 0; |
2943 | ||
8b6e50c9 | 2944 | /* Remove non printable chars */ |
d1e1f4e4 FF |
2945 | for (i = 0; i < len - 1; i++) { |
2946 | if (s[i] < ' ' || s[i] > 127) | |
2947 | s[i] = '?'; | |
2948 | } | |
2949 | ||
8b6e50c9 | 2950 | /* Remove trailing spaces */ |
d1e1f4e4 FF |
2951 | strim(s); |
2952 | } | |
2953 | ||
2954 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) | |
2955 | { | |
2956 | int i; | |
2957 | while (len--) { | |
2958 | crc ^= *p++ << 8; | |
2959 | for (i = 0; i < 8; i++) | |
2960 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); | |
2961 | } | |
2962 | ||
2963 | return crc; | |
2964 | } | |
2965 | ||
6dcbe0cd HS |
2966 | /* Parse the Extended Parameter Page. */ |
2967 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, | |
2968 | struct nand_chip *chip, struct nand_onfi_params *p) | |
2969 | { | |
2970 | struct onfi_ext_param_page *ep; | |
2971 | struct onfi_ext_section *s; | |
2972 | struct onfi_ext_ecc_info *ecc; | |
2973 | uint8_t *cursor; | |
2974 | int ret = -EINVAL; | |
2975 | int len; | |
2976 | int i; | |
2977 | ||
2978 | len = le16_to_cpu(p->ext_param_page_length) * 16; | |
2979 | ep = kmalloc(len, GFP_KERNEL); | |
5cb13271 BN |
2980 | if (!ep) |
2981 | return -ENOMEM; | |
6dcbe0cd HS |
2982 | |
2983 | /* Send our own NAND_CMD_PARAM. */ | |
2984 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); | |
2985 | ||
2986 | /* Use the Change Read Column command to skip the ONFI param pages. */ | |
2987 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, | |
2988 | sizeof(*p) * p->num_of_param_pages , -1); | |
2989 | ||
2990 | /* Read out the Extended Parameter Page. */ | |
2991 | chip->read_buf(mtd, (uint8_t *)ep, len); | |
2992 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) | |
2993 | != le16_to_cpu(ep->crc))) { | |
2994 | pr_debug("fail in the CRC.\n"); | |
2995 | goto ext_out; | |
2996 | } | |
2997 | ||
2998 | /* | |
2999 | * Check the signature. | |
3000 | * Do not strictly follow the ONFI spec, maybe changed in future. | |
3001 | */ | |
3002 | if (strncmp(ep->sig, "EPPS", 4)) { | |
3003 | pr_debug("The signature is invalid.\n"); | |
3004 | goto ext_out; | |
3005 | } | |
3006 | ||
3007 | /* find the ECC section. */ | |
3008 | cursor = (uint8_t *)(ep + 1); | |
3009 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { | |
3010 | s = ep->sections + i; | |
3011 | if (s->type == ONFI_SECTION_TYPE_2) | |
3012 | break; | |
3013 | cursor += s->length * 16; | |
3014 | } | |
3015 | if (i == ONFI_EXT_SECTION_MAX) { | |
3016 | pr_debug("We can not find the ECC section.\n"); | |
3017 | goto ext_out; | |
3018 | } | |
3019 | ||
3020 | /* get the info we want. */ | |
3021 | ecc = (struct onfi_ext_ecc_info *)cursor; | |
3022 | ||
4ae7d228 BN |
3023 | if (!ecc->codeword_size) { |
3024 | pr_debug("Invalid codeword size\n"); | |
3025 | goto ext_out; | |
6dcbe0cd HS |
3026 | } |
3027 | ||
4ae7d228 BN |
3028 | chip->ecc_strength_ds = ecc->ecc_bits; |
3029 | chip->ecc_step_ds = 1 << ecc->codeword_size; | |
5cb13271 | 3030 | ret = 0; |
6dcbe0cd HS |
3031 | |
3032 | ext_out: | |
3033 | kfree(ep); | |
3034 | return ret; | |
3035 | } | |
3036 | ||
8429bb39 BN |
3037 | static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) |
3038 | { | |
3039 | struct nand_chip *chip = mtd->priv; | |
3040 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; | |
3041 | ||
3042 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, | |
3043 | feature); | |
3044 | } | |
3045 | ||
3046 | /* | |
3047 | * Configure chip properties from Micron vendor-specific ONFI table | |
3048 | */ | |
3049 | static void nand_onfi_detect_micron(struct nand_chip *chip, | |
3050 | struct nand_onfi_params *p) | |
3051 | { | |
3052 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; | |
3053 | ||
3054 | if (le16_to_cpu(p->vendor_revision) < 1) | |
3055 | return; | |
3056 | ||
3057 | chip->read_retries = micron->read_retry_options; | |
3058 | chip->setup_read_retry = nand_setup_read_retry_micron; | |
3059 | } | |
3060 | ||
6fb277ba | 3061 | /* |
8b6e50c9 | 3062 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
6fb277ba FF |
3063 | */ |
3064 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, | |
08c248fb | 3065 | int *busw) |
6fb277ba FF |
3066 | { |
3067 | struct nand_onfi_params *p = &chip->onfi_params; | |
3068 | int i; | |
3069 | int val; | |
3070 | ||
7854d3f7 | 3071 | /* Try ONFI for unknown chip or LP */ |
6fb277ba FF |
3072 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
3073 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || | |
3074 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') | |
3075 | return 0; | |
3076 | ||
c7f23a70 BN |
3077 | /* |
3078 | * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not | |
3079 | * with NAND_BUSWIDTH_16 | |
3080 | */ | |
3081 | if (chip->options & NAND_BUSWIDTH_16) { | |
3082 | pr_err("ONFI cannot be probed in 16-bit mode; aborting\n"); | |
3083 | return 0; | |
3084 | } | |
3085 | ||
6fb277ba FF |
3086 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
3087 | for (i = 0; i < 3; i++) { | |
3088 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); | |
3089 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == | |
3090 | le16_to_cpu(p->crc)) { | |
6fb277ba FF |
3091 | break; |
3092 | } | |
3093 | } | |
3094 | ||
c7f23a70 BN |
3095 | if (i == 3) { |
3096 | pr_err("Could not find valid ONFI parameter page; aborting\n"); | |
6fb277ba | 3097 | return 0; |
c7f23a70 | 3098 | } |
6fb277ba | 3099 | |
8b6e50c9 | 3100 | /* Check version */ |
6fb277ba | 3101 | val = le16_to_cpu(p->revision); |
b7b1a29d BN |
3102 | if (val & (1 << 5)) |
3103 | chip->onfi_version = 23; | |
3104 | else if (val & (1 << 4)) | |
6fb277ba FF |
3105 | chip->onfi_version = 22; |
3106 | else if (val & (1 << 3)) | |
3107 | chip->onfi_version = 21; | |
3108 | else if (val & (1 << 2)) | |
3109 | chip->onfi_version = 20; | |
b7b1a29d | 3110 | else if (val & (1 << 1)) |
6fb277ba | 3111 | chip->onfi_version = 10; |
b7b1a29d BN |
3112 | |
3113 | if (!chip->onfi_version) { | |
20171642 | 3114 | pr_info("unsupported ONFI version: %d\n", val); |
b7b1a29d BN |
3115 | return 0; |
3116 | } | |
6fb277ba FF |
3117 | |
3118 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); | |
3119 | sanitize_string(p->model, sizeof(p->model)); | |
3120 | if (!mtd->name) | |
3121 | mtd->name = p->model; | |
4355b70c | 3122 | |
6fb277ba | 3123 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
4355b70c BN |
3124 | |
3125 | /* | |
3126 | * pages_per_block and blocks_per_lun may not be a power-of-2 size | |
3127 | * (don't ask me who thought of this...). MTD assumes that these | |
3128 | * dimensions will be power-of-2, so just truncate the remaining area. | |
3129 | */ | |
3130 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); | |
3131 | mtd->erasesize *= mtd->writesize; | |
3132 | ||
6fb277ba | 3133 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
4355b70c BN |
3134 | |
3135 | /* See erasesize comment */ | |
3136 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); | |
63795755 | 3137 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
13fbd179 | 3138 | chip->bits_per_cell = p->bits_per_cell; |
e2985fc1 HS |
3139 | |
3140 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) | |
08c248fb | 3141 | *busw = NAND_BUSWIDTH_16; |
e2985fc1 HS |
3142 | else |
3143 | *busw = 0; | |
6fb277ba | 3144 | |
10c86bab HS |
3145 | if (p->ecc_bits != 0xff) { |
3146 | chip->ecc_strength_ds = p->ecc_bits; | |
3147 | chip->ecc_step_ds = 512; | |
6dcbe0cd HS |
3148 | } else if (chip->onfi_version >= 21 && |
3149 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { | |
3150 | ||
3151 | /* | |
3152 | * The nand_flash_detect_ext_param_page() uses the | |
3153 | * Change Read Column command which maybe not supported | |
3154 | * by the chip->cmdfunc. So try to update the chip->cmdfunc | |
3155 | * now. We do not replace user supplied command function. | |
3156 | */ | |
3157 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) | |
3158 | chip->cmdfunc = nand_command_lp; | |
3159 | ||
3160 | /* The Extended Parameter Page is supported since ONFI 2.1. */ | |
3161 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) | |
c7f23a70 BN |
3162 | pr_warn("Failed to detect ONFI extended param page\n"); |
3163 | } else { | |
3164 | pr_warn("Could not retrieve ONFI ECC requirements\n"); | |
10c86bab HS |
3165 | } |
3166 | ||
8429bb39 BN |
3167 | if (p->jedec_id == NAND_MFR_MICRON) |
3168 | nand_onfi_detect_micron(chip, p); | |
3169 | ||
6fb277ba FF |
3170 | return 1; |
3171 | } | |
3172 | ||
e3b88bd6 BN |
3173 | /* |
3174 | * nand_id_has_period - Check if an ID string has a given wraparound period | |
3175 | * @id_data: the ID string | |
3176 | * @arrlen: the length of the @id_data array | |
3177 | * @period: the period of repitition | |
3178 | * | |
3179 | * Check if an ID string is repeated within a given sequence of bytes at | |
3180 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a | |
d4d4f1bf | 3181 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
e3b88bd6 BN |
3182 | * if the repetition has a period of @period; otherwise, returns zero. |
3183 | */ | |
3184 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) | |
3185 | { | |
3186 | int i, j; | |
3187 | for (i = 0; i < period; i++) | |
3188 | for (j = i + period; j < arrlen; j += period) | |
3189 | if (id_data[i] != id_data[j]) | |
3190 | return 0; | |
3191 | return 1; | |
3192 | } | |
3193 | ||
3194 | /* | |
3195 | * nand_id_len - Get the length of an ID string returned by CMD_READID | |
3196 | * @id_data: the ID string | |
3197 | * @arrlen: the length of the @id_data array | |
3198 | ||
3199 | * Returns the length of the ID string, according to known wraparound/trailing | |
3200 | * zero patterns. If no pattern exists, returns the length of the array. | |
3201 | */ | |
3202 | static int nand_id_len(u8 *id_data, int arrlen) | |
3203 | { | |
3204 | int last_nonzero, period; | |
3205 | ||
3206 | /* Find last non-zero byte */ | |
3207 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) | |
3208 | if (id_data[last_nonzero]) | |
3209 | break; | |
3210 | ||
3211 | /* All zeros */ | |
3212 | if (last_nonzero < 0) | |
3213 | return 0; | |
3214 | ||
3215 | /* Calculate wraparound period */ | |
3216 | for (period = 1; period < arrlen; period++) | |
3217 | if (nand_id_has_period(id_data, arrlen, period)) | |
3218 | break; | |
3219 | ||
3220 | /* There's a repeated pattern */ | |
3221 | if (period < arrlen) | |
3222 | return period; | |
3223 | ||
3224 | /* There are trailing zeros */ | |
3225 | if (last_nonzero < arrlen - 1) | |
3226 | return last_nonzero + 1; | |
3227 | ||
3228 | /* No pattern detected */ | |
3229 | return arrlen; | |
3230 | } | |
3231 | ||
7db906b7 HS |
3232 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
3233 | static int nand_get_bits_per_cell(u8 cellinfo) | |
3234 | { | |
3235 | int bits; | |
3236 | ||
3237 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; | |
3238 | bits >>= NAND_CI_CELLTYPE_SHIFT; | |
3239 | return bits + 1; | |
3240 | } | |
3241 | ||
fc09bbc0 BN |
3242 | /* |
3243 | * Many new NAND share similar device ID codes, which represent the size of the | |
3244 | * chip. The rest of the parameters must be decoded according to generic or | |
3245 | * manufacturer-specific "extended ID" decoding patterns. | |
3246 | */ | |
3247 | static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip, | |
3248 | u8 id_data[8], int *busw) | |
3249 | { | |
e3b88bd6 | 3250 | int extid, id_len; |
fc09bbc0 | 3251 | /* The 3rd id byte holds MLC / multichip data */ |
7db906b7 | 3252 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
fc09bbc0 BN |
3253 | /* The 4th id byte is the important one */ |
3254 | extid = id_data[3]; | |
3255 | ||
e3b88bd6 BN |
3256 | id_len = nand_id_len(id_data, 8); |
3257 | ||
fc09bbc0 BN |
3258 | /* |
3259 | * Field definitions are in the following datasheets: | |
3260 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) | |
af451af4 | 3261 | * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) |
73ca392f | 3262 | * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) |
fc09bbc0 | 3263 | * |
af451af4 BN |
3264 | * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung |
3265 | * ID to decide what to do. | |
fc09bbc0 | 3266 | */ |
af451af4 | 3267 | if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && |
1d0ed69d | 3268 | !nand_is_slc(chip) && id_data[5] != 0x00) { |
fc09bbc0 BN |
3269 | /* Calc pagesize */ |
3270 | mtd->writesize = 2048 << (extid & 0x03); | |
3271 | extid >>= 2; | |
3272 | /* Calc oobsize */ | |
e2d3a35e | 3273 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
fc09bbc0 BN |
3274 | case 1: |
3275 | mtd->oobsize = 128; | |
3276 | break; | |
3277 | case 2: | |
3278 | mtd->oobsize = 218; | |
3279 | break; | |
3280 | case 3: | |
3281 | mtd->oobsize = 400; | |
3282 | break; | |
e2d3a35e | 3283 | case 4: |
fc09bbc0 BN |
3284 | mtd->oobsize = 436; |
3285 | break; | |
e2d3a35e BN |
3286 | case 5: |
3287 | mtd->oobsize = 512; | |
3288 | break; | |
3289 | case 6: | |
e2d3a35e BN |
3290 | mtd->oobsize = 640; |
3291 | break; | |
94d04e82 HS |
3292 | case 7: |
3293 | default: /* Other cases are "reserved" (unknown) */ | |
3294 | mtd->oobsize = 1024; | |
3295 | break; | |
fc09bbc0 BN |
3296 | } |
3297 | extid >>= 2; | |
3298 | /* Calc blocksize */ | |
3299 | mtd->erasesize = (128 * 1024) << | |
3300 | (((extid >> 1) & 0x04) | (extid & 0x03)); | |
3301 | *busw = 0; | |
73ca392f | 3302 | } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && |
1d0ed69d | 3303 | !nand_is_slc(chip)) { |
73ca392f BN |
3304 | unsigned int tmp; |
3305 | ||
3306 | /* Calc pagesize */ | |
3307 | mtd->writesize = 2048 << (extid & 0x03); | |
3308 | extid >>= 2; | |
3309 | /* Calc oobsize */ | |
3310 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { | |
3311 | case 0: | |
3312 | mtd->oobsize = 128; | |
3313 | break; | |
3314 | case 1: | |
3315 | mtd->oobsize = 224; | |
3316 | break; | |
3317 | case 2: | |
3318 | mtd->oobsize = 448; | |
3319 | break; | |
3320 | case 3: | |
3321 | mtd->oobsize = 64; | |
3322 | break; | |
3323 | case 4: | |
3324 | mtd->oobsize = 32; | |
3325 | break; | |
3326 | case 5: | |
3327 | mtd->oobsize = 16; | |
3328 | break; | |
3329 | default: | |
3330 | mtd->oobsize = 640; | |
3331 | break; | |
3332 | } | |
3333 | extid >>= 2; | |
3334 | /* Calc blocksize */ | |
3335 | tmp = ((extid >> 1) & 0x04) | (extid & 0x03); | |
3336 | if (tmp < 0x03) | |
3337 | mtd->erasesize = (128 * 1024) << tmp; | |
3338 | else if (tmp == 0x03) | |
3339 | mtd->erasesize = 768 * 1024; | |
3340 | else | |
3341 | mtd->erasesize = (64 * 1024) << tmp; | |
3342 | *busw = 0; | |
fc09bbc0 BN |
3343 | } else { |
3344 | /* Calc pagesize */ | |
3345 | mtd->writesize = 1024 << (extid & 0x03); | |
3346 | extid >>= 2; | |
3347 | /* Calc oobsize */ | |
3348 | mtd->oobsize = (8 << (extid & 0x01)) * | |
3349 | (mtd->writesize >> 9); | |
3350 | extid >>= 2; | |
3351 | /* Calc blocksize. Blocksize is multiples of 64KiB */ | |
3352 | mtd->erasesize = (64 * 1024) << (extid & 0x03); | |
3353 | extid >>= 2; | |
3354 | /* Get buswidth information */ | |
3355 | *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; | |
60c67382 BN |
3356 | |
3357 | /* | |
3358 | * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per | |
3359 | * 512B page. For Toshiba SLC, we decode the 5th/6th byte as | |
3360 | * follows: | |
3361 | * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, | |
3362 | * 110b -> 24nm | |
3363 | * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC | |
3364 | */ | |
3365 | if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA && | |
1d0ed69d | 3366 | nand_is_slc(chip) && |
60c67382 BN |
3367 | (id_data[5] & 0x7) == 0x6 /* 24nm */ && |
3368 | !(id_data[4] & 0x80) /* !BENAND */) { | |
3369 | mtd->oobsize = 32 * mtd->writesize >> 9; | |
3370 | } | |
3371 | ||
fc09bbc0 BN |
3372 | } |
3373 | } | |
3374 | ||
f23a481c BN |
3375 | /* |
3376 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id | |
3377 | * decodes a matching ID table entry and assigns the MTD size parameters for | |
3378 | * the chip. | |
3379 | */ | |
3380 | static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip, | |
3381 | struct nand_flash_dev *type, u8 id_data[8], | |
3382 | int *busw) | |
3383 | { | |
3384 | int maf_id = id_data[0]; | |
3385 | ||
3386 | mtd->erasesize = type->erasesize; | |
3387 | mtd->writesize = type->pagesize; | |
3388 | mtd->oobsize = mtd->writesize / 32; | |
3389 | *busw = type->options & NAND_BUSWIDTH_16; | |
3390 | ||
1c195e90 HS |
3391 | /* All legacy ID NAND are small-page, SLC */ |
3392 | chip->bits_per_cell = 1; | |
3393 | ||
f23a481c BN |
3394 | /* |
3395 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since | |
3396 | * some Spansion chips have erasesize that conflicts with size | |
3397 | * listed in nand_ids table. | |
3398 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) | |
3399 | */ | |
3400 | if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 | |
3401 | && id_data[6] == 0x00 && id_data[7] == 0x00 | |
3402 | && mtd->writesize == 512) { | |
3403 | mtd->erasesize = 128 * 1024; | |
3404 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); | |
3405 | } | |
3406 | } | |
3407 | ||
7e74c2d7 BN |
3408 | /* |
3409 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some | |
3410 | * heuristic patterns using various detected parameters (e.g., manufacturer, | |
3411 | * page size, cell-type information). | |
3412 | */ | |
3413 | static void nand_decode_bbm_options(struct mtd_info *mtd, | |
3414 | struct nand_chip *chip, u8 id_data[8]) | |
3415 | { | |
3416 | int maf_id = id_data[0]; | |
3417 | ||
3418 | /* Set the bad block position */ | |
3419 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) | |
3420 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; | |
3421 | else | |
3422 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; | |
3423 | ||
3424 | /* | |
3425 | * Bad block marker is stored in the last page of each block on Samsung | |
3426 | * and Hynix MLC devices; stored in first two pages of each block on | |
3427 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, | |
3428 | * AMD/Spansion, and Macronix. All others scan only the first page. | |
3429 | */ | |
1d0ed69d | 3430 | if (!nand_is_slc(chip) && |
7e74c2d7 BN |
3431 | (maf_id == NAND_MFR_SAMSUNG || |
3432 | maf_id == NAND_MFR_HYNIX)) | |
3433 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; | |
1d0ed69d | 3434 | else if ((nand_is_slc(chip) && |
7e74c2d7 BN |
3435 | (maf_id == NAND_MFR_SAMSUNG || |
3436 | maf_id == NAND_MFR_HYNIX || | |
3437 | maf_id == NAND_MFR_TOSHIBA || | |
3438 | maf_id == NAND_MFR_AMD || | |
3439 | maf_id == NAND_MFR_MACRONIX)) || | |
3440 | (mtd->writesize == 2048 && | |
3441 | maf_id == NAND_MFR_MICRON)) | |
3442 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; | |
3443 | } | |
3444 | ||
ec6e87e3 HS |
3445 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
3446 | { | |
3447 | return type->id_len; | |
3448 | } | |
3449 | ||
3450 | static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, | |
3451 | struct nand_flash_dev *type, u8 *id_data, int *busw) | |
3452 | { | |
3453 | if (!strncmp(type->id, id_data, type->id_len)) { | |
3454 | mtd->writesize = type->pagesize; | |
3455 | mtd->erasesize = type->erasesize; | |
3456 | mtd->oobsize = type->oobsize; | |
3457 | ||
7db906b7 | 3458 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
ec6e87e3 HS |
3459 | chip->chipsize = (uint64_t)type->chipsize << 20; |
3460 | chip->options |= type->options; | |
57219342 HS |
3461 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
3462 | chip->ecc_step_ds = NAND_ECC_STEP(type); | |
ec6e87e3 HS |
3463 | |
3464 | *busw = type->options & NAND_BUSWIDTH_16; | |
3465 | ||
092b6a1d CZ |
3466 | if (!mtd->name) |
3467 | mtd->name = type->name; | |
3468 | ||
ec6e87e3 HS |
3469 | return true; |
3470 | } | |
3471 | return false; | |
3472 | } | |
3473 | ||
7aa65bfd | 3474 | /* |
8b6e50c9 | 3475 | * Get the flash and manufacturer id and lookup if the type is supported. |
7aa65bfd TG |
3476 | */ |
3477 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, | |
ace4dfee | 3478 | struct nand_chip *chip, |
7351d3a5 FF |
3479 | int busw, |
3480 | int *maf_id, int *dev_id, | |
5e81e88a | 3481 | struct nand_flash_dev *type) |
7aa65bfd | 3482 | { |
d1e1f4e4 | 3483 | int i, maf_idx; |
426c457a | 3484 | u8 id_data[8]; |
1da177e4 LT |
3485 | |
3486 | /* Select the device */ | |
ace4dfee | 3487 | chip->select_chip(mtd, 0); |
1da177e4 | 3488 | |
ef89a880 KB |
3489 | /* |
3490 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) | |
8b6e50c9 | 3491 | * after power-up. |
ef89a880 KB |
3492 | */ |
3493 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
3494 | ||
1da177e4 | 3495 | /* Send the command for reading device ID */ |
ace4dfee | 3496 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 LT |
3497 | |
3498 | /* Read manufacturer and device IDs */ | |
ace4dfee | 3499 | *maf_id = chip->read_byte(mtd); |
d1e1f4e4 | 3500 | *dev_id = chip->read_byte(mtd); |
1da177e4 | 3501 | |
8b6e50c9 BN |
3502 | /* |
3503 | * Try again to make sure, as some systems the bus-hold or other | |
ed8165c7 BD |
3504 | * interface concerns can cause random data which looks like a |
3505 | * possibly credible NAND flash to appear. If the two results do | |
3506 | * not match, ignore the device completely. | |
3507 | */ | |
3508 | ||
3509 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); | |
3510 | ||
4aef9b78 BN |
3511 | /* Read entire ID string */ |
3512 | for (i = 0; i < 8; i++) | |
426c457a | 3513 | id_data[i] = chip->read_byte(mtd); |
ed8165c7 | 3514 | |
d1e1f4e4 | 3515 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
20171642 | 3516 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
d0370219 | 3517 | *maf_id, *dev_id, id_data[0], id_data[1]); |
ed8165c7 BD |
3518 | return ERR_PTR(-ENODEV); |
3519 | } | |
3520 | ||
7aa65bfd | 3521 | if (!type) |
5e81e88a DW |
3522 | type = nand_flash_ids; |
3523 | ||
ec6e87e3 HS |
3524 | for (; type->name != NULL; type++) { |
3525 | if (is_full_id_nand(type)) { | |
3526 | if (find_full_id_nand(mtd, chip, type, id_data, &busw)) | |
3527 | goto ident_done; | |
3528 | } else if (*dev_id == type->dev_id) { | |
3529 | break; | |
3530 | } | |
3531 | } | |
5e81e88a | 3532 | |
d1e1f4e4 FF |
3533 | chip->onfi_version = 0; |
3534 | if (!type->name || !type->pagesize) { | |
6fb277ba | 3535 | /* Check is chip is ONFI compliant */ |
47450b35 | 3536 | if (nand_flash_detect_onfi(mtd, chip, &busw)) |
6fb277ba | 3537 | goto ident_done; |
d1e1f4e4 FF |
3538 | } |
3539 | ||
5e81e88a | 3540 | if (!type->name) |
7aa65bfd TG |
3541 | return ERR_PTR(-ENODEV); |
3542 | ||
ba0251fe TG |
3543 | if (!mtd->name) |
3544 | mtd->name = type->name; | |
3545 | ||
69423d99 | 3546 | chip->chipsize = (uint64_t)type->chipsize << 20; |
7aa65bfd | 3547 | |
12a40a57 | 3548 | if (!type->pagesize && chip->init_size) { |
8b6e50c9 | 3549 | /* Set the pagesize, oobsize, erasesize by the driver */ |
12a40a57 HS |
3550 | busw = chip->init_size(mtd, chip, id_data); |
3551 | } else if (!type->pagesize) { | |
fc09bbc0 BN |
3552 | /* Decode parameters from extended ID */ |
3553 | nand_decode_ext_id(mtd, chip, id_data, &busw); | |
7aa65bfd | 3554 | } else { |
f23a481c | 3555 | nand_decode_id(mtd, chip, type, id_data, &busw); |
7aa65bfd | 3556 | } |
bf7a01bf BN |
3557 | /* Get chip options */ |
3558 | chip->options |= type->options; | |
d1e1f4e4 | 3559 | |
8b6e50c9 BN |
3560 | /* |
3561 | * Check if chip is not a Samsung device. Do not clear the | |
3562 | * options for chips which do not have an extended id. | |
d1e1f4e4 FF |
3563 | */ |
3564 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) | |
3565 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; | |
3566 | ident_done: | |
3567 | ||
7aa65bfd | 3568 | /* Try to identify manufacturer */ |
9a909867 | 3569 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
7aa65bfd TG |
3570 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
3571 | break; | |
3572 | } | |
0ea4a755 | 3573 | |
64b37b2a MC |
3574 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
3575 | WARN_ON(chip->options & NAND_BUSWIDTH_16); | |
3576 | chip->options |= busw; | |
3577 | nand_set_defaults(chip, busw); | |
3578 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { | |
3579 | /* | |
3580 | * Check, if buswidth is correct. Hardware drivers should set | |
3581 | * chip correct! | |
3582 | */ | |
20171642 EG |
3583 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
3584 | *maf_id, *dev_id); | |
3585 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name); | |
3586 | pr_warn("bus width %d instead %d bit\n", | |
d0370219 BN |
3587 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
3588 | busw ? 16 : 8); | |
7aa65bfd TG |
3589 | return ERR_PTR(-EINVAL); |
3590 | } | |
61b03bd7 | 3591 | |
7e74c2d7 BN |
3592 | nand_decode_bbm_options(mtd, chip, id_data); |
3593 | ||
7aa65bfd | 3594 | /* Calculate the address shift from the page size */ |
ace4dfee | 3595 | chip->page_shift = ffs(mtd->writesize) - 1; |
8b6e50c9 | 3596 | /* Convert chipsize to number of pages per chip -1 */ |
ace4dfee | 3597 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
61b03bd7 | 3598 | |
ace4dfee | 3599 | chip->bbt_erase_shift = chip->phys_erase_shift = |
7aa65bfd | 3600 | ffs(mtd->erasesize) - 1; |
69423d99 AH |
3601 | if (chip->chipsize & 0xffffffff) |
3602 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; | |
7351d3a5 FF |
3603 | else { |
3604 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); | |
3605 | chip->chip_shift += 32 - 1; | |
3606 | } | |
1da177e4 | 3607 | |
26d9be11 | 3608 | chip->badblockbits = 8; |
14c65786 | 3609 | chip->erase_cmd = single_erase_cmd; |
7aa65bfd | 3610 | |
8b6e50c9 | 3611 | /* Do not replace user supplied command function! */ |
ace4dfee TG |
3612 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
3613 | chip->cmdfunc = nand_command_lp; | |
7aa65bfd | 3614 | |
20171642 EG |
3615 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
3616 | *maf_id, *dev_id); | |
3617 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, | |
3723e93c | 3618 | chip->onfi_version ? chip->onfi_params.model : type->name); |
20171642 | 3619 | pr_info("%dMiB, %s, page size: %d, OOB size: %d\n", |
3723e93c HS |
3620 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
3621 | mtd->writesize, mtd->oobsize); | |
7aa65bfd TG |
3622 | return type; |
3623 | } | |
3624 | ||
7aa65bfd | 3625 | /** |
3b85c321 | 3626 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
8b6e50c9 BN |
3627 | * @mtd: MTD device structure |
3628 | * @maxchips: number of chips to scan for | |
3629 | * @table: alternative NAND ID table | |
7aa65bfd | 3630 | * |
8b6e50c9 BN |
3631 | * This is the first phase of the normal nand_scan() function. It reads the |
3632 | * flash ID and sets up MTD fields accordingly. | |
7aa65bfd | 3633 | * |
3b85c321 | 3634 | * The mtd->owner field must be set to the module of the caller. |
7aa65bfd | 3635 | */ |
5e81e88a DW |
3636 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
3637 | struct nand_flash_dev *table) | |
7aa65bfd | 3638 | { |
d1e1f4e4 | 3639 | int i, busw, nand_maf_id, nand_dev_id; |
ace4dfee | 3640 | struct nand_chip *chip = mtd->priv; |
7aa65bfd TG |
3641 | struct nand_flash_dev *type; |
3642 | ||
7aa65bfd | 3643 | /* Get buswidth to select the correct functions */ |
ace4dfee | 3644 | busw = chip->options & NAND_BUSWIDTH_16; |
7aa65bfd | 3645 | /* Set the default functions */ |
ace4dfee | 3646 | nand_set_defaults(chip, busw); |
7aa65bfd TG |
3647 | |
3648 | /* Read the flash type */ | |
7351d3a5 FF |
3649 | type = nand_get_flash_type(mtd, chip, busw, |
3650 | &nand_maf_id, &nand_dev_id, table); | |
7aa65bfd TG |
3651 | |
3652 | if (IS_ERR(type)) { | |
b1c6e6db | 3653 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
d0370219 | 3654 | pr_warn("No NAND device found\n"); |
ace4dfee | 3655 | chip->select_chip(mtd, -1); |
7aa65bfd | 3656 | return PTR_ERR(type); |
1da177e4 LT |
3657 | } |
3658 | ||
07300164 HS |
3659 | chip->select_chip(mtd, -1); |
3660 | ||
7aa65bfd | 3661 | /* Check for a chip array */ |
e0c7d767 | 3662 | for (i = 1; i < maxchips; i++) { |
ace4dfee | 3663 | chip->select_chip(mtd, i); |
ef89a880 KB |
3664 | /* See comment in nand_get_flash_type for reset */ |
3665 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
1da177e4 | 3666 | /* Send the command for reading device ID */ |
ace4dfee | 3667 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
1da177e4 | 3668 | /* Read manufacturer and device IDs */ |
ace4dfee | 3669 | if (nand_maf_id != chip->read_byte(mtd) || |
07300164 HS |
3670 | nand_dev_id != chip->read_byte(mtd)) { |
3671 | chip->select_chip(mtd, -1); | |
1da177e4 | 3672 | break; |
07300164 HS |
3673 | } |
3674 | chip->select_chip(mtd, -1); | |
1da177e4 LT |
3675 | } |
3676 | if (i > 1) | |
20171642 | 3677 | pr_info("%d chips detected\n", i); |
61b03bd7 | 3678 | |
1da177e4 | 3679 | /* Store the number of chips and calc total size for mtd */ |
ace4dfee TG |
3680 | chip->numchips = i; |
3681 | mtd->size = i * chip->chipsize; | |
7aa65bfd | 3682 | |
3b85c321 DW |
3683 | return 0; |
3684 | } | |
7351d3a5 | 3685 | EXPORT_SYMBOL(nand_scan_ident); |
3b85c321 DW |
3686 | |
3687 | ||
3688 | /** | |
3689 | * nand_scan_tail - [NAND Interface] Scan for the NAND device | |
8b6e50c9 | 3690 | * @mtd: MTD device structure |
3b85c321 | 3691 | * |
8b6e50c9 BN |
3692 | * This is the second phase of the normal nand_scan() function. It fills out |
3693 | * all the uninitialized function pointers with the defaults and scans for a | |
3694 | * bad block table if appropriate. | |
3b85c321 DW |
3695 | */ |
3696 | int nand_scan_tail(struct mtd_info *mtd) | |
3697 | { | |
3698 | int i; | |
3699 | struct nand_chip *chip = mtd->priv; | |
97de79e0 | 3700 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
f02ea4e6 | 3701 | struct nand_buffers *nbuf; |
3b85c321 | 3702 | |
e2414f4c BN |
3703 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
3704 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && | |
3705 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); | |
3706 | ||
f02ea4e6 HS |
3707 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
3708 | nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize | |
3709 | + mtd->oobsize * 3, GFP_KERNEL); | |
3710 | if (!nbuf) | |
3711 | return -ENOMEM; | |
3712 | nbuf->ecccalc = (uint8_t *)(nbuf + 1); | |
3713 | nbuf->ecccode = nbuf->ecccalc + mtd->oobsize; | |
3714 | nbuf->databuf = nbuf->ecccode + mtd->oobsize; | |
3715 | ||
3716 | chip->buffers = nbuf; | |
3717 | } else { | |
3718 | if (!chip->buffers) | |
3719 | return -ENOMEM; | |
3720 | } | |
4bf63fcb | 3721 | |
7dcdcbef | 3722 | /* Set the internal oob buffer location, just after the page data */ |
784f4d5e | 3723 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
1da177e4 | 3724 | |
7aa65bfd | 3725 | /* |
8b6e50c9 | 3726 | * If no default placement scheme is given, select an appropriate one. |
7aa65bfd | 3727 | */ |
97de79e0 | 3728 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
61b03bd7 | 3729 | switch (mtd->oobsize) { |
1da177e4 | 3730 | case 8: |
97de79e0 | 3731 | ecc->layout = &nand_oob_8; |
1da177e4 LT |
3732 | break; |
3733 | case 16: | |
97de79e0 | 3734 | ecc->layout = &nand_oob_16; |
1da177e4 LT |
3735 | break; |
3736 | case 64: | |
97de79e0 | 3737 | ecc->layout = &nand_oob_64; |
1da177e4 | 3738 | break; |
81ec5364 | 3739 | case 128: |
97de79e0 | 3740 | ecc->layout = &nand_oob_128; |
81ec5364 | 3741 | break; |
1da177e4 | 3742 | default: |
d0370219 BN |
3743 | pr_warn("No oob scheme defined for oobsize %d\n", |
3744 | mtd->oobsize); | |
1da177e4 LT |
3745 | BUG(); |
3746 | } | |
3747 | } | |
61b03bd7 | 3748 | |
956e944c DW |
3749 | if (!chip->write_page) |
3750 | chip->write_page = nand_write_page; | |
3751 | ||
61b03bd7 | 3752 | /* |
8b6e50c9 | 3753 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
7aa65bfd | 3754 | * selected and we have 256 byte pagesize fallback to software ECC |
e0c7d767 | 3755 | */ |
956e944c | 3756 | |
97de79e0 | 3757 | switch (ecc->mode) { |
6e0cb135 SN |
3758 | case NAND_ECC_HW_OOB_FIRST: |
3759 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ | |
97de79e0 | 3760 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
9a4d4d69 | 3761 | pr_warn("No ECC functions supplied; " |
d0370219 | 3762 | "hardware ECC not possible\n"); |
6e0cb135 SN |
3763 | BUG(); |
3764 | } | |
97de79e0 HS |
3765 | if (!ecc->read_page) |
3766 | ecc->read_page = nand_read_page_hwecc_oob_first; | |
6e0cb135 | 3767 | |
6dfc6d25 | 3768 | case NAND_ECC_HW: |
8b6e50c9 | 3769 | /* Use standard hwecc read page function? */ |
97de79e0 HS |
3770 | if (!ecc->read_page) |
3771 | ecc->read_page = nand_read_page_hwecc; | |
3772 | if (!ecc->write_page) | |
3773 | ecc->write_page = nand_write_page_hwecc; | |
3774 | if (!ecc->read_page_raw) | |
3775 | ecc->read_page_raw = nand_read_page_raw; | |
3776 | if (!ecc->write_page_raw) | |
3777 | ecc->write_page_raw = nand_write_page_raw; | |
3778 | if (!ecc->read_oob) | |
3779 | ecc->read_oob = nand_read_oob_std; | |
3780 | if (!ecc->write_oob) | |
3781 | ecc->write_oob = nand_write_oob_std; | |
3782 | if (!ecc->read_subpage) | |
3783 | ecc->read_subpage = nand_read_subpage; | |
3784 | if (!ecc->write_subpage) | |
3785 | ecc->write_subpage = nand_write_subpage_hwecc; | |
f5bbdacc | 3786 | |
6dfc6d25 | 3787 | case NAND_ECC_HW_SYNDROME: |
97de79e0 HS |
3788 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
3789 | (!ecc->read_page || | |
3790 | ecc->read_page == nand_read_page_hwecc || | |
3791 | !ecc->write_page || | |
3792 | ecc->write_page == nand_write_page_hwecc)) { | |
9a4d4d69 | 3793 | pr_warn("No ECC functions supplied; " |
d0370219 | 3794 | "hardware ECC not possible\n"); |
6dfc6d25 TG |
3795 | BUG(); |
3796 | } | |
8b6e50c9 | 3797 | /* Use standard syndrome read/write page function? */ |
97de79e0 HS |
3798 | if (!ecc->read_page) |
3799 | ecc->read_page = nand_read_page_syndrome; | |
3800 | if (!ecc->write_page) | |
3801 | ecc->write_page = nand_write_page_syndrome; | |
3802 | if (!ecc->read_page_raw) | |
3803 | ecc->read_page_raw = nand_read_page_raw_syndrome; | |
3804 | if (!ecc->write_page_raw) | |
3805 | ecc->write_page_raw = nand_write_page_raw_syndrome; | |
3806 | if (!ecc->read_oob) | |
3807 | ecc->read_oob = nand_read_oob_syndrome; | |
3808 | if (!ecc->write_oob) | |
3809 | ecc->write_oob = nand_write_oob_syndrome; | |
3810 | ||
3811 | if (mtd->writesize >= ecc->size) { | |
3812 | if (!ecc->strength) { | |
e2788c98 MD |
3813 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
3814 | BUG(); | |
3815 | } | |
6dfc6d25 | 3816 | break; |
e2788c98 | 3817 | } |
9a4d4d69 | 3818 | pr_warn("%d byte HW ECC not possible on " |
d0370219 | 3819 | "%d byte page size, fallback to SW ECC\n", |
97de79e0 HS |
3820 | ecc->size, mtd->writesize); |
3821 | ecc->mode = NAND_ECC_SOFT; | |
61b03bd7 | 3822 | |
6dfc6d25 | 3823 | case NAND_ECC_SOFT: |
97de79e0 HS |
3824 | ecc->calculate = nand_calculate_ecc; |
3825 | ecc->correct = nand_correct_data; | |
3826 | ecc->read_page = nand_read_page_swecc; | |
3827 | ecc->read_subpage = nand_read_subpage; | |
3828 | ecc->write_page = nand_write_page_swecc; | |
3829 | ecc->read_page_raw = nand_read_page_raw; | |
3830 | ecc->write_page_raw = nand_write_page_raw; | |
3831 | ecc->read_oob = nand_read_oob_std; | |
3832 | ecc->write_oob = nand_write_oob_std; | |
3833 | if (!ecc->size) | |
3834 | ecc->size = 256; | |
3835 | ecc->bytes = 3; | |
3836 | ecc->strength = 1; | |
1da177e4 | 3837 | break; |
61b03bd7 | 3838 | |
193bd400 ID |
3839 | case NAND_ECC_SOFT_BCH: |
3840 | if (!mtd_nand_has_bch()) { | |
9a4d4d69 | 3841 | pr_warn("CONFIG_MTD_ECC_BCH not enabled\n"); |
193bd400 ID |
3842 | BUG(); |
3843 | } | |
97de79e0 HS |
3844 | ecc->calculate = nand_bch_calculate_ecc; |
3845 | ecc->correct = nand_bch_correct_data; | |
3846 | ecc->read_page = nand_read_page_swecc; | |
3847 | ecc->read_subpage = nand_read_subpage; | |
3848 | ecc->write_page = nand_write_page_swecc; | |
3849 | ecc->read_page_raw = nand_read_page_raw; | |
3850 | ecc->write_page_raw = nand_write_page_raw; | |
3851 | ecc->read_oob = nand_read_oob_std; | |
3852 | ecc->write_oob = nand_write_oob_std; | |
193bd400 ID |
3853 | /* |
3854 | * Board driver should supply ecc.size and ecc.bytes values to | |
3855 | * select how many bits are correctable; see nand_bch_init() | |
8b6e50c9 BN |
3856 | * for details. Otherwise, default to 4 bits for large page |
3857 | * devices. | |
193bd400 | 3858 | */ |
97de79e0 HS |
3859 | if (!ecc->size && (mtd->oobsize >= 64)) { |
3860 | ecc->size = 512; | |
3861 | ecc->bytes = 7; | |
193bd400 | 3862 | } |
97de79e0 HS |
3863 | ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes, |
3864 | &ecc->layout); | |
3865 | if (!ecc->priv) { | |
9a4d4d69 | 3866 | pr_warn("BCH ECC initialization failed!\n"); |
193bd400 ID |
3867 | BUG(); |
3868 | } | |
97de79e0 | 3869 | ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size); |
193bd400 ID |
3870 | break; |
3871 | ||
61b03bd7 | 3872 | case NAND_ECC_NONE: |
9a4d4d69 | 3873 | pr_warn("NAND_ECC_NONE selected by board driver. " |
d0370219 | 3874 | "This is not recommended!\n"); |
97de79e0 HS |
3875 | ecc->read_page = nand_read_page_raw; |
3876 | ecc->write_page = nand_write_page_raw; | |
3877 | ecc->read_oob = nand_read_oob_std; | |
3878 | ecc->read_page_raw = nand_read_page_raw; | |
3879 | ecc->write_page_raw = nand_write_page_raw; | |
3880 | ecc->write_oob = nand_write_oob_std; | |
3881 | ecc->size = mtd->writesize; | |
3882 | ecc->bytes = 0; | |
3883 | ecc->strength = 0; | |
1da177e4 | 3884 | break; |
956e944c | 3885 | |
1da177e4 | 3886 | default: |
97de79e0 | 3887 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
61b03bd7 | 3888 | BUG(); |
1da177e4 | 3889 | } |
61b03bd7 | 3890 | |
9ce244b3 | 3891 | /* For many systems, the standard OOB write also works for raw */ |
97de79e0 HS |
3892 | if (!ecc->read_oob_raw) |
3893 | ecc->read_oob_raw = ecc->read_oob; | |
3894 | if (!ecc->write_oob_raw) | |
3895 | ecc->write_oob_raw = ecc->write_oob; | |
9ce244b3 | 3896 | |
5bd34c09 TG |
3897 | /* |
3898 | * The number of bytes available for a client to place data into | |
8b6e50c9 | 3899 | * the out of band area. |
5bd34c09 | 3900 | */ |
97de79e0 HS |
3901 | ecc->layout->oobavail = 0; |
3902 | for (i = 0; ecc->layout->oobfree[i].length | |
3903 | && i < ARRAY_SIZE(ecc->layout->oobfree); i++) | |
3904 | ecc->layout->oobavail += ecc->layout->oobfree[i].length; | |
3905 | mtd->oobavail = ecc->layout->oobavail; | |
5bd34c09 | 3906 | |
7aa65bfd TG |
3907 | /* |
3908 | * Set the number of read / write steps for one page depending on ECC | |
8b6e50c9 | 3909 | * mode. |
7aa65bfd | 3910 | */ |
97de79e0 HS |
3911 | ecc->steps = mtd->writesize / ecc->size; |
3912 | if (ecc->steps * ecc->size != mtd->writesize) { | |
9a4d4d69 | 3913 | pr_warn("Invalid ECC parameters\n"); |
6dfc6d25 | 3914 | BUG(); |
1da177e4 | 3915 | } |
97de79e0 | 3916 | ecc->total = ecc->steps * ecc->bytes; |
61b03bd7 | 3917 | |
8b6e50c9 | 3918 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
1d0ed69d | 3919 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
97de79e0 | 3920 | switch (ecc->steps) { |
29072b96 TG |
3921 | case 2: |
3922 | mtd->subpage_sft = 1; | |
3923 | break; | |
3924 | case 4: | |
3925 | case 8: | |
81ec5364 | 3926 | case 16: |
29072b96 TG |
3927 | mtd->subpage_sft = 2; |
3928 | break; | |
3929 | } | |
3930 | } | |
3931 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; | |
3932 | ||
04bbd0ea | 3933 | /* Initialize state */ |
ace4dfee | 3934 | chip->state = FL_READY; |
1da177e4 | 3935 | |
1da177e4 | 3936 | /* Invalidate the pagebuffer reference */ |
ace4dfee | 3937 | chip->pagebuf = -1; |
1da177e4 | 3938 | |
a5ff4f10 | 3939 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
97de79e0 | 3940 | if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9)) |
a5ff4f10 JW |
3941 | chip->options |= NAND_SUBPAGE_READ; |
3942 | ||
1da177e4 | 3943 | /* Fill in remaining MTD driver data */ |
963d1c28 | 3944 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
93edbad6 ML |
3945 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
3946 | MTD_CAP_NANDFLASH; | |
3c3c10bb AB |
3947 | mtd->_erase = nand_erase; |
3948 | mtd->_point = NULL; | |
3949 | mtd->_unpoint = NULL; | |
3950 | mtd->_read = nand_read; | |
3951 | mtd->_write = nand_write; | |
3952 | mtd->_panic_write = panic_nand_write; | |
3953 | mtd->_read_oob = nand_read_oob; | |
3954 | mtd->_write_oob = nand_write_oob; | |
3955 | mtd->_sync = nand_sync; | |
3956 | mtd->_lock = NULL; | |
3957 | mtd->_unlock = NULL; | |
3958 | mtd->_suspend = nand_suspend; | |
3959 | mtd->_resume = nand_resume; | |
3960 | mtd->_block_isbad = nand_block_isbad; | |
3961 | mtd->_block_markbad = nand_block_markbad; | |
cbcab65a | 3962 | mtd->writebufsize = mtd->writesize; |
1da177e4 | 3963 | |
6a918bad | 3964 | /* propagate ecc info to mtd_info */ |
97de79e0 HS |
3965 | mtd->ecclayout = ecc->layout; |
3966 | mtd->ecc_strength = ecc->strength; | |
3967 | mtd->ecc_step_size = ecc->size; | |
ea3b2ea2 SL |
3968 | /* |
3969 | * Initialize bitflip_threshold to its default prior scan_bbt() call. | |
3970 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be | |
3971 | * properly set. | |
3972 | */ | |
3973 | if (!mtd->bitflip_threshold) | |
3974 | mtd->bitflip_threshold = mtd->ecc_strength; | |
1da177e4 | 3975 | |
0040bf38 | 3976 | /* Check, if we should skip the bad block table scan */ |
ace4dfee | 3977 | if (chip->options & NAND_SKIP_BBTSCAN) |
0040bf38 | 3978 | return 0; |
1da177e4 LT |
3979 | |
3980 | /* Build bad block table */ | |
ace4dfee | 3981 | return chip->scan_bbt(mtd); |
1da177e4 | 3982 | } |
7351d3a5 | 3983 | EXPORT_SYMBOL(nand_scan_tail); |
1da177e4 | 3984 | |
8b6e50c9 BN |
3985 | /* |
3986 | * is_module_text_address() isn't exported, and it's mostly a pointless | |
7351d3a5 | 3987 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
8b6e50c9 BN |
3988 | * to call us from in-kernel code if the core NAND support is modular. |
3989 | */ | |
3b85c321 DW |
3990 | #ifdef MODULE |
3991 | #define caller_is_module() (1) | |
3992 | #else | |
3993 | #define caller_is_module() \ | |
a6e6abd5 | 3994 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
3b85c321 DW |
3995 | #endif |
3996 | ||
3997 | /** | |
3998 | * nand_scan - [NAND Interface] Scan for the NAND device | |
8b6e50c9 BN |
3999 | * @mtd: MTD device structure |
4000 | * @maxchips: number of chips to scan for | |
3b85c321 | 4001 | * |
8b6e50c9 BN |
4002 | * This fills out all the uninitialized function pointers with the defaults. |
4003 | * The flash ID is read and the mtd/chip structures are filled with the | |
4004 | * appropriate values. The mtd->owner field must be set to the module of the | |
4005 | * caller. | |
3b85c321 DW |
4006 | */ |
4007 | int nand_scan(struct mtd_info *mtd, int maxchips) | |
4008 | { | |
4009 | int ret; | |
4010 | ||
4011 | /* Many callers got this wrong, so check for it for a while... */ | |
4012 | if (!mtd->owner && caller_is_module()) { | |
d0370219 | 4013 | pr_crit("%s called with NULL mtd->owner!\n", __func__); |
3b85c321 DW |
4014 | BUG(); |
4015 | } | |
4016 | ||
5e81e88a | 4017 | ret = nand_scan_ident(mtd, maxchips, NULL); |
3b85c321 DW |
4018 | if (!ret) |
4019 | ret = nand_scan_tail(mtd); | |
4020 | return ret; | |
4021 | } | |
7351d3a5 | 4022 | EXPORT_SYMBOL(nand_scan); |
3b85c321 | 4023 | |
1da177e4 | 4024 | /** |
61b03bd7 | 4025 | * nand_release - [NAND Interface] Free resources held by the NAND device |
8b6e50c9 BN |
4026 | * @mtd: MTD device structure |
4027 | */ | |
e0c7d767 | 4028 | void nand_release(struct mtd_info *mtd) |
1da177e4 | 4029 | { |
ace4dfee | 4030 | struct nand_chip *chip = mtd->priv; |
1da177e4 | 4031 | |
193bd400 ID |
4032 | if (chip->ecc.mode == NAND_ECC_SOFT_BCH) |
4033 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); | |
4034 | ||
5ffcaf3d | 4035 | mtd_device_unregister(mtd); |
1da177e4 | 4036 | |
fa671646 | 4037 | /* Free bad block table memory */ |
ace4dfee | 4038 | kfree(chip->bbt); |
4bf63fcb DW |
4039 | if (!(chip->options & NAND_OWN_BUFFERS)) |
4040 | kfree(chip->buffers); | |
58373ff0 BN |
4041 | |
4042 | /* Free bad block descriptor memory */ | |
4043 | if (chip->badblock_pattern && chip->badblock_pattern->options | |
4044 | & NAND_BBT_DYNAMICSTRUCT) | |
4045 | kfree(chip->badblock_pattern); | |
1da177e4 | 4046 | } |
e0c7d767 | 4047 | EXPORT_SYMBOL_GPL(nand_release); |
8fe833c1 RP |
4048 | |
4049 | static int __init nand_base_init(void) | |
4050 | { | |
4051 | led_trigger_register_simple("nand-disk", &nand_led_trigger); | |
4052 | return 0; | |
4053 | } | |
4054 | ||
4055 | static void __exit nand_base_exit(void) | |
4056 | { | |
4057 | led_trigger_unregister_simple(nand_led_trigger); | |
4058 | } | |
4059 | ||
4060 | module_init(nand_base_init); | |
4061 | module_exit(nand_base_exit); | |
4062 | ||
e0c7d767 | 4063 | MODULE_LICENSE("GPL"); |
7351d3a5 FF |
4064 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
4065 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); | |
e0c7d767 | 4066 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |