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